洛谷P3375

#pragma once
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<complex>
#include<fstream>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<string>
#include<utility>
#include<vector>
#pragma warning(disable:4996 6031 6385 6386 6297 26451)
#define go(a,b,c,d) for(int a=b;a<=c;a+=d)
#define sgo(a,b) for(auto a:b)
#define lson root<<1
#define rson lson|1
#define Mid int m=((l+r)>>1)
#define ls lson,l,m
#define rs rson,m+1,r
#define rt 1,1,n
#define INF32 0x7f7f7f7f
#define INF64 0x7f7f7f7f7f7f7f7f
#define r(x) return x
#define rv return
#define pii pair<int,int>
#define pll pair<ll,ll>
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int l1, l2, j, nxt[1000001];
char s1[1000001], s2[1000001];
signed main() {
    cin >> s1 + 1 >> s2 + 1;
    l1 = strlen(s1 + 1);
    l2 = strlen(s2 + 1);
    go(i, 2, l2, 1) {
        while (j && s2[i] != s2[j + 1])
            j = nxt[j];
        if (s2[j + 1] == s2[i])
            ++j;
        nxt[i] = j;
    }
    j = 0;
    go(i, 1, l1, 1) {
        while (j && s2[j + 1] != s1[i])
            j = nxt[j];
        if (s2[j + 1] == s1[i])
            ++j;
        if (j == l2)
            cout << i - l2 + 1 << endl,
            j = nxt[j];
    }
    go(i, 1, l2, 1)
        cout << nxt[i] << ' ';
    return 0;
}

发表回复