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版之后有了改进,即新增了一 ...
随机推荐
- NSArray中地内存管理 理解
问题: 通过alloc和init的方法创建了NSArray和NSDictionary,然后通过addobject和setobject:forkey:将object添加进去.通过addobject会自动 ...
- UITableView控件Protocell的Identifier设置 注意事项
1. 注意:如果想使用Subtitle类型的单元格,需在Storyboard中将Protocell设置为subtitle类型,且Protocell的identifier必须与ViewControll ...
- woff字体找不到导致的404错误
在iis中添加mime类型: .woff application/x-font-woff .woff2 application/x-font-woff
- AngularJS 表单验证小结
注:本文所述的表单验证即AngularJS自带的表单验证,无需引入第三方的插件 一.常用的验证特性标签 1.非空 为input加入一个required属性即可,例如: <input type=& ...
- 修改office文档修改日期
修改“创建日期”可采用如下方法: 首先把系统日期调整到您所希望的时间,然后到MS-DOS方式下,对该文件输入如下命令:COPY /B filename +,, (一个加号.两个逗号),当询问您是否确认 ...
- Java IP白名单相关工具类
关于设置IP白名单相关的一些方法,整理,记录了一下. package com.tools.iptool; import java.util.ArrayList; import java.util.Ha ...
- Mahout介绍
3.11简介 Mahout:是一个Apache的一个开源的机器学习库,主要实现了三大类算法Recommender (collaborative filtering).Clustering.classi ...
- iOS11.2-11.3.1进行越狱及问题
设备环境:Electra.iOS11.13.1 PS:Electra最新版本进行越狱只支持11.14以下的版本.同时这是不完美越狱,每次重启手机都需要重新越狱,最后,由于Electra版本推出仓促,一 ...
- linux配置mysq与navicat关联
第一步:在linux中安装mysql(执行如下语句) 安装 mysql: yum install mysql yum install mysql-server yum install mysql-de ...
- 如何用SQL语句处理缓慢变化维(渐变维,拉链表)SCD-2?
假设有一张居民维表,需要记录居民状态的变更历史,根据Kimball建模理论,设计居民维表如下: 另外在ODS中有居民信息的每日快照表(每天都记录一份居民的全量信息):O_USERINFO 如何将ODS ...