1 条题解

  • 1
    @ 2024-7-25 13:31:39

    参考答案:

    #include<iostream>
    
    using namespace std;
    
    const int N = 100010, M = 1000010;
    int ne[N];
    char s[M], p[N];
    int n, m;
    
    int main()
    {
        cin >> n >> p + 1 >> m >> s + 1;
    
        for (int i = 2, j = 0; i <= n; ++i)
        {
            while (j && p[i] != p[j + 1]) j = ne[j];
            if (p[i] == p[j + 1])
                ++j;
            ne[i] = j;
        }
    
        for (int i = 1, j = 0; i <= m; ++i)
        {
            while (j && s[i] != p[j + 1]) j = ne[j];
            if (s[i] == p[j + 1]) ++j;
            if (j == n)
            {
                j = ne[j];
                cout << i - n << ' ';
            }
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    83
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    8
    已通过
    2
    上传者