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. synchronized块中的wait()、nofity()、nofityAll()方法

    前言 在Java并发编程实战,会经常遇到多个线程访问同一个资源的情况,这个时候就需要维护数据的一致性,否则会出现各种数据错误,其中一种同步方式就是利用Synchronized关键字执行锁机制,锁机制是 ...

  2. 支撑微博亿级社交平台,小白也能玩转Redis集群(实战篇)

    上篇文章<支撑微博亿级社交平台,小白也能玩转Redis集群(原理篇)>介绍了Redis集群相关原理,这篇文章将介绍Redis Cluster集群的搭建.配置,运维.扩容等具体操作 集群搭建 ...

  3. Java自动化测试框架-03 - TestNG之Test Group篇 - 我们一起组团打怪升级(详细教程)

    简介 其实这篇文章的group宏哥在上一篇中就提到过,但是就是举例一笔带过的,因此今天专门有一篇文章来讲解Group的相关知识.希望大家茅塞顿开 ,有着更进一步认识和了解测试组. 一.Test Gro ...

  4. SVN工具常用功能总结

    使用SVN作为版本管理工具,可以使用VisualSVN Server+TortoiseSVN搭建SVN版本控制系统,组长安装VisualSVN Server,组员安装TortoiseSVN. Tort ...

  5. LWIP移植文件介绍

    在介绍文件之前首先介绍一下DMA描述符 stm32以太网模块接收/发送FIFO和内存之间的以太网传输是通过以太网DMA使用DMA描述符完成的,一共有两个描述符列表:一个用于接收,一个用于发送, 两个列 ...

  6. Python编程系列---使用装饰器传参+字典实现动态路由

    # 实现一个空路由表,利用装饰器将url和功能函数的对应关系自动存到这个字典中 router_dict = {} # 定义一个装饰器 # 再给一层函数定义,用来传入一个参数,这个参数就是访问的页面地址 ...

  7. Etcd安装和使用

    Etcd安装和使用 一.安装 1.1 二进制安装 从这里下载: etcd-v3.2.11-linux-amd64.tar.gz 下载包后解压即可运行: # 解压 tar zxvf etcd-v3.2. ...

  8. Java根据参数返回相应类

    问题初衷:如何根据参数变换方法的返回类型(参数为 类) 解决方案: 下面方法是放在工具类(例:YslRequestUtil) public <T> T response(Object re ...

  9. SpringBoot2.X整合Actuator

    一 说明 Actuator 的定义 actuator 是一个制造术语,指的是用于移动或控制某物的机械装置.执行器可以通过一个小的变化产生大量的运动. 要将 actuator 添加到基于 Maven 的 ...

  10. 关于./xhost: unable to open display问题的解决

    看了很多大同小异的帖子,都没能解决这个问题,以下是我的实测经验,注意第三步,很关键. 注:以下操作在确保vncserver.xdpyinfo服务正常的情况下进行 第一步:root登录,启动vncser ...