uvaoj455Periodic 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
题意:给出一个字符串,求出最短周期串的长度,每输出一个长度就隔一行再输入。
题解:枚举
#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(枚举)的更多相关文章
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- 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 ...
- 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 ...
- Swift----函数 、 闭包 、 枚举 、 类和结构体 、 属性
1 数组排序 1.1 问题 本案例实现一个整型数组排序的函数,数组排序的规则由传递的规则函数决定. 1.2 方案 首先定义一个整型数组排序函数sortInts,该函数有一个整型数组类型的参数,该参数必 ...
- [POJ3096]Surprising Strings
[POJ3096]Surprising Strings 试题描述 The D-pairs of a string of letters are the ordered pairs of letters ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
- E - Power Strings,求最小周期串
E - Power Strings Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ3096Surprising Strings(map)
题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...
- 拔高你的Java代码质量吧:推荐使用枚举定义常量(转)
提高你的Java代码质量吧:推荐使用枚举定义常量 一.分析 常量的声明是每一个项目中不可或缺的,在Java1.5之前,我们只有两种方式的声明:类常量和接口常量.不过,在1.5版之后有了改进,即新增了一 ...
随机推荐
- EOJ Monthly 2019.2 (based on February Selection) D 进制转换 【数学 进制转换】
任意门:https://acm.ecnu.edu.cn/contest/140/problem/D/ D. 进制转换 单测试点时限: 2.0 秒 内存限制: 256 MB “他觉得一个人奋斗更轻松自在 ...
- 配置文件和mybatis文件存放位置导致系统启动不了
1.web.xml <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation< ...
- 相机姿态估计(Pose Estimation)
(未完待续.....) 根据针孔相机模型,相机成像平面一点的像素坐标p和该点在世界坐标系下的3D坐标P有$p=KP$的关系,如果用齐次坐标表示则有: $$dp=KP$$ 其中d是空间点深度(为了将p的 ...
- PAT——1045. 快速排序
著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后的N个互不相同的正整数的排列,请问有多 ...
- NOIP2018(更新中)
\(Day_1T_1\) 铺设道路 (Link) 现在你有一个序列,每一个\(i\)有一个深度\(Deep[i]\),现在你可以选择任意的区间,将整个区间的\(Deep\)都减少\(1\).但前提是这 ...
- Nginx负载均衡+代理+ssl+压力测试
一.Tomcat安装 1.下载jdk,Tomcat,解压到/usr/local/ 2.配置jdk环境: # vim /etc/profile export JAVA_HOME=/usr/local/j ...
- asp.net mvc5 step by step(四)——关于Controller的ActionResult
ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的 是非ActionResult类型,控制器将会将结果转换 ...
- window系统mysql无法输入和无法显示中文的处理配置
第一步:使用记事本打开mysql安装目录下的"my.ini”文件. # MySQL client library initialization. [client] port= [mysql] ...
- Python-调用系统指令小记
import subprocess def exec_command(cmd, log_path, **kwargs): with open(log_path, 'w') as f: p = subp ...
- Spring quantz--定时任务调度工具
1.在xml中交给spring管理的一些类 <bean id="cancelOrderJobDetail" class="org.springframework.s ...