A character string is said to have period k if it can be formed by concatenating one or more repetitionsof another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formedby 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (onerepetition 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 yourprogram will test followed by a blank line. Each test case will contain a single character string of upto 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 areseparated by a blank line.

Sample Input

1

HoHoHo

Sample Output

2

问题链接:UVA455 Periodic Strings

题意简述

  如果一个串可以由长度为k的字符串重复多次连接而得到,则称该串为k周期。本题求一个串的最小周期。

问题分析:(略)

程序说明

  程序中,封装了一个函数strcmplen()用于比较相同串的两个指定长度的子串,使得主函数的逻辑变得简单。

  另外,输出格式上需要注意。题中有“Two consecutive output are separated by a blank line.”,需要小心处理。

AC的C语言程序如下:

/* UVA455 Periodic Strings */#include <stdio.h>
#include <string.h>#define MAXN 80int strcmplen(char a[], int s, int t, int len)
{int i;for(i=1; i<=len; i++) {if(a[s] != a[t])return a[s] - a[t];s++;t++;}return 0;
}int main(void)
{int n, len, i, j;char s[MAXN+1];scanf("%d", &n);while(n--) {scanf("%s", s);len = strlen(s);for(i=1; i<len; i++) {if(len % i)continue;for(j=i; j<len; j+=i)if(strcmplen(s, 0, j, i) != 0)break;if(j == len)break;}printf("%d\n", i);if(n)printf("\n");}return 0;
}

UVA455 Periodic Strings【水题】相关推荐

  1. UVA455 Periodic Strings

    问题链接:UVA455 Periodic Strings.入门练习题,用C语言编写程序. 题意:如果一个串可以由长度为k的字符串重复多次连接而得到,则称该串为k周期.本题求一个串的最小周期. 程序中, ...

  2. UVa455 Periodic Strings(kmp)

    先计算字符串的前缀函数,字符串长度为n,如果,说明字符串是周期的,否则不是. 代码参考: OJ/Periodic Strings(kmp).java at master · wuli2496/OJ · ...

  3. UVA455 - Periodic Strings(周期串)

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

  4. UVA455 - Periodic Strings

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830& ...

  5. 【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

    题干: Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wa ...

  6. 【Educational CF Round 86 (Rated for Div. 2) / 1342 A + B】- A. Road To Zero + B. Binary Perio - 水题

    目录 A. Road To Zero 题目大意 思路 代码 B. Binary Period 题目大意 思路 代码 A. Road To Zero time limit per test :1 sec ...

  7. Coderforces 字符串水题合集

    今天我们聊聊 Coderforces 字符串水题合集. 字符串就是string. 这是string的百度翻译,我们要谈的是画框的.↓ string是C++.java.VB等编程语言中的字符串,字符串是 ...

  8. 水题/poj 1852 Ants

    1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...

  9. HDU2673-shǎ崽(水题)

    如果不能够直接秒杀的题,就不算水题.又应证了那句话,有时候,如果在水题上卡住,那么此题对于你来说,也就不算是水题了额~~ 刚睡醒,迷迷糊糊. 题目的意思很简单,求一个最大的,再求一个最小的.几乎是什么 ...

最新文章

  1. x-pack watch邮件报警配置
  2. Cookies工作原理
  3. android 5.0 ios 8,Android 5.0和iOS8.1哪个好?安卓5.0与iOS8.1区别对比
  4. python3 的windows下文件操作注意问题
  5. 给一个不多于5位的正整数 要求_java练习题 打卡第5天
  6. android图标随着进度条动画,Android开发之ProgressBar字体随着进度条的加载而滚动...
  7. Java 12 - Java StringBuffer和StringBuilder类
  8. [Array]Majority Element
  9. 【资源下载】866页《计算机视觉:原理,算法,应用,学习》最新第五版-附下载
  10. 《植物大战僵尸》网页版!火热试玩当中
  11. 什么是自动化与自主化?
  12. 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
  13. python工程师要求-高级Python开发工程师职位描述与岗位职责任职要求
  14. [转]windows 7 professional 64 bit SP1 change system language(sucess)
  15. 普罗米修斯Prometheus手记
  16. 信息系统高级项目管理师英语词汇(二)-常见项目管理词汇
  17. 最让我感动的图片...
  18. 解不等式之调和级数不等式
  19. 【转】Chrome浏览器截全屏
  20. 【报告分享】2021天猫坚果消费趋势报告-CBNData(附下载)

热门文章

  1. AR增强现实开发介绍
  2. cocos2d-x 中创建 CCSprite 精灵动画
  3. DXUT框架剖析(3)
  4. 《Managed DirectX +C# 开发(入门篇)》系列文章
  5. robotframework安装_Robot Framework零基础入门教程
  6. 【java学习之路】(java SE篇)003.java SE基础语法之数组
  7. Nginx增加第三方外部插件
  8. matlab中confcounts,Matlab函数分类汇总—R2011b版.doc
  9. hbase建表语句_HBase 中文参考指南真不少~
  10. 《0元搭建个人博客史上最详细教程》(阿里云学生服务器+宝塔一键部署篇)