【UVa】455 - Periodic Strings

来源:互联网 发布:u盘怎么恢复数据 编辑:程序博客网 时间:2024/06/15 12:28

Problem here
A character string is said to have period k if it can be formed by concatenating one or more repetitions
of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed
by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one
repetition of ”abcabcabcabc”).
Write a program to read a character string and determine its smallest period.

Input

The first line oif the input file will contain a single integer N indicating how many test case that your
program will test followed by a blank line. Each test case will contain a single character string of up
to 80 non-blank characters. Two consecutive input will separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. Two consecutive output are
separated by a blank line.

Sample Input

1
HoHoHo

Sample Output

2

Solution

#include <iostream>#include <string>using namespace std;int main(){    int kase;    cin >> kase;    while(kase--){        string line;        getline(cin, line);        bool pass = false;        string input;        cin >> input;        for(int i = 1; i <= input.size()-1; i++){            if(input.size() % i == 0){                int j;                for(j = i; j < input.size(); j++){                    if(input[j] != input[j%i])                        break;                }                if(j == input.size()){                    cout << i << endl;                    pass = true;                    break;                }            }        }        if(!pass)            cout << input.size() << endl;        if(kase)            cout << endl;    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 北京65岁老年证怎么办 怎么办65岁免费乘车卡 5个月小孩晚上哭怎么办 婴儿要含奶头睡怎么办 我把输入法删了怎么办 头发干枯毛躁怎么办用什么洗发水 落枕后脖子歪了怎么办 睡觉睡得脖子疼怎么办? 婴儿脖子睡歪了怎么办 怀孕8个月肚皮痒怎么办 怀孕大阴唇肿了怎么办 孕妇肚皮撑的疼怎么办 我大阴唇特别长怎么办 切完洋葱辣眼睛怎么办 下昆山车没刷卡怎么办 高德地图用不了怎么办 玩游戏老是闪退怎么办 苹果平板电脑闪退怎么办 钉钉忘记打卡了怎么办 钉钉手机号换了怎么办 玖富叮当贷逾期怎么办 玖富超能怎么办现金贷 包包的油边开裂怎么办 lv包包油边开裂怎么办 lv钱包油边开裂怎么办 德运奶粉没勺子怎么办 音响坏了没声音怎么办 6s蓝牙无法配对怎么办 魅族耳机进水了怎么办 苹果6耳机进水了怎么办 耳机内部线坏了怎么办 酷狗m1开不开机怎么办 小米2s蓝牙坏了怎么办 苹果手机蓝牙坏了怎么办 老婆不精明傻笨怎么办 老婆管得太严怎么办 骂自己父母的人怎么办 5岁宝宝说谎话怎么办 孩子老说谎话该怎么办 喜欢的人不理我怎么办 君生我未生怎么办