UVa 455 - Periodic Strings

来源:互联网 发布:淘宝伴侣手机 编辑:程序博客网 时间:2024/05/18 02:43

https://uva.onlinejudge.org/external/4/455.pdf

455 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
A single character string of up to 80 non-blank characters.
Output
An integer denoting the smallest period of the input string.
Sample Input
HoHoHo
Sample Output
2

分析:
找一个串的最小循环节,数据很弱,暴力枚举就可以
注意输出两组数据间要空一行

#include <iostream>#include <sstream>#include <iomanip>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <bitset>#include <string>#include <numeric>#include <algorithm>#include <functional>#include <iterator>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <cctype>#include <complex>#include <ctime>#define INF 0x3f3f3f3f#define eps 1e-6#define p(x) printf("%d\n", x)#define k(x) printf("Case %d: ", ++x)#define mes(x, d) memset(x, d, sizeof(x))#define s(x) scanf("%d", &x)/*int gcd(int a,int b){    return ! b ? a : gcd(b,a % b);}struct data{    int val;    int pos;    int ranks;}p[2005];bool cmp1(const data &a,const data &b){    if(a.val == b.val)        return a.pos < b.pos;    return a.val < b.val;}bool cmp2(const data &a,const data &b){    return a.val > b.val;}bool cmp3(const data &a,const data &b){    return a.pos < b.pos;}*/typedef long long LL;const double pi = acos(-1.0);const long long mod = 1e9 + 7;using namespace std;char s[105];int main(){    //freopen("int.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;    scanf("%d",&T);    while(T--)    {        scanf("%s",s);        int len = strlen(s);        for(int i = 1,j;i <= len;i++)        {            if(len % i == 0)            {                for(j = i;j < len;j++)                    if(s[j] != s[j % i])                        break;                if(j == len)                {                    printf("%d\n",i);                    break;                }            }        }        if(T)            puts("");    }    return 0;}
0 0
原创粉丝点击