求助70分,不知怎么改
查看原帖
求助70分,不知怎么改
865206
Braveme楼主2024/10/3 21:09
#include<bits/stdc++.h>
using namespace std;
int ne[1000010];
char a[1000010],b[1000010];
void get_next(){
    int j = 0,len = strlen(b);
    ne[0] = 0;
    for(int i = 1;i < len;i++){
        while(j > 0 and b[i] != b[j]) j = ne[j-1];
        if(b[i] == b[j])j++;
        ne[i] = j;
    }
}
void kmp(){
    int j = 0,nl = strlen(a),ml= strlen(b);
    get_next();
    for(int i = 0;i < nl;i++){
        while(j > 0 and a[i] != b[j]) j = ne[j-1];
        if(a[i]== b[j])j++;
        if(j == ml){
            cout << i-ml+2<<endl;
            i = i-ml+2,j = 0;
        }
    }
    return;
}
int main(){
    cin >> a >> b;
    kmp();
    int ml = strlen(b);
    for(int i = 0;i < ml;i++){
        cout << ne[i]<<" ";
    }
}

第12和14点错了https://www.luogu.com.cn/record/179786725 路过的大神帮忙看看 ,万分感谢

2024/10/3 21:09
加载中...