Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 56162   Accepted: 23370

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.

Source

 
 
kmp的经典应用
设$len$表示字符串的长度,$next[i]$表示$i$号字符串的最长公共前后缀的长度
如果$len \ mod  \  next[len] == 0$,那么循环节的长度为$n / next[len]$
 
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 1e6 + ;
char s[MAXN];
int fail[MAXN];
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#endif
while(scanf("%s", s + ) && s[] != '.') {
int N = strlen(s + ), now = ;
for(int i = ; i <= N; i++) {
while(now && s[i] != s[now + ]) now = fail[now];
if(s[i] == s[now + ]) now++;
fail[i] = now;
}
if(N % (N - fail[N]) == ) printf("%d\n", N / (N - fail[N]));
else printf("1\n");
}
return ;
}
 
 

POJ2406 Power Strings(KMP)的更多相关文章

  1. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  2. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  3. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

  4. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  5. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  6. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  7. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  8. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  9. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

随机推荐

  1. 菜鸟学配置vim

    看啥都不会的菜鸟怎么进行vim配置 如果你想让你的vim和VS差不多你一定需要这个网址 http://www.open-open.com/lib/view/open1429884437588.html ...

  2. Vector 、ArrayList、LinkedList比较

    这三者都可以随机访问,也就是支持通过索引查找数据. 都是有序(可以实现元素怎么进怎么出) Vector和ArrayList比较 相同之处 1 它们都是List 它们都继承于AbstractList,并 ...

  3. windows环境下wampserver配置https

    因为公司业务主要是在微信上进行开展的,所以作为程序员的我们每天的开发任务就都是在与微信打交道,这个时候我们就需要在本地配置端口映射到外网,方便我们在微信客户端进行调试. 最近某种需要,所以需要配置 h ...

  4. 常见ie6的浏览器兼容bug

    1.文字本身的大小不兼容 同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的, ie下实际占高16px,下留白3px ff 下实际占高17px,上留白1px,下留白3px ...

  5. join......on 后面的and 和where的区别

    a.where 是在两个表join完成后,再附上where条件. b. and 则是在表连接前过滤A表或B表里面哪些记录符合连接条件,同时会兼顾是left join还是right join.即 假如是 ...

  6. Android ViewPager+TabHost实现首页导航

    今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就 ...

  7. Windows7建立无线热点

    很实用的技巧,加以记录. 最初我是想使用connectify的,不过安装这个软件之后,发现有线账号登不上了,所以就选择使用Windows7自带的工具了. 首先以管理员身份运行cmd. 进入之后cd到c ...

  8. Struts2学习-横切关注点

    1.建空项目 2.建包 3.建类 4.编写 package com.nf.action; import com.opensymphony.xwork2.ActionInvocation; import ...

  9. February 28 2017 Week 9 Tuesday

    Time you enjoy wasting, was not wasted. 你乐于挥霍的时间,都不能算作是浪费. A few days ago, I learned a sentence from ...

  10. CRUD全栈式编程架构之数据层的设计

    CodeFirst 一直以来我们写应用的时候首先都是创建数据库 终于在orm支持codefirst之后,我们可以先建模. 通过模型去创建数据库,并且基于codefirst可以实现方便的 实现数据库迁移 ...