1 条题解

  • 0
    @ 2022-08-13 22:08:20
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef unsigned long long UL;
    
    UL h[400005], mul[400005], K = 1000000031;
    char s[400005];
    
    int main() {
        mul[0] = 1;
    
        for (int i = 1; i <= 400000; i++) //初始化每个位置的权值
            mul[i] = mul[i - 1] * K;
    
        while (~scanf("%s", s + 1)) { //输入字符串
            int n = strlen(s + 1); //求得字符串长度
    
            for (int i = 1; i <= n; i++) {
                h[i] = h[i - 1] * K + s[i] - 'a' +
                       1; //计算前i个字符的哈希值 a的值为1,b的值为2,以此类推
            }
    
            for (int i = 1; i <= n; i++) {
                if (h[i] == h[n] - (h[n - i]*mul[i]))
                    printf("%d ", i);
            }
    
            putchar('\n');
        }
    
        return 0;
    }
    
  • 1

Seek the Name, Seek the Fame

信息

ID
1509
难度
11
分类
KMP字符串 | 其他 点击显示
标签
递交数
1
已通过
1
通过率
100%
上传者