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 ...
随机推荐
- Anno&Viper -分布式锁服务端怎么实现
1.Anno简介 Anno是一个微服务框架引擎.入门简单.安全.稳定.高可用.全平台可监控.依赖第三方框架少.底层通讯RPC(Remote Procedure Call)采用稳定可靠经过无数成功项目验 ...
- 最常用SQL joins:内连接(交集)、左外连接、右外连接、左连接、右连接、全连接(并集),全外连接
1.内连接.两个表的公共部分用Inner join,Inner join是交集的部分. Select * from TableA A inner join TableB B on A.key=B.ke ...
- vue:表格中多选框的处理
效果如下: template中代码如下: <el-table v-loading="listLoading" :data="list" element-l ...
- spring boot的 yml和properties的对比
Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!支持的外部配置方式就有很多种,笔者没有去统计,也许是为了灵活使用吧. application.yml 和 appli ...
- .NET测试断言工具Shouldly
.NET测试断言工具Shouldly .NET测试 Shouldly在GitHub的开源地址:https://github.com/shouldly/shouldly Shouldly的官方文档:ht ...
- c++类的简介
一 类的定义 在c++中,我们使用"类"来描述"对象",可以说"类"的实现是c++OOP的核心. 类中包括对象的"属性" ...
- WPF 基础 - 属性
1. CLR 属性 .Net Framework 中的属性又称为 CLR 属性,是对 private 字段的安全访问包装. 使用 ILSpy 反编译器可以看到 C# 中代码的属性的编译结果是 set. ...
- Nacos常用配置
属性配置 1. 配置年级是否显示 这里配置的屏蔽的年级,在运营后台去删掉相关id就行了 2. 过滤标签显示特定课程数据 指定 yaml 文件显示 course.tagCourse.tagName=寒假 ...
- 基础篇:java.security框架之签名、加密、摘要及证书
前言 和前端进行数据交互时或者和第三方商家对接时,需要对隐私数据进行加密.单向加密,对称加密,非对称加密,其对应的算法也各式各样.java提供了统一的框架来规范(java.security)安全加密这 ...
- Dart简明教程
[前言]Dart语言是使用flutter框架开发时候必备的语言,flutter是一个跨平台的框架,一套代码就可以完美实现安卓和ios两个平台,适配也很不错,Dart语言很友好,和java很类似,学习成 ...