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. JAVA锁的可重入性

    机制:每个锁都关联一个请求计数器和一个占有他的线程,当请求计数器为0时,这个锁可以被认为是unhled的,当一个线程请求一个unheld的锁时,JVM记录锁的拥有者,并把锁的请求计数加1,如果同一个线 ...

  2. Highcharts在IE中不能一次性正常显示的一种解决办法

    由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...

  3. Hyper-V Windows 8.1 & Windows Server 2012 R2 Q&A

    从Windows8开始,x64位系统自带Hyper-V功能,很多开发者和专业用户往往希望利用的Microsoft提供的这一免费功能,但是微软在这方面并不是最佳. 主要写几个大家经常遇到的问题. Win ...

  4. Java_Iterator-------迭代器配合Listarray使用,具有更多的功能(转载)

    转载自:http://www.cnblogs.com/amboyna/archive/2007/09/25/904804.html 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可 ...

  5. C++学习笔记3—对话框

    1.模态对话框 CTipDlg tipDlg; INT_PTR nResponse = tipDlg.DoModal(); if(nResponse == IDCANCEL) {  return; } ...

  6. IOS中的NSTimer定时器详解

    /* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...

  7. ZeroMemory和memset的区别

    摘自百度百科,保存为学习使用 ZeroMemory,是美国微软公司的软件开发包SDK中的一个宏. 其作用是用0来填充一块内存区域. 声明 void ZeroMemory( PVOID Destinat ...

  8. STUN/TURN/ICE协议在P2P SIP中的应用(一)

    1           说明 本文详细描述了基于STUN系列协议实现的P2P SIP电话过程,其中涉及到了SIP信令的交互,P2P的原理,以及STUN.TURN.ICE的协议交互 本文所提到的各个服务 ...

  9. STL unique使用问题

    string strs[] = {"one","one","two","three","three" ...

  10. [java bug记录] java.util.zip.ZipException: invalid code lengths set

    1. 描述:将代码迁移到maven工程,其中用ZipInputStream读取/src/main/resources下的zip文件时报错:“java.util.zip.ZipException: in ...