UVa OJ 455

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

寻找周期字符串的最小周期。从小到大枚举各个周期,一旦符合条件就输出。注意每一个测试用例的输出结果之间空一行,最后一个用例的输出后面无空行。

此题个人认为最关键在于对题意的把握,即例如ABCDEF这样的串最后的结果是6而不是0,把握了这一点,多考虑一下即可AC

/**
* UVa 455 Periodic Strings
* Author: Sleigh
* Last Modified: 2016.03.23
*/
#include <stdio.h>
#include <string.h>
int main()
{
int T,len,i,temp,first=1;//T代表测试块的个数,len是字符串长度,temp存储可能的周期值
char str[90]={0};//存储字符串
scanf("%d",&T);
while(T--){
scanf("%s",str);
temp=len=strlen(str);//针对ABC这样的情况
for(i=1;i<len;i++){
if(str[i]==str[0]){
temp=i;
for(int k=0;k<temp;k++,i++){
if(str[k]!=str[i]){//反向思维,针对ABABACC的情况
temp=len;
i--;
break;
}
if(k!=temp-1&&i==len-1){//主要针对ABCDAB的情况
temp=len;
break;
}
if((k==temp-1)&&(i!=len-1))
k=-1;//始终注意k的自加
}
}
}
if(first){
printf("%d\n",temp);
first=0;
}
else
printf("\n%d\n",temp);
}
return 0;
}

UVa 455 - Periodic Strings解题报告的更多相关文章

  1. UVA.455 Periodic Strings(字符串的最小周期)

    Periodic Strings 模板 题意分析 判断字符串的最小周期 代码总览 /* Title:UVA.455 Author:pengwill Date:2016-12-16 */ #includ ...

  2. UVa 455 - Periodic Strings - ( C++ ) - 解题报告

    1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...

  3. UVa 455 Periodic Strings

    题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...

  4. UVa OJ 455 Periodic Strings

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

  5. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  6. 【LeetCode】555. Split Concatenated Strings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  7. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  8. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  9. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. JAVA之类的动手动脑

    1.默认构造方法与自定义的构造方法的冲突 package com.xu; class fool { int value; fool(int nowvalue) { value=nowvalue; } ...

  2. 《深入理解Java虚拟机》-----第12章 Java内存模型与线程

    概述 多任务处理在现代计算机操作系统中几乎已是一项必备的功能了.在许多情况下,让计算机同时去做几件事情,不仅是因为计算机的运算能力强大了,还有一个很重要的原因是计算机的运算速度与它的存储和通信子系统速 ...

  3. sublime text插件emmet自定义模板

    首先要找到 snippets.json这个文件,路径是preferences>browse packages,看看有没有emmet目录. 如果没有,可能是您没有安装emmet插件,或者您安装了但 ...

  4. opencv::霍夫变换-直线

    霍夫直线变换介绍 Hough Line Transform用来做直线检测 前提条件 – 边缘检测已经完成 平面空间到极坐标空间转换 对于任意一条直线上的所有点来说,变换到极坐标中,从[0~360]空间 ...

  5. redis应用场景总结redis平时我们用到的地方蛮多的,下面就了解的应用场景做个总结:

    redis常见应用场景 1.热点数据的缓存 由于redis访问速度块.支持的数据类型比较丰富,所以redis很适合用来存储热点数据,另外结合expire,我们可以设置过期时间然后再进行缓存更新操作,这 ...

  6. Linux必备工具与软件包

    yum -y update(所有都升级和改变) 升级所有包,系统版本和内核,改变软件设置和系统设置 ----------------------------------------------- yu ...

  7. Joomla3.4.6 RCE漏洞深度分析

    笔者<Qftm>原文发布:https://www.freebuf.com/vuls/216512.html *严正声明:本文仅限于技术讨论与分享,严禁用于非法途径 0×00 背景 10月9 ...

  8. SpringBoot学习(二)探究Springboot启动机制

    引言: SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起探究一下SpringBoot的启动原 ...

  9. java编写基于netty的RPC框架

    一 简单概念 RPC:(Remote Procedure Call),远程调用过程,是通过网络调用远程计算机的进程中某个方法,从而获取到想要的数据,过程如同调用本地的方法一样. 阻塞IO:当阻塞I/O ...

  10. 使用C++代码打印数字正方形

    使用C++代码打印数字正方形 作为一名初学者,最近在跟着网课学习C++程序设计基础.在学习过程中遇到了一些习题,我根据自己的理解和思路写了一些代码实现,算是对自己学习过程的一个记录,也希望可以对别人有 ...