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;
    }
  • 相关阅读:
    使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法
    通过javac导出Jar包
    JAVA中JDBC连接Mysql数据库简单测试
    使用Swashbuckle.AspNetCore生成.NetCore WEBAPI的接口文档
    CentOS7中使用yum安装Nginx的方法
    Dockerfile 指令 ADD 和 COPY介绍
    Azure Web应用中设置静态虚拟目录的方法(比如部署Django需要用到)
    pip常用命令(转载)
    android发布新版忘记keystore(jks)密码终极解决方案
    IntelliJ Idea 常用快捷键列表
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626803.html
Copyright © 2011-2022 走看看