十分暴力的KMP,枚举左端点,在向右侧推进的同时,取较小的la保证条件,n方暴力
#include<bits/stdc++.h> #define rep(i,j,k) for(int i=j;i<=k;i++) #define inf 0x3fffffff using namespace std; const int N=20000; char s[N];int l,k,nxt[N],la[N],ans; int main(){ freopen("3620.in","r",stdin); freopen("3620.out","w",stdout); scanf("%s",s+1);l=strlen(s+1);scanf("%d",&k);ans=0; rep(i,1,l){ nxt[i]=i-1,la[i]=inf; rep(ii,i,l-1){ int j=nxt[ii];while(j>=i&&s[ii+1]!=s[j+1])j=nxt[j]; if(s[ii+1]==s[j+1]) ++j;nxt[ii+1]=j; if(nxt[ii+1]-i+1<k)la[ii+1]=inf;else{ la[ii+1]=min(la[nxt[ii+1]],nxt[ii+1]); if(la[ii+1]-i+1<=(ii+1-i)>>1) ++ans;} } } printf("%d",ans); return 0; }
原文地址:https://www.cnblogs.com/asdic/p/9645957.html
时间: 2024-11-09 06:43:58