求助!
  • 板块灌水区
  • 楼主封禁用户
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/7 19:07
  • 上次更新2023/11/7 20:56:02
查看原帖
求助!
1183222
封禁用户楼主2023/11/7 19:07

https://www.luogu.com.cn/problem/CF126B

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long  ULL;
string s;
string ans;
const int maxn = 1e6 + 6;
ULL pw[maxn];
ULL Hash[maxn];

ULL h(int i, int len) {
    return Hash[i] - Hash[i + len] * pw[len];
}

int main () {
    cin >> s;
    
    pw[0] = 1;
    for (int i = 1;i <= maxn; i++) {
        pw[i] = pw[i - 1] * 131;
    }
    
    int lens = s.size();
    Hash[lens] = 0;
    for (int i = lens - 1;i >= 0; i--) {
        Hash[i] = Hash[i + 1] * 131 + s[i] ;
    }
    
    for (int len = 1; len < lens; len++ ) {
        if (h(0, len) == h(lens - len , len)){
            ULL num = h(0, len);
            for (int i = 1;i < lens - 1; i++) {
                if (h(i, len) == num) {
                    ans = s.substr(0,len);
                }else {
                    break;
                }
            }
        }
        else {
            break;
        }
    }
    if (ans == ""){
        cout << "Just a legend";
    }else{
        cout << ans;
    }
    return 0;
}
2023/11/7 19:07
加载中...