UVa 455 周期串

原创 2015年07月07日 21:51:39
 Periodic Strings 

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

就是求字符串的最小周期,注意格式就好。
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    string s;
    int n;
    scanf("%d",&n);
    while(n--)
    {
        getchar();
        cin>>s;
        string ss=s;
        int i,j,k=1;
        for( i=1;i<s.size();i++)
        {
           string s1=s.substr(0,i);

           int flag=1;
           for( j=i;j<s.size();j+=i)
           {
               string s2=s.substr(j,i);
               if(s1!=s2){flag=0;break;}
           }
           if(flag){cout<<i<<endl;k=0;break;}
        }
        if(k)cout<<s.size()<<endl;
        if(n!=0)cout<<endl;
    }
    return 0;
}






            
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

周期串(Periodic Strings,UVa455)

最近在学习开始学习算法,现在看的是刘汝佳的《算法竞赛入门》,感觉还是有必要把一些有问题题目记录下来。另外其他的代码都同步到了gitbub上的仓库了,可能不是最好的,不过都被在uva上ac了。欢迎感兴趣...

周期串Uva455 紫书P57 3-4

A character string is said to have period k if it can be formed by concatenating one or more repetit...

UVA 455 - Periodic Strings

A character string is said to have period k if it can be formed by concatenating one or more repetit...

uva 455 Periodic Strings

uva 455 Periodic Strings

uva455 Periodic Strings & hdu3746 Cyclic Nacklace

两道题类似,uva455是求最小周期,hdu3746是求最少插入多少字符可以使得字符串最少有两个周期。 类似于kmp算法,使用一个next数组, 对字符串进行处理,标记所有的前缀, 时间复杂度是O(n...

UVa 1584 Circular Sequence / 455 Periodic Strings(枚举字符串)

原题地址https://vjudge.net/problem/UVA-1584 题意:DNA的四个字母CGAT排列成环形,可以移动起点,输出字典序最小的环形序列(顺时针)。 解题思路这道题是《算法...

UVa 455 Periodic Strings (字符串处理)

455 - Periodic Strings Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlin...

【UVa】[455]Periodic Strings

我已经感受到了 紫书习题满满的恶意

UVa-455 - Periodic Strings

A character string is said to have period k if it can be formed by concatenating one or more repetit...

UVA455 Periodic Strings

Description A character string is said to have period k if it can be formed by concatenating one ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)