UVa 455 - Periodic Strings 解题报告

1.题目大意

求一个长度不超过80的字符串的最小周期.

2.思路

非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑.

3.应该注意的地方

(1) 最后输出的方式要注意,不然很容易就PE了.不过个人认为,其实这题Sample Output给的不好

(2) 注意输出的要求是最小周期

4.代码

#include"stdio.h"
#include"string.h"
#define maxn 80

int main()
{
    int T,m,i,j,flag;
    char s[maxn];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        m=strlen(s);
        for(i=1; i<m+1; i++)
        {
            flag=1;
            for(j=0; j<m; j++)
                if(s[j]!=s[(j+i)%m])
                {
                    flag=0;
                    break;
                }
            if(flag)
            {
                printf("%d\n",i);
                break;
            }
        }
        if(T) printf("\n");
    }
    return 0;
}

  

参考书目:算法竞赛入门经典(第2版) 刘汝佳 编著

时间: 2024-10-08 10:29:07

UVa 455 - Periodic Strings 解题报告的相关文章

UVa 455 Periodic Strings (周期串)

Periodic Strings Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Periodic Strings  A character string is said to have period k if it can be formed by concatenating one or more repetitions of anothe

UVa 455 Periodic Strings

题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 char s[105]; 8 9 int main() 10 { 11 int ncase,t,i,k,j,len; 12 scanf("%d"

UVa OJ 455 Periodic Strings

 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 o

(UVA)455 --Periodic Strings(周期串)

题目链接:http://vjudge.net/problem/UVA-455 可以从1开始枚举周期,对后面的字符逐个测试. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int t; 9 char s[100]; 10 scanf("%d",&t); 11 while(t--)

POJ2406----Power Strings解题报告

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 43514   Accepted: 18153 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

uva 489.Hangman Judge 解题报告

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=430 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 using namespace std; 6 7 con

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charact

【LeetCode】Multiply Strings 解题报告

[题目] Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. [解析] 题意:两个字符串表示的非负整数相乘,用字符串的形式返回积. 思路:逐位相乘. 关键:中间结果如何保存?如果用字符串保存中间结果,频繁该值不太方便,所以还是用整数数组保