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<cstring>
#include <iomanip>
#include<string>
using namespace std;
int main(http://www.my516.com)
{
char a[81];
int n;
cin >> n;
while (n--)
{
cin >> a;
int t = 1;
while (true)
{
int c = 0;
for (int i = 0; i < strlen(a); i++)
{
if (a[i] == a[(i + t) % strlen(a)]) c++;
}
if (c == strlen(a)) break;
++t;
}
cout << t << endl;
if (n == 0) cout << endl;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
学习笔记:环形串问题可看成一个圆,只要圆转动几次后可回到原位,即最小周期,所以只需要看s[i]==s[(i+t)%len]。
---------------------

最新文章

  1. ios学习-控制按钮图片的走动和变换
  2. BookRent借阅管理
  3. NPOI读取Excel 数据 转。。。
  4. Qt 按钮事件不响应
  5. 怎样将Sqlserver数据库转成mysql数据库
  6. BEvent_标准控件Event的用法(案例)(待整理)
  7. HTTP长连接200万尝试及调优
  8. eclipse 工程加入ant以支持自动打war包
  9. BeanUtils框架浅析
  10. [leetcode-581-Shortest Unsorted Continuous Subarray]
  11. 【Java IO流】RandomAccessFile类的使用
  12. SVG视野
  13. RandomAccess接口的使用
  14. [Swift]LeetCode848. 字母移位 | Shifting Letters
  15. 安装 Tensorflow
  16. react 会员登录
  17. ssh 配置免密失败
  18. I/O复习
  19. soj1036. Crypto Columns
  20. ie浏览器报 promise 问题

热门文章

  1. JQUERY的$(function(){})和window.onload=function(){}的区别【转】
  2. Borůvka (Sollin) 算法求 MST 最小生成树
  3. 建站手册-浏览器信息:挪威的 Opera 浏览器
  4. day106 支付功能与优惠券功能 contentype
  5. python 装饰器 第二步:扩展函数的功能(不修改原函数)
  6. Dubbo 系列(07-5)集群容错 - Mock
  7. Codeforces Breaking Good
  8. Linux系统安全
  9. alpha阶段绩效考核
  10. getopts举例