Oulipo HDU - 1686(kmp)

    科技2024-08-09  27

    Oulipo HDU - 1686

    题意:

    要你求有几次成功匹配(允许重叠)

    AC

    #include <iostream> #include <cstring> using namespace std; const int maxn=1e6+10; const int maxm=1e4+10; char s[maxn],p[maxm]; int nxt[maxm]; void getnxt(char *p){ int m = strlen(p); int i=0,j=-1; nxt[0]=-1; while(i<m){ if(j==-1||p[i]==p[j])nxt[++i]=++j; else j=nxt[j]; } } int kmp(){ int res=0; int i=0, j=0; int n=strlen(s),m=strlen(p); while(i<n){ if(j==-1||s[i]==p[j])i++,j++; else j=nxt[j]; if(j==m)res++; } return res; } int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int tt;cin>>tt; while(tt--){ cin>>p>>s; getnxt(p); cout<<kmp()<<endl; } return 0; }
    Processed: 0.009, SQL: 8