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

UVa455 最小周期串问题的更多相关文章

  1. E - Power Strings,求最小周期串

    E - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  2. 周期串(Periodic Strings,UVa455)

    解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC  A ...

  3. 问题 K: 周期串plus

    问题 K: 周期串plus 时间限制: 1 Sec  内存限制: 128 MB提交: 682  解决: 237[提交] [状态] [命题人:外部导入] 题目描述 如果一个字符串可以由某个长度为k的字符 ...

  4. 【KMP求最小周期】POJ2406-Power Strings

    [题意] 给出一个字符串,求出最小周期. [思路] 对KMP的next数组的理解与运用orz ①证明:如果最小周期不等于它本身,则前缀和后缀必定有交叉. 如果没有交叉,以当前的next[n]为最小周期 ...

  5. 周期串(JAVA语言)

    package 第三章习题; /*  * 如果一个字符可以由某个长度为k的字符串重复多次得到,则称该串以k为周期.  * 例如:abcabcabcabc 以3为周期(注意:它也以6和12为周期)  * ...

  6. 【C/C++】习题3-4 周期串/算法竞赛入门经典/数组和字符串

    [题目] 如果某个字符串可以由长度为k的字符串重复多次得到,则称该串以k为周期. 输入一个长度不超过80的字符串,输出最小周期. [思路] 暴力求解.依次考察周期1~长度n. 筛选:周期一定是长度n的 ...

  7. HDU 1358 (所有前缀中的周期串) Period

    题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出 ...

  8. 【周期串】NYOJ-1121 周期串

    [题目链接:NYOJ-1121] 例如:abcabcabc 该字符串的长度为9,那么周期串的长度len只可能为{1,3,9},否则就不可能构成周期串. 接下来,就是要在各周期间进行比较.描述不清... ...

  9. 【XSY2779】最小表示串 KMP DP polya定理

    题目描述 给你一个字符串\(s\),问你有多少个串是最小表示串且字典序\(\leq s\) \(|s|\leq 1000\) 题解 先把\(s\)变成比\(s\)小的最大的最小表示串.方法是从后枚举每 ...

随机推荐

  1. GIT安装包备用地址

    如果官网下载被禁止,可在下面这个地址下载,速度飞快 http://www.wmzhe.com/soft-38801.html#download

  2. JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框

    1.this的使用 this js中的关键字 js内部已经定义好了,可以不声明 直接使用 this的指向问题 1. 在函数外部使用 this指向的是window 2. 在函数内部使用 有名函数 直接调 ...

  3. CSS-美化checkbox

    注意:css3 的用: checked 伪类选择器会去检查元素属性(`input[checked]`),而不是 dom 节点上的属性( ``).所以要使用 jquery 的 prop 而非 attr ...

  4. 测开之路五十一:代码实现MongoDB增删改查

    初始化时连接.析构时断开连接 from pymongo import MongoClient class Mogo(object): def __init__(self, host='127.0.0. ...

  5. linux svn 服务器搭建问题

    我的svn版本 svn, version 1.7.14 (r1542130) compiled Nov 20 2015, 19:25:09 Copyright (C) 2013 The Apache ...

  6. 【Web API]无法添加AttributeRoutes的解决方案

    1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttribut ...

  7. 关于Ext4 extraParams 不能传递动态参数的问题解决办法

    可以监听请求发送之前的事件:beforeload ,然后再添加请求的参数 me.store = Ext.create('Ext.data.JsonStore', { remoteSort: true, ...

  8. Windows Server 2008 R2 官方简体中文免费企业版/标准版/数据中心版

    Windows Server 2008 R2是一款微软发布的Windows服务器操作系统,和之前发布的Windows Server 2008相比功能更为完善运行更为稳定,提升了系统管理弹性.虚拟化.网 ...

  9. mysql自带压测工具--mysqlslap

    mysqlslap  压测 mysql 5.7.20 目前版本,打印不出内存.cpu使用信息 mysqlslap -h192.168.9.60 -P4406 -uroot -p --create-sc ...

  10. C++中的抽象类和接口

    1,在 C++ 语言中,并不直接支持面向对象中的抽象类和接口概念,但是 C++ 语言 却可以间接实现这些概念: 2,什么是抽象类: 1,面向对象中的抽象(其实就是分类)概念: 1,在进行面向对象分析时 ...