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, 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
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