zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    使用SpringAOP获取一次请求流经方法的调用次数和调用耗时
    疫苗之殇与理性应对之道
    【做更好的职场人】理性、弹性、开放的沟通
    使用IntelljIDEA生成接口的类继承图及装饰器模式
    订单导出应对大流量订单导出时的设计问题
    预发和线上的自动化对比工具微框架
    从实战角度看如何构建高质量的软件:一线工程师的一份质量手记
    代码问题及对策
    若干设计经验教训小记
    输入输出无依赖型函数的GroovySpock单测模板的自动生成工具(上)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626803.html
Copyright © 2011-2022 走看看