循环子串

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 100000001;
// ll Next[N];
unordered_map<ll, ll> Next;
string str;
void getnext(){
    ll i, j;
    i = 0;
    j = -1;
    Next[0] = -1;
    while(i < str.size()){
        if(j == -1 || str[i] == str[j]) Next[++i] = ++j;
        else j = Next[j];
    }
}
int main(){
    cin>>str;
    getnext();
    ll n = str.size() - Next[str.size()];
    if(str.size() % n == 0)
        cout<<str.substr(0, n)<<endl;
    else{
        cout<<str<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/108699433