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. Protobuf-net学习笔记

    对于Socket应用来说,如何序列化和反序列化消息一直是比较头痛的问题,C#提供了自动序列化的功能(类似AS3中的AMF),但是唯一的缺点就是前后端都必须是C#实现,如果前后端语言不一致该怎么办? G ...

  2. 关于Windows文件名和路径名的那些事

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:关于Windows文件名和路径名的那些事.

  3. Asp.Net Core- 配置组件详解

    我们之前写的配置都是放置在配置文件Web.config或者app.config中,.net core提供了全新的配置方式,可以直接写在内存中或者写在文件中. .Net Core的配置API主要体现在3 ...

  4. XMPP——Smack[4]状态,心情,头像更改

    呵呵,三天时间,看的不是很深入,欢迎大家补充呀 修改自身状态 包括上线,隐身,对某人隐身,对某人上线 public static void updateStateToAvailable(XMPPCon ...

  5. libpq程序例子

    程序: [root@lex tst]# cat testlibpq.c /* * testlibpq.c * Test the C version of LIBPQ, the POSTGRES fro ...

  6. MST最小生成树及克鲁斯卡尔(Kruskal)算法

    最小生成树MST,英文名如何拼写已忘,应该是min spaning tree吧.假设一个无向连通图有n个节点,那么它的生成树就是包括这n个节点的无环连通图,无环即形成树.最小生成树是对边上权重的考虑, ...

  7. C 栈实例

    就近匹配: 应用1:就近匹配 几乎所有的编译器都具有检测括号是否匹配的能力 如何实现编译器中的符号成对检测? #include <stdio.h> ][]; ]; p = a[]; ; 算 ...

  8. Models

    Models Models control the data source, they are used for collecting and issuing data, this could be ...

  9. 零门槛!ZBLibrary仿微信朋友圈自定义View,就是这么简单!

    传统方法是继承现有View再重写方法,这种方式缺点很多: 1.往往不能在xml编辑器中预览效果: 2.比较难实现预期效果,比如设置宽度为wrap_content,实际显示为match_parent等: ...

  10. Android_listView_BaseAdapter_downLoadImg

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...