循环子串

#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;
}

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页