玄关。UAC,hack T了怎么办
查看原帖
玄关。UAC,hack T了怎么办
551417
CommonDigger楼主2024/7/12 20:08
/*
Luogu P3375 KMP
https://www.luogu.com.cn/problem/P3375
 */
#include "iostream"
#include "cstring"
using namespace std;
char s[1000005], sub[1000005];
int _next[1000005];
void getNext(){
    int j=0, temp=strlen(sub+1);
    for (int i=2;i<=temp;i++)
    {
        while(j&&sub[i]!=sub[j+1])
            j=_next[j];
        if(sub[j+1]==sub[i])j++;
        _next[i]=j;
    }
}
void index_KMP(){
    int j=0, temp=strlen(s+1);
    for(int i=1;i<=temp;i++)
    {
        while(j>0&&sub[j+1]!=s[i]) j=_next[j];
        if(sub[j+1]==s[i]) j++;
        if (j==strlen(sub+1)){
            cout << (int)(i-strlen(sub+1)+1) << endl;
            j=_next[j];
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> (s+1) >> (sub+1);
    getNext();
    index_KMP();
    for(int i=1;i<=strlen(sub+1);i++){
        cout << _next[i] << " ";
    }
}
2024/7/12 20:08
加载中...