Power Strings

Time Limit: 3000MS Memory Limit: 65536K

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.


就是一个寻找字符串的循环节,用KMP的思想,其实很简单就是len/(len-next[n])。(当然是在没有余数的情况下,不然就是1)


#include<stdio.h>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 1e6+100;
char s[maxn];
int next[maxn]; void cal_next()
{
int k;
next[0] = k = -1;
int len = strlen(s);
for(int i=1;i<len;i++)
{
while(k>-1 && s[i] != s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
} int main()
{
while(scanf("%s",s))
{
if(s[0] == '.' && s[1] == 0)
break;
cal_next();
int len = strlen(s);
if(len%(len - next[len-1] - 1) == 0)
printf("%d\n",len/(len - next[len-1] - 1));
else
printf("1\n");
memset(s,0,sizeof(s));
}
return 0;
}

POJ:2406-Power Strings(寻找字符串循环节)的更多相关文章

  1. POJ 2406 Power Strings next数组循环节应用、

    题意:就给出个字符串做*的定义.a^0 = "" (the empty string) and a^(n+1) = a*(a^n).    题目要求n的最大值. 思路: 化简上面的 ...

  2. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  3. POJ 2406 Power Strings(字符串的最小循环节)

    题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...

  4. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

  5. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  6. POJ 2406 Power Strings (KMP)

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

  7. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  8. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  9. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  10. poj 2406 Power Strings【最小循环节】

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36926   Accepted: 15254 D ...

随机推荐

  1. L. Right Build bfs

    http://codeforces.com/gym/101149/problem/L 给出一个有向图,从0开始,<u, v>表示要学会v,必须掌握u,现在要学会a和b,最小需要经过多少个点 ...

  2. [异常]undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>

    在进行Rspec 编译测试: bundle exec rspec spec/requests/static_pages_spec.rb 提示错误: FF Failures: 1) Static pag ...

  3. WPF学习04:2D绘图 使用Shape绘基本图形

    我们将使用Shape进行基本图形绘制. 例子 一个可移动的矩形方框: XAML代码: <Window x:Class="Shape.MainWindow" xmlns=&qu ...

  4. Java中的变量——通过示例学习Java编程(4)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=14&cid= 变量是用来存放可以更改的值的容 ...

  5. 在webconfig放置固定值

    通常的,为了布置到服务器后修改的方便通常把一些会改变的值放在webconfig: 首先在web.ocnfig中放入如下值 <appSettings> <add key="A ...

  6. python第一模块数据类型

    一·进制之间的转换 十进制转换为二进制:逆序取余法. 二进制转换为十进制:如1101  1*2^0 + 0*2^1 + 1*2^2 +1 十六进制转换为二进制:231     0010  0011   ...

  7. 关于IE兼容的问题

    以下内容,均来自不同的网站,非本人原创,只是收集一下放在一起! =============================== [一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 ...

  8. github小技巧之Creating a pull request 创建 pull 请求

    创建一个 pull 请求是为了协作更改存储库.这些变化会产生一个分支,它确保主分支保持干净整洁. 与commits提交是不同的,提交是fork之后的一种操作. 在你可以打开一个 pull 请求之前,您 ...

  9. 精仿百思不得姐客户端应用iOS源码

    XFBaiSiBuDeJie 高仿百思不得姐客户端 初次学习使用RAC,还不是怎么熟悉,使用的仍是MVC模式,MVVM还在摸索中... 如果大家觉得还不错,请给颗星星支持下~~~ 程序中使用到的库 A ...

  10. mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona

    1. 操作mysql的时候提示如下错误 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont ...