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]

Description

一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, 使得 A = PB. 如果 P A 并且 P 不是一个空串,那么我们说 P 是A的一个proper前缀. 定义Q 是A的周期, 当且仅当Q是A的一个proper 前缀并且A是QQ的前缀(不一定要是proper前缀). 比如串 abab 和 ababab 都是串abababa的周期. 串A的最大周期就是它最长的一个周期或者是一个空串(当A没有周期的时候), 比如说, ababab的最大周期是abab. 串abc的最大周期是空串. 给出一个串,求出它所有前缀的最大周期长度之和.

Input

第一行一个整数 k ( 1 k 1 000 000) 表示串的长度. 接下来一行表示给出的串.

Output

输出一个整数表示它所有前缀的最大周期长度之和.

Sample Input

8
babababa

Sample Output

24
 

这题要求每个前缀的最长循环节的和。给出最长循环节的定义,假设循环节为q,那么要求该前缀是两个q相连的字符串的子串,

并且长度大于q。如一个前缀的最长循环节是他本身那么对答案的贡献为0.这个不难发现最长的循环节一定循环了不超过两次,

因为最短循环节是n-末位失配。那么除去末位失配这一段,剩下的保持不变,然后让末位失配这一段求失配不为0时的最短的

失配,就是最长循环节多出来的一块。如果每次都找肯定不行,所以递推 f[i]=(t[i]?f[t[i]]:i)

 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;
15 inline int read()
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 {
47     n=read();scanf("%s",s);
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 }

猜你喜欢

转载自www.cnblogs.com/fengzhiyuan/p/8983582.html