HDU3336-- so prefix and the number of occurrences (KMP)

Meaning of the questions: http://acm.hdu.edu.cn/showproblem.php?pid=3336

rt

Ideas: https://www.cnblogs.com/Tree-dream/p/7443897.html

Almost, dp recursive just simplify it.

 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
 2 #include <cstdio>//sprintf islower isupper
 3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
 4 #include <iostream>//pair
 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
 6 #include <bitset>
 7 //#include <map>
 8 //#include<unordered_map>
 9 #include <vector>
10 #include <stack>
11 #include <set>
12 #include <string.h>//strstr substr strcat
13 #include <string>
14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
15 #include <cmath>
16 #include <deque>
17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
18 #include <vector>//emplace_back
19 //#include <math.h>
20 #include <cassert>
21 #include <iomanip>
22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
25 //******************
26 clock_t __START,__END;
27 double __TOTALTIME;
28 void _MS(){__START=clock();}
29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
30 //***********************
31 #define rint register int
32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
34 #define mem(a,b) memset(a,b,sizeof(a))
35 #define pr printf
36 #define sc scanf
37 #define ls rt<<1
38 #define rs rt<<1|1
39 typedef pair<int,int> PII;
40 typedef vector<int> VI;
41 typedef unsigned long long ull;
42 typedef long long ll;
43 typedef double db;
44 const db E=2.718281828;
45 const db PI=acos(-1.0);
46 const ll INF=(1LL<<60);
47 const int inf=(1<<30);
48 const db ESP=1e-9;
 49  const  int MOD = ( int ) 1E9 + . 7 ;
 50  const  int N = ( int ) 1e6 + 10 ;
 51 is  
52 is  int the Next [N], LenB;
 53 is  char B [N];
 54 is  
55  void set_naxt ()                   // substring the next array: the longest prefix and a suffix of the longest string of the same length 
56 is  {
 57 is      int I = 0 , J = - . 1 ;
 58      the next [ 0 ] = - . 1 ;                   // initialize to -1 does not exist the same longest prefix and suffix maximum
59      the while (I < LenB)
 60      {
 61 is          IF (j == - . 1 || B [I] == B [j])
 62 is          {
 63 is              I ++ ;
 64              j ++ ;
 65              the Next [I] = j;            // the j (same maximum prefix length and the maximum suffix) assigned Next [I] 
66          }
 67          the else 
68              J = the Next [J];            // Previous retrospective 
69      }
 70  }
 71 is  int DP [N];
 72  
73 is  void Solve ()
 74  {
75     sc("%d",&lenb);
76     sc("%s",b);
77     b[lenb++]='%';
78     fo(i,0,lenb)dp[i]=0;
79     set_naxt();
80     int ans=0;
81     for(int i=1;i<lenb;++i)
82         dp[i]=dp[Next[i]]+1,ans+=dp[i],ans%=10007;
83     pr("%d\n",ans);
84 }
85 
86 int main()
87 {
88     int T;
89     sc("%d",&T);
90     while(T--)solve();
91     return 0;
92 }
93 
94 /**************************************************************************************/

 

Guess you like

Origin www.cnblogs.com/--HPY-7m/p/12331209.html