标签:ring i++ using 算法 name The std ble 一个
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
const int N=400005;
int pi[N];
void Pi(string s){
memset(pi,0,sizeof(pi));
int n=s.length();
pi[0]=0;
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0&&s[i]!=s[j]){
j=pi[j-1];
}
if(s[i]==s[j])
j++;
pi[i]=j;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string s;
while(cin>>s){
set<int> se;
Pi(s);
int n=s.length();
if(n==1){
cout<<1<<endl;
continue;
}
int j=pi[n-2];
while(j>0){
if(s[n-1]==s[j]){
se.insert(j+1);
}
j=pi[j-1];
}
se.insert(n);
if(s[0]==s[n-1])
se.insert(1);
int k=0;
int cnt=(int)se.size();
for(set<int>::iterator it=se.begin();it!=se.end();it++){
if(k++==cnt-1)
cout<<*it<<endl;
else
cout<<*it<<" ";
}
}
return 0;
}
标签:ring i++ using 算法 name The std ble 一个
原文地址:https://www.cnblogs.com/GarrettWale/p/11326449.html