Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 41252   Accepted: 17152

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.

题意:

给出的一个字符串某个字串链接N次得出的,求最大N;

思想:

KMP匹配算法之next[];模式串结尾指针回溯最小移动次数即最小字串长度,N=s.length%(s.length-next[s.length]);

#include <stdio.h>
#include <string.h>
char s[1000005];
int num[1000005];
int main()
{
while(scanf("%s",s),s[0]!='.')
{
int j=-1;
num[0]=-1;
int n=strlen(s);
for(int i=0; i<n;)
{
if(j==-1||s[i]==s[j])num[++i]=++j;
else j=num[j];
}
if(n%(n-num[n])==0)
printf("%d\n",n/(n-num[n]));
else printf("1\n");
}
return 0;
}

POJ 2406:Power Strings的更多相关文章

  1. 【poj 2406】Power Strings 后缀数组DC3模板 【连续重复子串】

    Power Strings 题意 给出一个字符串s,求s最多由几个相同的字符串重复而成(最小循环节的重复次数) 思路 之前学习KMP的时候做过. 我的思路是:枚举字符串的长度,对于当前长度k,判断\( ...

  2. 【POJ 2406】Power Strings 连续重复子串

    看的<后缀数组——处理字符串的有力工具>这篇论文,在那里这道题是用后缀数组实现的,复杂度为$O(nlogn)$,很明显长度为$2×10^6$的数据会TLE,所以必需得用复杂度为$O(n)$ ...

  3. 【POJ 2406】Power Strings(KMP循环节)

    终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...

  4. 【POJ 2406】 Power Strings

    [题目链接] 点击打开链接 [算法] KMP 如果字符串中存在循环节,则next[len] = (循环节个数 - 1) * 循环节长度 循环节个数 = len / (len - next[len]) ...

  5. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  6. POJ 2109 :Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18258   Accepted: ...

  7. 【TOJ 2406】Power Strings(KMP找最多重复子串)

    描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc& ...

  8. POJ2406:Power Strings——题解

    http://poj.org/problem?id=2406 就是给一个串,求其循环节的个数. 稍微想一下就知道,KMP中nxt数组记录了所有可与前面匹配的位置. 那么如果我们的循环节长度为k,有n个 ...

  9. POJ 3096:Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6258   Accepted: 407 ...

随机推荐

  1. SpringJUnit4ClassRunner拉起来的单元测试怎么装配Container实例

    由于历史代码的原因,产品中部分spring装配的实例需要通过Container的实现类(自定义的)去获取.那么当在单元测试中怎么实例化这个Container实现呢? 实例化Container实现需要A ...

  2. mac osx 启动wireshark闪退

    wireshark启动会提示安装x11 去x11地址安装后 启动还是闪退 原来是姿势不对 这样才行~~ 这一步 这个路径一定要对!路径一定要对!路径一定要对! 然后报错不用管它,如果没反应了,就继续等 ...

  3. SQL注入攻击之关键字检测

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

  4. Java基础之处理事件——添加工具提示(Sketcher 9 with tooltips)

    控制台程序. 在Java中实现对工具提示的支持是非常简单的,秘诀仍在我们一直使用的Action对象中.Action对象拥有存储工具提示文本的内置功能因为文本是通过SHORT_DESCRIPTION键提 ...

  5. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  6. Excel 生成sql语句

    CONCATENATE 拼接字符串 IF 不能跟 CONCATENATE 连用,所以可以通过建立新列去完成更高的需求 新建一列 cw=CONCATENATE("insert into biC ...

  7. /Users/alamps/AndroidStudioProjects/Demo10ScrollView

    .define xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" andr ...

  8. paper 69:Haar-like矩形遍历检测窗口演示Matlab源代码[转载]

    Haar-like矩形遍历检测窗口演示Matlab源代码 clc; clear; close all; % Haar-like特征矩形计算 board = 24 % 检测窗口宽度 num = 24 % ...

  9. ssh-copy-id帮你建立信任

    一.ssh-keygen -t rsa [nameA@machineA]$ ssh-keygen -t rsa Generating public/private rsa key pair. Ente ...

  10. 解决windows的控制台显示utf8乱码的问题

    在控制台的属性里 修改自体为: 新宋体 在控制台下执行命令: chcp 65001