Power Strings

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.
 
 
【题意】
  一个字符串的最大重复字串。
  即一个字符串是一个子串最多重复多少次得到的。
 
【分析】
  

  ① len==1||s[next[len-1]]!=s[len-1] ans=1;
  ② len%(len-next[len])==0 ans= len/(len-next[len]);

  原因如图:

  

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1000010 char s[Maxn];
int len,nt[Maxn]; void kmp()
{
nt[]=;
int p=;
for(int i=;i<=len;i++)
{
while(s[i]!=s[p+]&&p) p=nt[p];
if(s[i]==s[p+]) p++;
nt[i]=p;
}
} int main()
{
int n,i;
while()
{
gets(s+);
len=strlen(s+);
if(len==&&s[]=='.') break;
kmp();
if(len%(len-nt[len])==) printf("%d\n",len/(len-nt[len]));
else printf("1\n");
}
}

[POJ2406]

2016-07-20 16:30:02

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

  1. 【POJ2406】Power Strings(KMP,后缀数组)

    题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longi ...

  2. poj2406 Power Strings(kmp)

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

  3. POJ 2406 Power Strings(KMP)

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

  4. Power Strings(KMP)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 D ...

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

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

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

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

  7. jzoj3208. 【JSOI2013】编程作业(kmp)

    题面 Description Will相信,很多同学都有过这样的经历:大牛已经写好了编程作业,而作为菜鸟的自己不会写怎么办呢?拿大牛的代码抄一下嘛!但是提交一模一样的作业是不是不太好?于是就改一改变量 ...

  8. 【CH1809】匹配统计(KMP)

    题目链接 摘自https://www.cnblogs.com/wyboooo/p/9829517.html 用KMP先求出以a[i]为结尾的前缀与b匹配的最长长度. 比如 f[i] = j,就表示a[ ...

  9. 【poj2406】 Power Strings

    http://poj.org/problem?id=2406 (题目链接) 题意 给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值. Solution 后 ...

随机推荐

  1. Web内容禁止选中的两种方式

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5761818. ...

  2. magento性能优化的教程(非常详细)

    Magento是一套专业开源的电子商务系统,Magento设计得非常灵活,具有模块化架构体系和丰富的功能但有朋友会发现此模块用到了会发现非常的缓慢了,那么下面我们来看关于magento性能优化的例子. ...

  3. Css3炫酷总结使用

    先从CSS3的选择器说起: E F:所有的子孙元素: E>F: E中的子元素: E+F:E元素之后的最近的选择器: E~F:E中所以后面兄弟元素(CSS3 不包括自己本身,前面也不包括) att ...

  4. ADO.Net知识总结

    (一)基础知识 ADO.NET: .NET中用来向数据库提交执行SQL语句的一堆类 本机访问直接"Windows验证",但是一般项目中都是单独的数据库服务器,程序在另外一台电脑上连 ...

  5. 20151215jquery学习笔记--jqueryUI --dialog(对话框)

    对话框(dialog),是 jQuery UI 非常重要的一个功能.它彻底的代替了 JavaScript 的 alert().prompt()等方法,也避免了新窗口或页面的繁杂冗余 一.开启多个 di ...

  6. TransactionScope事务

    一个错误的理解就是Complete()方法是提交事务的,这是错误的,事实上,它的作用的表示本事务完成,它一般放在try{}的结尾处,不用判断前台操作是否成功,如果不成功,它会自己回滚. #region ...

  7. c#对象初始化

    class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...

  8. MySQL的基本使用

    SQL     DDL:数据定义语言  CREATE DROP ALTER     DML:数据操作语言  SELECT INSERT UPDATE DELETE     DCL:数据控制语言  GR ...

  9. 01_C语言基础

    内容提要: 1. C语言概述2. 数据类型.运算符与表达式3. C语言程序结构 4. VC6.0使用练习 知识详解01:C语言的历史 1. C语言与其它语言比较 汇编语言: (1).可直接对硬件进行操 ...

  10. linux-信号。

    信号 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的. 信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到 ...