UVa455

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
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

题意:

给出一个字符串,找出它的最大循环节字符串长度。

输入:

情况数T,然后是一个空行,之后每一种情况给一个字符串,相邻两情况间有空行。

输出:

每种情况输出它的最大循环节字符串长度。

分析:

简单模拟,编写一个函数检验输入的字符串是否是某个长度的字符串的重复。然后列举输入字符串长度的所有因子,检查字符串是否是为某个因子长度的字符串的重复即可。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 const int MAX_LEN = 80;
 7 char str[90];
 8 bool IS_THIS_STRING_LOOPING_IN_THIS_NUMBER(char str[],int loop_num,int len){
 9     for(int i = 0 ; i < loop_num ; i++)
10         for(int j = i + loop_num ; j < len ; j += loop_num)
11             if(str[i] != str[j]) return false;
12     return true;
13 }
14 int main(){
15     int T; cin >> T;
16     while(T--){
17         int ans;
18         getchar();
19         scanf("%s",str);
20         int len = strlen(str);
21         for(int i = 1 ; i <= len ; i++){
22             if(len % i == 0){
23                 if(IS_THIS_STRING_LOOPING_IN_THIS_NUMBER(str,i,len)){
24                     ans = i;
25                     break;
26                 }
27             }
28         }
29         printf("%d\n",ans);
30         if(T != 0) cout << endl;
31     }
32     return 0;
33 }

时间: 2024-10-10 05:46:28

UVa455的相关文章

周期字符串[UVA-455]

Periodic Strings  UVA - 455 https://vjudge.net/problem/UVA-455 书上第三章的习题3-4.题目要求判断一个给定的串的最小周期,题目保证了串的长度不大于80,因此使用朴素的暴力穷举法就可以解决.根据题意,第一个周期必然从第一个字符开始,因此只要用一个指针,从前往后扫,那么这个指针将整个串分割为两个字串,只要验证第一个字串是不是满足周期性即可.验证周期性可以依次扫描第二个字串,判断对应字符是否相等即可.我这里的对应使用了求余的方法.C++实

周期串(Periodic Strings,UVa455)

解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC  ABCABC (345678)->%其字串长度3 012  3%3 4%3 5%3  6%3 7%3  8%3   0      1     2        0      1       2 if(a[j]!=a[j%3])说明不对应,不是周期,进行下一位扫描. AC Code: #include<

习题3-4 周期串 UVa455

Description 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 s

UVa455 Periodic Strings

#include <stdio.h>#include <string.h> int main(){    int T, k, len;    char str[81], *p, *q, *end;    scanf("%d", &T);    while (T--)    {        scanf("%s", str);        len = strlen(str);        end = str+len;        

D - Periodic Strings(UVA-455)

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 "

算法竞赛入门经典题解——第三章 3-4 周期串UVa455

思路:遍历可能的周期,比较s[k]与s[k%i](其中i为周期) #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int T; char s[90]; scanf("%d",&T); while(T--){ scanf("%s",s); int len,i; len=strlen(s); for(i=1;i<=len;i++

UVA-455 利用c++ string快速解题

int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 ); 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己 #include<iostream> #include<string> using namespace std; int main(){ int n

UVa455 周期串

题目描述:给一个字符串,找它的重复周期 思路: 假设周期从1~N,依次看是否能成为该字符串的重复周期.需要注意: 1.若周期串没有周期,其重复周期就是本身长度N 2.假设的周期k如果不能被N整除,自然就不是周期,不必考虑 3.N长的字符串等分成k长的子部,找每一个k长子部对应位置的字符是否相等即可 代码: 1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int N; 6 char s[100]; 7 sca

(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--)