Beautiful Substrings
Practice
0 (0 votes)
Depth first search
Trie
Data structures
Dynamic programming
Advanced data structures
Problem
86% Success 1301 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A string S is a substring of string T if it is possible to choose several consecutive letters in T in such a way that they form S.

A string is called beautiful if it can be split into substrings, such that for every substring the first and last characters are 'b' and all other characters are 'a'.

For instance, strings "bab", "bb", "baab", "baaabbabbbbaab" are beautiful strings, but strings "baa", "bbb", "b", "baabab" are not beautiful.

Given \(N\) strings consisting of letters 'a' and 'b'.

Emil decided to write down all possible prefixes of the given strings (he will write each prefix only once which mean he'll write distinct prefix only). After that he will count the number of beautiful substrings in every prefix and sum them up (a beautiful substring will be counted as many times as it is contained in each prefix).

Output the sum Emil will get.

As the answer can be pretty large output it modulo \(10^9 + 7\).

Input format

  • The first line contains \(N\) denoting the number of strings.
  • The  \(i'th\) of the next \(N\) lines contains the string \(S_i\).

Output format

 The sum Emil gets modulo \(10^9 + 7\).

Constraints

 \(1 \leq N \leq 10^5 \\1 \leq |S_i| \leq 10^5 \\1 ≤ \displaystyle\sum_{i=1}^{N} |S_i| ≤ 2*10^6\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
4 votes
Tags:
Advanced Data StructuresData StructuresHardSegment TreesTries
Points:50
6 votes
Tags:
Data StructuresAdvanced Data StructuresTrie
Points:50
3 votes
Tags:
TrieAdvanced Data StructuresData Structures