Constructing buildings

5

3 votes
Hard, Algorithms, String Searching, String Algorithms, Hashing, String
Problem

Your task is to construct a building. There are N people in your city and the ith person contains a string codeword Si. Each of them randomly selects a non-empty substring of his codeword and calls this substring Flag. The building is constructed only if all their Flag 's are palindrome and pairwise identical. 

You are not sure whether the building will get constructed. Your task is to calculate the probability that the building is constructed so that you can prepare for the next construction. It can be shown that this probability can be expressed as a fraction PQ, where P and Q are coprime integers, P0Q>0 and Q is coprime with 109+7. You should compute (PQ1)mod(109+7), where Q1 denotes the multiplicative inverse of Q modulo 109+7.

Input format

  • First line: A single integer N denoting the number of people
  • Next N lines: A string where the ith of these lines denote the codeword of the ithperson

Output format

Print a single integer denoting the answer to the problem.

Constraints

1N105

1|Si|106

 i=N i=1|Si|106

All codewords consist of only lowercase Latin letters

Time Limit: 5
Memory Limit: 512
Source Limit:
Explanation

We consider possible candidate palindromes one by one :

  • Flag= a"
    •  1st string : {1,1}, 2nd string : {2,2}
    •  1st string : {1,1}, 2nd string : {3,3}
    •  1st string : {2,2}, 2nd string : {2,2}
    •  1st string : {2,2}, 2nd string : {3,3}
  • Flag= aa"
    •  1st string : {1,2}, 2nd string : {2,3}
  • Flag= c"
    •  1st string : {3,3}, 2nd string : {1,1}

Total number of ways to choose identical palindrome Flag=6

Total number of ways to choose Flag=3(3+1)23(3+1)2=36

So Probability to choose identical palindrome Flag = 636

Editor Image

?