题目: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

解法:

从最小周期开始枚举

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;int main()
{int n;cin>>n;while(n--){string str;cin>>str;int result;int len=str.length();for(int i=1;i<=len;i++) //周期{if(len%i!=0) //周期必须能整除字符串长度continue;int j;for(j=i;j<len;j++){if(str[j]!=str[j%i]) //根据周期比较子串break;}if(j==len)   //此周期的子串能循环整个串{printf("%d\n",i);break;}}if(n)printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/YYFFstory/p/5011697.html

UVA455 - Periodic Strings(周期串)相关推荐

  1. G - Periodic Strings (周期串)

    思路:枚举循环节(循环节要能被字符串长度整除),进行循环判断,找到第一组解就是最小解. #include <bits/stdc++.h> #include<stdio.h> u ...

  2. UVA455 Periodic Strings

    问题链接:UVA455 Periodic Strings.入门练习题,用C语言编写程序. 题意:如果一个串可以由长度为k的字符串重复多次连接而得到,则称该串为k周期.本题求一个串的最小周期. 程序中, ...

  3. UVA455 Periodic Strings【水题】

    A character string is said to have period k if it can be formed by concatenating one or more repetit ...

  4. UVa455 Periodic Strings(kmp)

    先计算字符串的前缀函数,字符串长度为n,如果,说明字符串是周期的,否则不是. 代码参考: OJ/Periodic Strings(kmp).java at master · wuli2496/OJ · ...

  5. uva455periodic strings周期串

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93800#problem/D 题意:输入一串长度不超过80的字符串,这个字符串可由一个字符 ...

  6. UVA455 - Periodic Strings

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830& ...

  7. UVA - 455 Periodic Strings【字符串】

    Periodic Strings UVA - 455 题目传送门 题目大意:先输入一个数字n,在输入n行字符串,对每一个字符串输出其最小的周期长度,每两个输出间有一空行. AC代码: #include ...

  8. 周期串java_求最小周期串

    如果一个字符串可以由某个长度为k的字符串重复多次得到,则该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也以6和12为周期).输入一个长度不超过80的串,输出它的最小周期. 样例输入 ...

  9. 1079 -- 周期串

    周期串 Time Limit:1000MS  Memory Limit:65536K Total Submit:119 Accepted:38 Description 如果一个字符串可以由某个长度为k ...

最新文章

  1. 棱形旋转c语言程序_C 语言时隔 5 年重回巅峰,这 20 个热门项目拿去练手!
  2. 互链网-未来世界的连接方式 (1)央行数字货币CBDC模型
  3. 【Linux】一步一步学Linux——ldconfig命令(255)
  4. 大整数乘法c语言数组,很大的数相乘怎么办?
  5. bigdecimal判断等于0_shell 脚本中if判断的条件总结
  6. fianl属性 java_java基础-类的高级属性(包、final、内部类)
  7. 为什么不能线程调用类的成员函数_C++多线程编程之创建线程的几种方法
  8. 计算机图形图像发展前景,计算机图形学发展前景有什么趋势?
  9. 创业失败感悟第十四天
  10. 记错严格管理时间---记一次老师讲的学习方法
  11. d630无电池升级bios_主板电池没电会怎样?手把手教你拆装主板电池
  12. SQL 标准发布第 15 部分:ISO/IEC 9075-15:2019 多维数组(SQL/MDA)
  13. 实时视频流:工作原理
  14. rust+mysql+prep+exec_Rust + Flutter 高性能的跨端尝试
  15. 艾尔塔7号/Elta7
  16. php实现echarts任务关系图,react中使用echarts(人物关系图)
  17. MySQL 分组后取最新记录
  18. python setup.py install与python setup.py develop的区别
  19. Linus 大神的在家办公经验(他也撸猫)
  20. Mssql2k之数据库管理

热门文章

  1. python定时启动代码_python每天定时运行某程序代码
  2. 某8位微型计算机地址码为18位,计算机组成原理复习题.doc
  3. 【CCCC】L2-026 小字辈 (25分),求多叉树的深度和底层叶节点
  4. android 层叠轮播,vue手写一个卡片化层叠轮播(支持滑动,移动端连续滚动,点击)...
  5. Python入门--字符串的判断操作
  6. 两波形相位差的计算值_波形相位频率可调DDS信号发生器(设计分享)
  7. [蓝桥杯]试题 基础练习 FJ的字符串
  8. prim最小生成树算法 邻接表+STL实现
  9. 排序算法的总结与使用题型
  10. bzoj 1413: [ZJOI2009]取石子游戏(博弈+DP)