周期串and Hongcow Learns the Cyclic Shift

来源:互联网 发布:js 判断对象未定义 编辑:程序博客网 时间:2024/06/03 21:18

周期串

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述

一个字符串可以由长度为k的字符串重复多次得到,我们说该串以k为周期,例如abcabcabcabc  以3为最小周期(注意它也可以以6和12为周期)

我们输入一个不超过200的串,输出它的最小周期(字符串区分大小写)

输入
有多组测试数据,每组输入一个字符串
输出
输出相应字符串的最小周期
样例输入
abcabcabcabcabcd
样例输出
34
来源
入门经典
上传者
TC_李苗

 



直接给代码,输出中间部分就懂什么意思了(有注释)

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;char s[200];int main(){    while(~scanf("%s",s))    {        int len=strlen(s);        for(int i=1; i<=len; i++)        {            if(len%i==0)            {                int ok=1;                for(int j=i; j<len; j++)                {                    if(s[j]!=s[j%i])                    {                        ok=0;                        break;                    }                    //printf("s[%d]:%c  s[%d]:%c  s[%d:%d]:%c\n",j,s[j],i,s[i],j,i,s[j%i]);                    //输出中间部分;                }                if(ok)                {                    printf("%d\n",i);                    break;                }            }        }    }}

再给一个题,

A. Hongcow Learns the Cyclic Shift
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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".



代码直接不用改,,,,直接过。。。。。


0 0
原创粉丝点击