西电数据结构上机题目-改进的模式匹配追踪算法KMP

希望学长的代码能给大二西电er们带来帮助(大家一定要好好学习数据结构,合理利用资源)

大家找到了什么bug或有什么改进意见可以私信我或在下方留言,我都会看的

不多说废话直接上代码​​

 

#include
using namespace std;
#define fori(i,a,b) for(int i=a;i<=b;++i)
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int n, m;
int ne[N];
string b, a;  //b是模板串 a是模式串
void printNext() {
    cout << "*********next 数组内容: ";
    for (int i = 1; i <= m; i++) {
        printf("%d ", ne[i]-1);
    }
    puts("");
}
signed main() {
    cin >> a >>  b;
    n = a.size(), m = b.size();
    int ans = 0;
    a = " " + a;
    b = " " + b;
    for (int i = 2, j = 0; i <= m; i++) {
        while (j && b[i] != b[j + 1])j = ne[j]; 
        if (b[i] == b[j + 1])j++; 
        ne[i] = j;
    }
    for (int i = 1, j = 0; i <= n; i++) {
        while (j && a[i] != b[j + 1]) {
            cout << i << " " << a[i] << " " << j + 1 << " " << b[j + 1] << endl;
            ans++;
            j = ne[j];
        }
        cout << i << " " << a[i]<<" "<

你可能感兴趣的:(数据结构,算法,c++,c语言)