​   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的更多相关文章

  1. UVA.455 Periodic Strings(字符串的最小周期)

    Periodic Strings 模板 题意分析 判断字符串的最小周期 代码总览 /* Title:UVA.455 Author:pengwill Date:2016-12-16 */ #includ ...

  2. 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 ...

  3. UVa OJ 455 Periodic Strings

     Periodic Strings  A character string is said to have period k if it can be formed by concatenating ...

  4. UVa 455 Periodic Strings

    题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...

  5. UVa 455 - Periodic Strings - ( C++ ) - 解题报告

    1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...

  6. 【习题 3-4 UVA - 455】Periodic Strings

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举 [代码] #include <bits/stdc++.h> using namespace std; const ...

  7. 周期串(Periodic Strings,UVa455)

    解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC  A ...

  8. UVa455 Periodic Strings

    #include <stdio.h>#include <string.h> int main(){    int T, k, len;    char str[81], *p, ...

  9. UVA 455(最小周期)

    最小周期可以用%枚举 #include <iostream> #include <string> #include <cstring> #include <c ...

随机推荐

  1. springCloud中的服务调用feign

    springCloud中的服务调用(要在调用端写) 前提进行了服务注册 流程: 1.在服务模块中添加依赖 <!--服务调用--> <dependency> <groupI ...

  2. SpringBoot+Vue豆宝社区前后端分离项目手把手实战系列教程02---创建后端工程

    本节代码开源地址 代码地址 项目运行截图 搭建后端工程 0.导入sql 在数据库导入 /* Navicat Premium Data Transfer Source Server : localhos ...

  3. oracle 查看 FK constraint referenced_table及columns

    select uc.table_name, uc.r_constraint_name, ucc.table_name, listagg(ucc.column_name, ',') within gro ...

  4. oracle 查锁

    reference:https://www.cnblogs.com/XQiu/p/5212787.html--以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$ ...

  5. Docker 镜像加速教程

    原文链接:https://fuckcloudnative.io/posts/docker-registry-proxy/ 在使用 Docker 和 Kubernetes 时,我们经常需要访问 gcr. ...

  6. 写了一个vue+antdv的后台管理模板

    1,项目简介 写在前面===>这是一个vue+antdv的后台管理模板 项目地址: https://github.com/BaiFangZi/vue-antd-manage 1.1,概述 ​ 最 ...

  7. Pyqt5——带图标的表格(Model/View)

    需求:表格中第一列内容为学生学号,为了突出学号的表示,在第一列的学号旁增加学号图标. 实现:(1)使用Qt的model-view模式生成表格视图. (2)重写代理(QAbstractItemDeleg ...

  8. sqlyog如何增删改查?

    转: sqlyog如何增删改查? 下面是一道完整的 sqlyog 增删改查的练习, 顺着做下去,可以迅速掌握. 1. 创建部门表dept,并插入数据: 2. 创建emp员工表,并插入数据: sql 代 ...

  9. 还在用crontab? 分布式定时任务了解一下

    前言 日常任务开放中,我们会有很多异步.批量.定时.延迟任务要处理,go-zero中有 go-queue,推荐使用 go-queue 去处理,go-queue 本身也是基于 go-zero 开发的,其 ...

  10. mongodb 聚合(aggregate)

      MongoDB中文手册|官方文档中文版 https://docs.mongoing.com/ 聚合操作处理数据记录和 return 计算结果.聚合操作将来自多个文档的值组合在一起,并且可以对分组数 ...