bzoj 3620: 似乎在梦中见过的样子

暴力处理每一个左端点,然后就和NOI2014动物园是差不多(几乎是一样的)

(一开始还以为是什么KMP的奇怪姿势)

 1 #include<bits/stdc++.h>
 2 #define N 1000005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
10     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
11     return x*f;
12 }
13 int k,n,fail[N],ans;
14 char ch[N];
15 int main()
16 {
17     scanf("%s",ch+1); k=ra();
18     n=strlen(ch+1);
19     for (int i=1; i<=n-k*2; i++)
20     {
21         int fix=i-1; fail[i]=i-1;
22         for (int j=i+1; j<=n; j++)
23         {
24             while (ch[fix+1]!=ch[j] && fix>i-1) fix=fail[fix]; 
25             if (ch[fix+1]==ch[j]) fix++; fail[j]=fix;
26         }
27         for (int j=i+k*2; j<=n; j++)
28         {
29             int pos=j;
30             while ((fail[pos]-i+1)*2>=j-i+1)
31                 pos=fail[pos];
32             if (fail[pos]-i+1>=k) ans++;
33         }
34     }
35     cout<<ans;
36     return 0;
37 }

 

posted @ 2017-02-24 20:38  ws_ccd  阅读(224)  评论(0编辑  收藏  举报