Periodic Strings UVA - 455
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 of 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
HINT
这个题目是要求求一个字符串的周期如:521521521的周期就是521的长度3。
解题思路就是将整个字符串进行切片处理,分成若干个可能的子字符串逐个判别,这样要解决的问题就有:
1.切除来的若干个等长字串如何保证等长?
利用相除取余来判断余数是否为零来解决。
2.切出来的等长字串如何来判断其是相等的?
将第一个字串作为标准,将其它子串的对应位置的字符进行比较来判断是否满足要求。注意看后面代码如何计算其他字串对应位 置的。
注意:输出的格式要求最后一个数字只有一个回车而其他的都有两个回车!!!
Accepted
#include<stdio.h>
#include<string.h>
int main()
{
int sum;
scanf("%d", &sum);
while(sum--)
{
char s[85];
scanf("%s", s);
int len = strlen(s);
int flag = 0;
for (int i = 1;i <= len;i++) //第一个循环来尝试找出可能的周期
{
if (len % i != 0)continue;
for (int j = 0;j < i;j++) //第二、第三个循环来按照选定的周期对字符串进行切片判断。
{
flag = 0;
for (int k = 1;k <= len / i;k++) //len/i切出来的子串的个数
{
if (k*i+j<len&&s[j] != s[k * i+j]) //每一次比较一个子串的对应位置的字符是否和第一个字串对应位置处的字符相等。
{
flag = 1;
break;
}
}
if (flag)break;
}
if (!flag&&sum!=0)
{
printf("%d\n\n", i);
break;
}
else if (!flag && sum == 0)
{
printf("%d\n", i);
break;
}
}
if (flag && sum != 0) //注意区分最后一个数字的输出格式
printf("%d\n\n", len);
else if (flag && sum == 0)
printf("%d\n", len);
}
}
Periodic Strings UVA - 455的更多相关文章
- UVA.455 Periodic Strings(字符串的最小周期)
Periodic Strings 模板 题意分析 判断字符串的最小周期 代码总览 /* Title:UVA.455 Author:pengwill Date:2016-12-16 */ #includ ...
- UVa 455 - Periodic Strings解题报告
UVa OJ 455 Periodic Strings A character string is said to have period k if it can be formed by conca ...
- UVa OJ 455 Periodic Strings
Periodic Strings A character string is said to have period k if it can be formed by concatenating ...
- UVa 455 Periodic Strings
题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...
- UVa 455 - Periodic Strings - ( C++ ) - 解题报告
1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...
- 【习题 3-4 UVA - 455】Periodic Strings
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举 [代码] #include <bits/stdc++.h> using namespace std; const ...
- 周期串(Periodic Strings,UVa455)
解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC A ...
- UVa455 Periodic Strings
#include <stdio.h>#include <string.h> int main(){ int T, k, len; char str[81], *p, ...
- UVA 455(最小周期)
最小周期可以用%枚举 #include <iostream> #include <string> #include <cstring> #include <c ...
随机推荐
- 后端程序员之路 8、一种内存kv数据库的实现
键值(Key-Value)存储数据库,这是一种NoSQL(非关系型数据库)模型,其数据按照键值对的形式进行组织.索引和存储.KV存储非常适合不涉及过多数据关系业务关系的业务数据,同时能有效减少读写磁盘 ...
- 看动画轻松学会 Raft 算法
由于 Paxos 算法过于晦涩难懂且难以实现,Diego Ongaro 提出了一种更易于理解和实现并能等价于 Paxos 算法的共识算法 - Raft 算法. 因为 Raft 算法清晰易懂越来越多的开 ...
- Mac电脑管理员密码丢失解决办法
1.重新启动电脑,并长按 Command (Win)+ S,并进入命令终端. 2.进入命令终端输入一下命令 /sbin/mount -uaw rm var/db/ .applesetupdone re ...
- Lua生成Guid(uuid)
全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) .GUID是一种由算法生成的二进制长度为 ...
- 面试常备,字符串三剑客 String、StringBuffer、StringBuilder
尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...
- 实话实说:只会.NET,会让我们一直处于鄙视链、食物链的下游
金三银四,是个躁动的季节. 结合最近的面试,谈一谈一个老牌开发人员的面试感悟. 大家都知道我的主力技术栈是 .NET + Devops + 弱前端 (当前技术认知,不排除以后变化). 面试了大小厂,有 ...
- 微信小程序在Android和Ios端的获取时间兼容性问题
an端 var time = new Date() 例如:2020-01-01 01:01:00 ios端 var time = new Date() 例如:2020/01/01 01:01:00 ...
- POJ_1227 Jack Straws 【二维平面判两线段相交】
一 题面 POJ1127 二 分析 在平面几何中,判断两线段相交的方法一般是使用跨立实验.但是这题考虑了非严格相交,即如何两个线段刚好端点相交则也是相交的,所以还需要使用快速排斥实验. 这里参考并引用 ...
- 2019 GDUT Rating Contest I : Problem B. Teamwork
题面: 传送门 B. Teamwork Input file: standard input Output file: standard output Time limit: 1 second Memor ...
- 如何在 ASP.Net Web Forms 中使用依赖注入
依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...