Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message, which is defined as
where the sum is over all nonempty strings p and is the number of occurences of p in s as a substring. (Note that the sum is infinite, but it only has a finite number of nonzero summands.)
Heidi refuses to do anything else until she knows how to calculate this self-similarity. Could you please help her? (If you would like to instead convince Heidi that a finite string cannot be a fractal anyway – do not bother, we have already tried.)
The input starts with a line indicating the number of test cases T (1 ≤ T ≤ 10). After that, T test cases follow, each of which consists of one line containing a string s (1 ≤ |s| ≤ 100 000) composed of lowercase letters (a-z).
Output T lines, every line containing one number – the answer to the corresponding test case.
4 aa abcd ccc abcc
5 10 14 12
A string s contains another string p as a substring if p is a contiguous subsequence of s. For example, ab is a substring of cab but not of acb.
题目大意:求一个字符串所有子串的出现次数平方和
惊了,我会Hard但是不会做normal
这个,参考一下弦论的话就是SAM裸题吧
SAM的每个状态代表了一些子串,它们的出现次数是就是R集合的大小,而子串的个数就是maxl-minl+1
求出SAM,求出每个状态的R集合大小s,以及maxl和minl
最终的答案就是 $ \sum (maxl-minl+1)s^2 $
R集合不是要拓扑排序么,事实上直接按照MAXL基数排序即可,从黄学长那学来的奇技淫巧
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include