# bzoj1511 [POI2006]OKR-Periods of Words kmp+乱搞

## 1511: [POI2006]OKR-Periods of Words

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 351  Solved: 220
[Submit][Status][Discuss]

8
babababa

## Sample Output

24

 1 #include<cstring>
2 #include<cmath>
3 #include<cstdio>
4 #include<algorithm>
5 #include<iostream>
6
7 #define ll long long
8 #define inf 1000000007
9 #define N 1000007
10
11 #define Wb putchar(' ')
12 #define We putchar('\n')
13 #define rg register int
14 using namespace std;
16 {
17     int x=0,f=1;char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
19     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
20     return x*f;
21 }
22 inline void write(ll x)
23 {
24     if(x<0) putchar('-'),x=-x;
25     if (x==0) putchar(48);
26     int num=0;char c[20];
27     while(x) c[++num]=(x%10)+48,x/=10;
28     while(num) putchar(c[num--]);
29 }
30
31 int n,t[N],f[N];
32 ll ans;
33 char s[N];
34
35 void make_nxt()
36 {
37     t[0]=-1;
38     for (rg i=0,j;i<n;i++)
39     {
40         j=t[i];
41         while(j!=-1&&s[i]!=s[j])j=t[j];
42         t[i+1]=++j;
43     }
44 }
45 int main()
46 {
48     make_nxt();
49     for (rg i=1;i<=n;i++)
50     {
51         if (t[i]) f[i]=f[t[i]];
52         else f[i]=i;
53         if (i-t[i]==i) continue;
54         ans+=i-f[i];
55     }
56     write(ans);
57 }

posted @ 2018-05-03 06:54  Kaiser-  阅读(207)  评论(0编辑  收藏  举报