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#读取信息备份

    class Program { static void Main(string[] args) { var wc = new WebClient(); var html = wc.DownloadSt ...

  2. 微信小程序开发工具快捷键

    格式调整 //保存文件 Ctrl+S //代码行缩进 Ctrl+[, Ctrl+] //折叠打开代码块 Ctrl+Shift+[, Ctrl+Shift+] //复制粘贴,如果没有选中任何文字则复制粘 ...

  3. 使用GraphViz画caffe网络结构图

    参考http://blog.csdn.net/happynear/article/details/45440709 1. 安装pydot: sudo pip install pydot 2. 安装Gr ...

  4. centos中java安装跟配置

    安装配置java环境 [root@JxateiLinux src]# Wget  http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c ...

  5. appium入门知识

    UI自动化相关工具 1.appium平台 2.Apple的UIAutomation 3.Google的UIAutomation 简单的介绍一下这几个工具: 1.Appium简介 Appium使用Web ...

  6. C#强大的编程功能

    下面列出一些C#重要的功能 1.布尔条件 2.自动垃圾回收 3.标准库 4.组件版本 5.属性和事件 6.委托和事件管理 7.易于使用的泛型 8.索引器 9.条件编译 10.简单的多线程 11.LIN ...

  7. 使你的ActiveX控件执行时不弹出安全性提示(转载)

    我们编写一个ActiveX控件在IE中运行,一般会弹出一个安全提示,如何避免这种情况?下面是我在参考前人的文章后,总结出“在浏览器中执行时不弹出警告的ActiveX控件”的两种编写方法,予以备忘.注意 ...

  8. Oracle 备份恢复实例

    Oracle 备份恢复实例:三思笔记 1 shutdown abort 系统归档模式,有备份 create table xx as select * from emp; update xx set e ...

  9. Drbd双机环境安装配置

    一.环境准备 1) 操作系统:ubuntu-14.04.1 x64 2) Ubuntu1 192.168.5.179 /dev/sdb1 主节点 Ubuntu2 192.168.5.178 /dev/ ...

  10. 463. Island Perimeter (5月29日)

    解答 class Solution { public: int islandPerimeter(vector<vector<int>>& grid) { int num ...