• 【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;
    }
  • 相关阅读:
    used内存较大,实际top查看系统进程中并没有占用这么多内存
    查看LINUX进程内存占用情况
    关于ConcurrentHashMap的key和value不能为null的深层次原因
    Linux修改用户所在组方法
    原因可能是托管的PInvoke签名与非托管的目标签名不匹配
    vs2019 实现C#调用c++的dll两种方法
    java jvm 参数 -Xms -Xmx -Xmn -Xss 调优总结
    java 读取文件的几种方式和通过url获取文件
    Idea中Maven的默认配置 (非常好)
    去哪儿网models数据更新
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626803.html
Copyright © 2020-2023  润新知