S[0…n-1]是一个长度为n的字符串,定义旋转函数Left(S)=S[1…n-1]+S[0].比如S=”abcd”,Left(S)=”bcda”.一个串是对串当且仅当这个串长度为偶数,前半段和后半段一样。比如”abcabc”是对串,”aabbcc”则不是。
现在问题是给定一个字符串,判断他是否可以由一个对串旋转任意次得到。
Input
第1行:给出一个字符串(字符串非空串,只包含小写字母,长度不超过1000000)
Output
对于每个测试用例,输出结果占一行,如果能,输出YES,否则输出NO。
Input示例
aa
ab
Output示例
YES
NO
一个对串经过题述定义的旋转之后得到的串仍为对串(可自行验证),所以只需要判断输入的字符串是否为对串即可。
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
string str;
cin>>str;
string ans1,ans2;
int len=str.size();
if(len%2)
cout<<"NO"<<endl;
else
{
for(int i=0;i<len/2;i++)
{
ans1+=str[i];
ans2+=str[i+len/2];
}
if(ans1==ans2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}