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

思路主要就看题就行了:


注意:每行答案之间有一个空行。。。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{int n;cin>>n;while(n--){char a[85];cin>>a;int len=strlen(a);int i,j;for(i=1;i<=len;i++)//i是重复最小的个数,也就是答案,从1开始找{if(len%i!=0)//竟然不能整除呵呵,所以i不可能是最小周期的啊continue;else{for(j=i;j<len;j++)//下标从第0到i-1是第一个周期,那就从下标i开始遍历{if(a[j]!=a[j%i])//竟然不一样,那就不成立break;}if(j==len)//成立!!{printf("%d\n",i);//i就是答案break;//找到最小的了就赶紧结束}}}if(n!=0)printf("\n");//题目要求separated by a line。。。。。真麻烦。。}return 0;
}

Periodic Strings (求HoHoHo字符串最小周期)相关推荐

  1. C++算法9:KMP应用求字符串最小周期

    1.输入重复序列的字符串,如果周期不存在,输出NO,如果周期存在,输出最小周期. 2.代码: # include <iostream> # include <string>us ...

  2. 字符串最小周期串问题

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

  3. UVA455 - Periodic Strings(周期串)

    题目:Periodic Strings A character string is said to have period k if it can be formed by concatenating ...

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

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

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

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

  6. 周期字符串的最小周期

    题目描述 右右喜欢听故事,但是右右的妈妈总是讲一些"从前有座山,山里有座庙,庙里有个老和尚给小和尚讲故事,讲的什么呢?从前有座山--"这样循环的故事来搪塞右右. 我们定义,如果一个 ...

  7. UVA455 Periodic Strings【水题】

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

  8. UVA455 Periodic Strings

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

  9. UVa455 Periodic Strings(kmp)

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

  10. SZUACM集训字符串基础总结: 字符串最小表示 ,KMP, EXKMP, Manracher, Trie树,字符串的hash; 附带一写常见的运用技巧,邝斌大佬的板子和例题[持续更新]

    第一部分 字符串的匹配<-------->KMP 模式匹配:子串的定位运算称为串的模式匹配或串匹配. 假设有两个串S,T,设S为主串,也称正文串,T为子串,也称为模式,在主串S中查找与模式 ...

最新文章

  1. 在typescript中导入第三方类库import报错
  2. ICLR 2020| 最新NAS benchmark:0.1秒完成NAS算法搜索
  3. mysql is fashion ctf_一道CTF题引发的思考-MySQL的几个特性(续)
  4. 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
  5. 代码编写工具_我希望在开始编写代码时就已经知道的工具:已复习
  6. 玩转oracle 11g(49):监听服务设置开机自启,不用一直配置监听
  7. java第六次实验——多线程
  8. win7下ado连接mysql_提示连接无法用于执行此操作_ADODB.Recordset (0x800A0E7D)连接无法用于执行此操作。在此上下文中它可能已被关闭或无效。...
  9. 我知道真相,但我仍然勇往直前
  10. mysql优化---订单查询优化(2):异步分页处理
  11. GitHub 标星 3.2w!史上最全技术人员面试手册!FackBoo发起和总结
  12. 页面三个txt加载联动省市县的代码,类似淘宝的收货地址的布局
  13. sqlite数据库下载安装和初步操作和所遇到的问题
  14. docker学习 主流的pass平台(体验的是设计模式)
  15. ROS机器人程序设计(原书第2版)补充资料 (贰) 第二章 ROS系统架构及概念
  16. C++知识讲解(二)
  17. CGContextRef绘图-iOS球形波浪加载进度控件-HcdProcessView详解 1
  18. order by、distribute by、sort by、group by、partition by
  19. 程序员装机必备利器列表
  20. 谷歌发布 12 款 Chrome 新皮肤

热门文章

  1. 入职5年前端工程师针对web前端小白,作出的职业规划建议
  2. java poodle,SSL 3.0 POODLE攻击信息泄露漏洞(CVE-2014-3566)
  3. Tacotron2 NVIDIA版本优化停顿问题之Biao-Bei数据PhonePrssCrystal
  4. 人民日报+俞敏洪:教育好自己的孩子,是你最重要的事业(两文)
  5. 挑战SQL经典题(一)
  6. 操作系统——进程调度
  7. Excel高手都会的Shift快捷键7个用法,让工作效率翻倍
  8. 无线路由器如何建立ftp服务器,利用无线路由器建立FTP服务器
  9. Widows系统截屏工具
  10. Reading Ingestion —— Paxos Made Simple