wa了5个点求调!
查看原帖
wa了5个点求调!
1148832
LabmemNo_012LzTopic楼主2024/12/16 20:09
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<set>
using namespace std;

char s[1000005], ss[2000005];
int k[2000005] = { 0 };

int main()
{
    cin >> s >> ss;
    int n, m;
    n = strlen(s);
    m = strlen(ss);
    ss[m] = '#';
    for (int i = m + 1; i <= m + n; i++)
    {
        ss[i] = s[i - m - 1];
    }
    for (int i = 1; i <= m + n; i++)
    {
        if (ss[i] == ss[k[i - 1]])
        {
            k[i] = k[i - 1] + 1;
        }
        else
        {
            int x, y;
            x = k[i - 1];
            while (x > 0)
            {
                if (ss[i] == ss[k[x - 1]])
                {
                    k[i] = k[x - 1] + 1;
                    break;
                }
                else
                {
                    x = k[x - 1] - 1;
                }
            }
        }
        if (k[i] == m)
        {
            printf("%d\n", i - m - m + 1);
        }
    }
    for (int i = 0; i < m; i++)
    {
        printf("%d ", k[i]);
    }
    return 0;
}
2024/12/16 20:09
加载中...