51nod 1347 旋转字符串(思维好题)

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
收藏
关注
取消关注

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

这题想到了很简单,想不到的话,嗯,慢慢想。

一个对串旋转之后仍然是对串,一个不是对串的字符串无论怎么旋转都不会是对串。

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 char str[1000005];
 5 int main()
 6 {
 7     ios::sync_with_stdio(false);
 8     while(cin>>str)
 9     {
10         int flag=0;
11         int len=strlen(str);
12         if(len%2!=0)
13         {
14             cout<<"NO"<<endl;
15             continue;
16         }
17         for(int i=0;i<len/2;i++)
18         {
19             if(str[i]!=str[i+len/2])
20             {
21                 cout<<"NO"<<endl;
22                 flag=1;
23                 break;
24             }
25         }
26         if(!flag)
27             cout<<"YES"<<endl;
28     }
29     return 0;
30 }
对串是天赋技能

 

 
posted @ 2017-07-30 16:18  只有你  阅读(457)  评论(0编辑  收藏  举报