UVa 455 - Periodic Strings解题报告
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解题报告的更多相关文章
- UVA.455 Periodic Strings(字符串的最小周期)
Periodic Strings 模板 题意分析 判断字符串的最小周期 代码总览 /* Title:UVA.455 Author:pengwill Date:2016-12-16 */ #includ ...
- UVa 455 - Periodic Strings - ( C++ ) - 解题报告
1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...
- UVa 455 Periodic Strings
题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...
- UVa OJ 455 Periodic Strings
Periodic Strings A character string is said to have period k if it can be formed by concatenating ...
- 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- 【LeetCode】555. Split Concatenated Strings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 在chrome浏览器安装各种前端,后台插件
如果想要在chrome安装插件,首先得先有应用chrome的商城,但是默认情况下是没有应用商店: 默认情况下下面这个界面是空白的,没有任何东西,第一步就先安装[谷歌访问助手] 安装步骤 第一步: 下载 ...
- python编程基础之二十四
函数: def 函数名([参数1],[参数2],[参数3], ... ,[参数n]): 函数体代码 函数名命名规则:同标识符命名相同,但是多了一点,不要和系统函数重名,其实所有命名都是一样只要符合标识 ...
- Centos 7.2天兔(Lepus 3.8)数据库监控系统部署
天兔(Lepus 3.8)数据库监控系统部署 转载自:https://blog.csdn.net/m0_38039437/article/details/79613260 一.安装LAMP基础环境 首 ...
- PowerUp攻击渗透实战
记录下PowerUp在实战渗透中的利用 准备环境: kali linux 攻击机 已获得靶机meterpreter(非管理)权限 win7 靶机 拥有powershell环境 1)Invoke-Al ...
- [插件化开发] Poc之后,我选择放弃OSGI
Poc之后,我选择放弃OSGI TIPS: 如贵司允许重构老系统或者允许使用OSGI的第三方框架改造所带来的投入成本,并且评估之后ROI乐观,那么还是可以使用的. Runtime Version 以下 ...
- [Luogu2458][SDOI2006]保安站岗
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
- opencv::Laplance算子
Laplance算子 理论:在二阶导数的时候,最大变化处的值为零即边缘是零值.通过二阶导数计算,依据此理论我们可以计算图像二阶导数,提取边缘. 拉普拉斯算子(Laplance operator) 处理 ...
- opensuse下安装redis
1.安装gcc zypper install gcc 2.解压,安装到指定路径 sudo tar xzf redis-4.0.1.tar.gzcd redis#安装到指定目录中sudo make PR ...
- tinyxml2
网上下载tinyxml2:tinyxml2.h和tinyxml2.cpp 加载xml XMLDocument doc; doc.LoadFile("test.xml"); ...
- 9.Linux用户管理(下)
1. 为用户添加密码 [root才能执行] 1为新用户添加密码{只能是root} {密码尽可能的复杂} [0-9][a-Z][a-Z] [!@#$%^&]* [root@yinwucheng ...