求最小循环节长度UVA - 455
2018-02-06 18:09
302 查看
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.
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.
separated by a blank line.
HoHoHo
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 yourprogram 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 areseparated by a blank line.
Sample Input
1HoHoHo
Sample Output
2思路: 暴力枚举循环节长度,循环问题用求模运算
//#define LOCAL/*注意提交时将此行代码注释掉*/ //#include<windows.h> #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int fun(char *str) { int k,len=strlen(str); for(int i=1;i<=strlen(str);i++) {//i是循序节长度 int flag=1; if(len%i==0) { for(k=i;k<len;k++) { if(str[k]!=str[k%i])//循环用求模运算 使得后面的字符与最初的循环体进行比较 { flag=0; break; } } if(flag) return i; } } return len; } int main() { //读写文件代码写在这里不会影响计时函数 #ifdef LOCAL freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout ); #endif // LOCAL /*计时函数*/ #ifdef LOCAL DWORD start_time, end_time; start_time = GetTickCount(); #endif // LOCAL /*main函数主体*/ int N; int flag=0; cin >> N; //getchar(); char str[100]; while(N--) { cin >> str; cout << fun(str) << endl; if(N) cout<<endl; } #ifdef LOCAL end_time = GetTickCount(); //提交时将时间函数输出注释掉 cout << "Running time is: " << static_cast<double>( end_time - start_time ) * 1.0 / 1000 << "ms" << endl; //输出运行时间 #endif // LOCAL return 0; }
相关文章推荐
- poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
- UVa 202 (分数最小循环节)
- UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
- UVa455 Periodic Strings 最小周期串
- UVA 455 Periodic Strings(字符串的循环节)
- HDU 1358 Period 求前缀长度和最小循环节长度
- nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】
- HDU1358-Period(kmp-求前缀长度和最小循环节长度 )
- Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
- UVA 455(最小周期)
- UVa 455 Periodic Strings【最小循环子串】
- UVA10003固定点切木棍,怎么切使得每次切之前的木棍长度总和最小
- UVALive 3026 Period ( kmp 求前缀最小循环节)
- 【UVa 10003】【区间DP】Cutting Sticks【有一个长为L的木棍,木棍中间有n个切点。每次切割的费用为当前木棍的长度。求切割木棍的最小费用。】
- hdu 3746 kmp求最小循环节长度
- HDU 3746 Cyclic Nacklace KMP求字符串最小循环节长度
- UVa 455 最小重复字符串
- UVA 455 Periodic Strings (最短周期串长度)
- UVa 1625 颜色的长度
- 最小树形图模板 UVa 11865