Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.
 
 
解题思路:题目大意:给定一个字符串由某一个子串重复得来,求出重复的次数。
利用fail数组fail[len],子串循环的次数满足if(len%(len-fail[len])==0)
           ans=len/(len-fail[len]);
否则ans=1
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; char s[];
int fail[];
int len; void get_fail(); int main()
{
while(scanf("%s",s+),s[]!='.')
{
len=strlen(s+);
get_fail();
int ans=;
//ÀûÓÃfail[len]
if(len%(len-fail[len])==)
ans=len/(len-fail[len]);
printf("%d\n",ans);
}
}
void get_fail()
{
memset(fail,,sizeof(fail));
fail[]=-;
for(int i=;i<=len;i++)
{
int p=fail[i-];
while(p>=&&s[p+]!=s[i])
p=fail[p];
fail[i]=p+;
//cout<<i<<" "<<fail[i]<<endl;
}
}

KMP入门(周期)的更多相关文章

  1. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  2. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  3. 转载 - kmp next函数 kmp的周期问题,深入了解kmp中next的原理

    出处:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html kmp next函数 kmp的周期问题,深入了解kmp中next的原理 ...

  4. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  6. KMP 入门

    再次学习 \(\rm KMP\) 后不一样的理解. 一些概念 定义字符串 \(S\) 的真 前/后 缀为非自身的 前/后 缀. 定义字符串 \(S\) 的 \(border\) 为 \(S\) 的公共 ...

  7. KMP入门题目[不定期更新]

    HDU 1711 Number Sequence(模板题) #include <cstdio> ; ; int N, M; int textS[MAXN]; int tarS[MAXL]; ...

  8. KMP入门(匹配)

    Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M ...

  9. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

随机推荐

  1. JSP九大内置对象分析

    JSP九大内置对象分为三类: 1.输入输出对象:out对象.response对象.request对象 2.通信控制对象:pageContext对象.session对象.application对象 3. ...

  2. iOS 定时器Timer常见问题

    最近有朋友问我使用NStimer遇见与ScrollView并存时存在主线程阻塞的问题,自己总结几种解决方法: 问题原因: 一般定时器timer都会被以默认模式default添加到主线程的runloop ...

  3. Java对MySQL数据库进行连接、查询和修改【转载】

    一般过程: (1) 调用Class.forName()方法加载驱动程序. (2) 调用DriverManager对象的getConnection()方法,获得一个Connection对象. (3) 创 ...

  4. 谈使用Eclipse与DDMS调试Android程序的方法

    在Eclipse开发工具中调试程序的方法很多,但是使用Eclipse调试Android程序时需要注意一些细节上的问题.许多刚接触 Android的开发者,在调试Android程序时总是不能迅速地找到程 ...

  5. android学习日记27--Dialog使用及其设计模式

    1.Dialog概述 对话框一般是一个出现在当前Activity之上的一个小窗口,处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功 ...

  6. ASM 图解

    http://www.askmaclean.com/archives/know-oracle-asm-basic-html.html

  7. 从问题看本质: 研究TCP close_wait的内幕

    Socket应用服务器TomcatOSUbuntu  /* * @author: ahuaxuan * @date: 2010-4-30 */ 最近遇到的一个关于socket.close的问题,在某个 ...

  8. 学习笔记之Linux开发(C语言)

    第二章 Linux下C程序开发环境 vi编辑器 gcc 第三章 Linux基础 Kernel Shell 第三章 Linux技术实验 ls -l 以长格式的形式查看当前目录下所有可见文件的详细属性. ...

  9. excel vba 打印设置(转)

    FROM: http://hi.baidu.com/kdlipm/blog/item/0897dd16ffc03e59f3de32ab.html PageSetup 函式就會記錄時, 設定的記錄三個部 ...

  10. mvc_ajax_for form

    在上一篇介绍MVC中的Ajax实现方法的时候,曾经提到了除了使用Ajax HTML Helper方式来实现之外,Jquery也是实现Ajax的另外一种方案. 通过get方法实现AJax请求 View ...