HDU 3336 Count the string

 1 //this problem is same like to (POJ)Seek the Name, Seek the Fame
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #define N 200010
 7 using namespace std;
 8 
 9 char a[N];
10 int Next[N];
11 int Len;
12 int dp[N];//dp[i] represents from 1 ~ i strings, how many prefixs are end by i
13 const int mod = 10007;
14 
15 void GetNext()
16 {
17     int i = 0, j = Next[0] = -1;
18     while (i < Len)
19     {
20         if (j == -1 || a[i] == a[j])
21             Next[++i] = ++j;
22         else
23             j = Next[j];
24     }
25 }
26 
27 int main(void)
28 {
29     ios::sync_with_stdio(false);
30     int cas;
31     cin >> cas;
32     while (cas--)
33     {
34         cin >> Len >> a;
35         GetNext();
36         int res = 0;
37         memset(dp, 0, sizeof(dp));
38         for (int i = 1; i <= Len; ++i)
39         {
40             dp[i] = (dp[Next[i]] + 1) % mod;// eg:(1 + 1) % mod <=> 1 % mod + 1 % mod
41             res = (res + dp[i]) % mod;
42         }
43         cout << res << endl;
44     }
45 
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/ducklu/p/8989223.html