【67.24%】【codeforces 745A】Hongcow Learns the Cyclic Shift

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.

Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word “abracadabra” Hongcow will get words “aabracadabr”, “raabracadab” and so on.

Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.

Input
The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters (‘a’–’z’).

Output
Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.

Examples
input
abcd
output
4
input
bbb
output
1
input
yzyz
output
2
Note
For the first sample, the strings Hongcow can generate are “abcd”, “dabc”, “cdab”, and “bcda”.

For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate “bbb”.

For the third sample, the two strings Hongcow can generate are “yzyz” and “zyzy”.

【题目链接】:http://codeforces.com/contest/745/problem/A

【题解】

模拟一下最后一个字符放到第一个位置的情况;
看看组成的字符串有没有重复就好.

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

string s;
map <string,int> dic;
int num = 0;

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    cin >> s;
    int len = s.size();
    num = 1;
    dic[s] = 1;
    rep1(i,1,len)
    {
        string temp;
        temp = s.substr(len-1,1) + s.substr(0,len-1);
        if (!dic[temp])
        {
            dic[temp] = 1;
            num++;
        }
        s = temp;
    }
    cout << num << endl;
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(255)  评论(0编辑  收藏  举报