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<bits/stdc++.h>
using namespace std;
char s[];
int main() {
int n;
scanf("%d",&n);
while(n--) {
memset(s,'\0',sizeof(s));
scanf("%s",s);
int len=strlen(s);
//printf("%d\n",len);
for(int i=;i<=len;i++)//假设从0到i为止是周期串
{
if(len%i==)//首先判断到当前i为止还能不能组成周期串
{
int j;
for(j=i;j<len;j++)
{
if(s[j%i]!=s[j])break; //和最前面的周期串比较 ,如果有不相等的,就不是周期串
}
if(j==len)//j到len有两种可能,没有周期串,i到len了,另一种就是找到了周期串
{//并且j到len了
printf("%d\n",i);break;
}
}
}
if(n)printf("\n");
}
return ;
}

uvaoj455Periodic Strings(枚举)的更多相关文章

  1. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  2. CodeForces-Zuhair and Strings(思维+枚举)

    Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string ss has a level xx, if xx is l ...

  3. Hackerrank11 LCS Returns 枚举+LCS

    Given two strings,  a and , b find and print the total number of ways to insert a character at any p ...

  4. Swift----函数 、 闭包 、 枚举 、 类和结构体 、 属性

    1 数组排序 1.1 问题 本案例实现一个整型数组排序的函数,数组排序的规则由传递的规则函数决定. 1.2 方案 首先定义一个整型数组排序函数sortInts,该函数有一个整型数组类型的参数,该参数必 ...

  5. [POJ3096]Surprising Strings

    [POJ3096]Surprising Strings 试题描述 The D-pairs of a string of letters are the ordered pairs of letters ...

  6. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

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

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

  8. POJ3096Surprising Strings(map)

    题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...

  9. 拔高你的Java代码质量吧:推荐使用枚举定义常量(转)

    提高你的Java代码质量吧:推荐使用枚举定义常量 一.分析 常量的声明是每一个项目中不可或缺的,在Java1.5之前,我们只有两种方式的声明:类常量和接口常量.不过,在1.5版之后有了改进,即新增了一 ...

随机推荐

  1. 我的第一个C++程序

    准备抽空学习C++了,不知道自己以后能不能坚持下去,去百度查了一下入门,大多数朋友都是选择用VC++或者VS,而我这里用的是C-Free 5 ,安装包也只有十几兆. 用起来也方便.对于初学者而言够用了 ...

  2. js 模拟百度关键字搜索与跳转

    测试效果: css样式: ul{ display:none; } html代码: <input type="text" id="text" /> & ...

  3. Google Map中的瓦片

    一.墨卡托投影google map使用的是EPSG:900913标准墨卡托投影(等角圆术地图投影)y = R*ln(tan(pi/4 + a/2))x = R*b当y等于piR时,投影图正好为一个正方 ...

  4. JavaScript在浏览器中把文本保存为文件的方法

    JavaScript在浏览器中把文本保存为文件的方法 经过测试第二种方法可以保存更多的文本不至于卡死 var saveTextAsFile1 = function (text, fileName, s ...

  5. 【题解】洛谷P1198 [JSOI2008] 最大数(线段树)

    洛谷P1198:https://www.luogu.org/problemnew/show/P1198 思路 一道水水的线段树 20分钟A掉 这道题只涉及到单点修改和区间查询 所以这道题甚至不用Laz ...

  6. Nlog日志出坑合集

    .net core框架下nlog不记录: 1.安装NLog.Web.AspNetCore 2.在Startup.cs文件的方法public void Configure(IApplicationBui ...

  7. Oracle中table数据数据类型

    function F_ReturnDescription(varID in varchar2) return varchar2 is numDataCount ); mytable ly_family ...

  8. C# Console类的方法使用总结

    Console类表示控制台应用程序的标准输入流.输出流和错误流. 此类不能被继承,而在Java中,类似的功能则由System.in和System.out来实现了. 一 输出到控制台 输出到控制台就是把 ...

  9. Zabbix——解决中文显示乱码

    前提条件: 准备好上传工具,我用的是WinSCP 使用字体是微软雅黑,如果使用其他的更改名称即可. 更改Zabbix服务器的默认参数: vi /usr/share/zabbix/include/def ...

  10. python3中sys.argv[]小记

    1.python3中sys.argv[]用于传递程序外部的参数,外部一般指命令行输入的参数,argv[]所传递的参数实质上是一个列表,其第一个元素为程序本身. 2. sys.argv[] #传入的参数 ...