hdu 3336 Count the string(kmp文字列比較)


It is well known that Aekdy Coin is good at string probles a s number theory probles.When given a string s,we can write dowl the non-emipty prefixs of string.For example:ab firewe can count the times it maches in s.So we can see that prefix“a”maches twice,“ab”maches twice,“ab”maches once.Now ararararararasked to cacacacacacacate the ab the ab the ab the at the ab the attttttttttttttttttttttttttttttttttttttttttwice,ab+at the at the ab+at the ab+at the ab+at the at the ab+ab+ge,so outputut the answer mod 10007.Input The first line is a single integer T、indicating the number of test case s.For each case、the first line is a n integern(1=n<2000)、which isthe ininininininininininininininininininininininininininininininininininininininininininststststres the the the stststststrereres.ststres.sts.strereres.strerererereres.sts.sts.sts.sts.strererereres.strech case、output only one number:the sum of the match times for all the prefixes of s mod 10007.Sample Input
1
4
abab
Sample Output
6
#include
#include
#include
#include
using namespace std;

#define M 200020
int ne[M];

void GetNext(char *a, int n)
{
    int i = 0, j = -1;
    ne[0] = -1;
    while (i < n)
    {
        if(j == -1 || a[i] == a[j]){
            ne[++i] = ++j;
        }
        else j = ne[j];//      
    }
}

int main()
{
    int t, l, i;
    scanf("%d", &t);
    while(t--)
    {
        char a[M];
        int s = 0;
        scanf("%d", &l);
        scanf("%s", &a);
        GetNext(a, l);
        s += l + ne[l];     //l        ,                      。ne[l]           ,                  ,    ne[l] 
        for(i = 0; i < l; i++){
            if(ne[i] > 0 && ne[i + 1] - ne[i] != 1)
                s += ne[i];       //  ne       ,                 ,                  
        }
        s = s % 10007;
        printf("%d
"
, s); } return 0; }
怖いですね.私はいつでもこのように考えを簡略化すればいいです.オーディはオーディエンスです.私の考えはtの私はつらいです.