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. python编程基础之三十四

    面向对象:三大特征:封装,继承,多态 封装:隐藏对象的实现过程,对外仅仅公开接口,控制在程序中的读取和修改的访问级别 类,函数都是一种封装 属性私有化:当类里面的属性不想被外部访问,可以将这些属性设置 ...

  2. mac 下修改 jenkins 端口以及Jenkins的启动、关闭与更新

    安装包安装的Jenkins修改默认端口的方法: 先关闭jenkins ; 命令行下修改端口:sudo defaults write /Library/Preferences/org.jenkins-c ...

  3. 流水线机制、滑动窗口协议、GBN、SR

    一.滑动窗口协议 为了解决停等操作的性能问题(发了一个分组之后一直等到确认了这个分组才发下一个),推出了流水线机制,提供资源利用率.就是允许发送方在收到对方的ACK前,发送多个分组 其中窗口是一个范围 ...

  4. 基于KVM的SRIOV直通配置及性能测试

    SRIOV介绍.VF直通配置,以及包转发率性能测试 小慢哥的原创文章,欢迎转载 目录 ▪ 1. SRIOV介绍 ▪ 2. 环境说明 ▪ 3. 开启SRIOV ▪ 4. 生成VF ▪ 5. VF直通 ▪ ...

  5. windows上gedit 安装

    1. 用浏览器打开https://wiki.gnome.org/Apps/Gedit 下载并安装 gedit 文本编辑器.这个操作无需管理员权限. 2. 把 gedit 放到桌面或者快速启动栏,这样你 ...

  6. API 网关的选型和持续集成

    2019 年 8 月 31 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·成都站,APISIX 作者温铭在活动上做了< API 网关的选 ...

  7. ArrayList源码解析[一]

    ArrayList源码解析[一] 欢迎转载,转载烦请注明出处,谢谢. https://www.cnblogs.com/sx-wuyj/p/11177257.html 在工作中集合list集合用的相对来 ...

  8. 常用函数-Win-IP

    //************************************************************************* // 函数名: GetAppPath // 返回 ...

  9. 将windows项目移植到linux上

    提要:由于项目使用java开发,移植中没有什么编译问题(移植很快,但小问题多) 1.移植过程中遇到的问题: (1).由于项目中使用了 1024以下的端口号,导致网络通信一直出错 原因:因为Linux要 ...

  10. 如何理解swift中的delegate

    Delegation翻译为代理或者委托,是一种设计模式.顾名思义,使class或struct能够将某些职责移交给其他类型的实例. 该设计模式通过定义一个封装(包含)delegate的protocol( ...