Beautiful Substrings

0

0 votes
Depth First Search, Trie, Data Structures, Dynamic Programming, Advanced Data Structures
Problem

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 109+7.

Input format

  • The first line contains N denoting the number of strings.
  • The  ith of the next N lines contains the string Si.

Output format

 The sum Emil gets modulo 109+7.

Constraints

 1N1051|Si|1051Ni=1|Si|2106

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 8 different prefixes, count of beautiful substrings in each prefix is listed below

  • b - 0
  • a - 0
  • ab - 0
  • bb - 1
  • bba - 1
  • bbab - 2
  • bbaba - 2
  • bbabaa - 2
Editor Image

?