蒟蒻刚学字符串,求助
查看原帖
蒟蒻刚学字符串,求助
295504
Into_qwq楼主2020/6/25 21:58
#include "bits/stdc++.h"
using namespace std;
const int N=1000005;
char str1[N],str2[N];
int len1,len2;
struct KMP{
    int next[N]={-1};
    inline void scan(){scanf("%s%s",str1,str2);len1=strlen(str1),len2=strlen(str2);}
    inline void getnext(){
        for(int i=1;i<len2;++i){
            int x=next[i];
            while(str2[x+1]!=str2[i]&&x>=0) x=next[x];
            if(str2[x+1]==str2[i]) next[i]=x+1;
            else next[i]=-1;
        }
    }
    inline void kmp(){
        for(int i=0,j=0;i<len1;)
            if(str1[i]==str2[j]){++i,++j;if(j==len2){printf("%d",i-len2+1),putchar('\n');j=next[j-1]+1;}}
            else if(!j) ++i;
            else j=next[j-1]+1;
    }
    inline void print(){putchar('d');for(int i=0;i<len2;++i) cout<<next[i],putchar(' '),putchar('d');}
    inline void work(){scan();getnext();putchar('D');kmp();putchar('D');print();putchar('D');}
}t;
int main(){
    t.work();
    return 0;
}
2020/6/25 21:58
加载中...