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. eclipse引入jar类包的源代码

    为了方便查看源代码,有时候需要对jar类包,引入源代码. 下载源代码 源代码是zip或者rar的压缩包 引入到项目中

  2. MySQL特殊语法---replace into

    MySQL中有这样的SQL语句 1. replace into tbl_name(col_name, ...) values(...) 2. replace into tbl_name(col_nam ...

  3. 阿里云ECS被攻击

    今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...

  4. c语言学习之基础知识点介绍(十二):结构体的介绍

    一.结构体的介绍 /* 语法: struct 结构体名{ 成员列表; }; 切记切记有分号! 说明:成员列表就是指你要保存哪些类型的数据. 注意:上面的语法只是定义一个新的类型,而这个类型叫做结构体类 ...

  5. 在Eclipse的DDMS中查看手机data文件夹中的内容

    在模拟器状态下,在Eclipse的DDMS中的File Explorer下查看手机data/data中的内容是件非常轻松的事情,特别是查看databases 但是在真机模式下就全然不是那么一回事了,在 ...

  6. leetcode处女作

    闲来无事[真的吗?你确定→_→ 在leetcode上刷了一道题.费时一小时,也是醉了.谨以此文,纪念我的伟大成果.[呵呵 题目是找出非排序数组中缺少的最小正整数.要求时间复杂度O(n),空间复杂度为常 ...

  7. /etc/sysconfig/目录详解

    /etc/sysconfig/目录包括了在红帽企业LINUX下各种系统配置文件,以下是在/etc/sysconfig/目录下的文件列表如图: 500)this.width=500;"> ...

  8. ASP.NET跨页面传值技巧

      1 使用QueryString变量    QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用  ...

  9. ios专题 -block用法

    what is block Blocks are a language-level feature added to C, Objective-C and C++, which allow you t ...

  10. 实例:图像载入、显示、混合与输出[OpenCV 笔记8]

    是的是的,忍着尿意努力更新,就是为了更到wuli男神的部分,当然要把男神放在前面镇楼,欢迎下载配图,具体操作见code wuliEddie.jpg logo.png results.jpg LoadS ...