Hi guys, as the title stated, I really need your help with this problem

You have a set. Initially the set is empty. Given Q queries (Q <= 10^5) , there are two types of queries:

1 s : s is a string, add s to the set. If the set already contains s, do nothing

2 S : For all string k that belong to the set, calculate the sum of D(k) * length(k), D(k) is the number of occurrences of k in S as a substring (example, aa is a substring of aabc, ac is not a substring of aabc) and length(k) is length of k

It is guaranteed that the sum of length of these strings don't exceed 10^5

Example :

Q = 5

1 aa

2 aa

1 aa

1 ab

2 ababaa

-> output : 2 , 6

Note: After the first query, the set is {aa}, hence the answer for the second query is 2

After the fourth query, the set is {aa,ab} -> ababaa = 6 because ab appears twice, aa appear once : 2*2 + 2*1 = 6

Auto comment: topic has been updated by desperateboy12344 (previous revision, new revision, compare).You can use AC-automaton to do this.Firstly,take the inquiry offline,record the first appearing time for all s and build an AC-automaton for them.Process inquiries in time order,then you can use the fail tree and some data structures to solve it.

AC automation? I could really use such a thing in contests.

By AC, did you mean Aho Corasick????