Returning Home

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 512M

Authors:
Problem type

After a long day, Adam is on his way home. However, after suffering a sudden bout of amnesia, he has forgotten the name of the street where he lives!

Fortunately, he has a list of N potential names, from S1 to Sn, which are composed of lowercase Latin characters, all with equal length. He also has a set of M quirky constraints that the street name should satisfy; each constraint is of the form [Li,Ri], meaning that the substring from Li to Ri should be a palindrome. (Recall that a palindrome is a string which reads the same forwards and backwards, like racecar or tacocat).

Can you help Adam figure out the total number of strings on his list that satisfy these constraints?

Constraints

For all subtasks:

1N5000

1M500000

1|Si|30, |Si|=|Si+1| for all integer i from 1 to N1.

1LiRi|Si|

Subtask 1 [20%]

1M30

Subtask 2 [80%]

No additional constraints.

Input Specification

The first line will contain the values for N, M, and |Si|, denoting the number of strings, number of queries, and the length of each string.

The next N lines will contain Si, the ith string Adam is considering.

The next M lines after that will contain two numbers Li and Ri, denoting the substring from Li to Ri which must be a palindrome.

Output Specification

Print out, on a single line, the number of strings that Adam is considering which satisfies the constraints.

Sample Input 1

Copy
3 2 8
maineste
aracecar
reeeeeee
3 3
2 8

Sample Output 1

Copy
2

Explanation for Sample Output 1

Though all three candidate strings satisfy the first constraint, only the second and third strings satisfy the 2nd to 8th letter being also a palindrome.

Sample Input 2

Copy
2 2 3
aaa
aba
1 3
2 3

Sample Output 2

Copy
1

Explanation for Sample Output 2

Only the first string satisfies both the string itself being a palindrome and the substring [2,3] also being a palindrome.


Comments

There are no comments at the moment.