AcWing 158. 项链
原题链接
简单
作者:
BTT
,
2021-05-04 21:11:57
,
所有人可见
,
阅读 765
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int n;
int fi(string s){
int i=0,j=1;
while(i<=n&&j<=n){
int k=0;
while(k<n&&s[i+k]==s[j+k])k++;
if(k==n)break;
if(s[i+k]>s[j+k])i+=k+1;
else j+=k+1;
if(i==j)i++;
}
int p=min(i,j);
return p;
}
int main(){
cin>>s1>>s2;
n=s1.length();
s1+=s1;
s2+=s2;
int pos1=fi(s1);
int pos2=fi(s2);
// cout<<pos1<<endl<<pos2<<endl;
string s3,s4;
for(int i=pos1;i<pos1+n;i++){
s3+=s1[i];
}
for(int i=pos2;i<pos2+n;i++){
s4+=s2[i];
}
if(s3==s4){
cout<<"Yes"<<endl;
for(int i=pos1;i<pos1+n;i++){
cout<<s1[i];
}
}
else cout<<"No";
}