Power Strings

Time Limit: 3000MS
Memory Limit: 65536K

Total Submissions: 29663
Accepted: 12387

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
 
::初学KMP真心不容易,理解起来很吃力,还好今天老师有讲解KMP的基本原理,现在思路清晰了一点,赶紧做这道以前暴力过的题。
证明:(PKU 2406 POWER STRINGS --- 字符串匹配,KMP算法

定理:假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]
例子证明:

设S=q1q2q3q4q5q6q7q8,并设next[8] = 6,此时str = S[len - next[len]] = q1q2,由字符串特征向量next的定义可知,q1q2q3q4q5q6 = q3q4q5q6q7q8,即有q1q2=q3q4,q3q4=q5q6,q5q6=q7q8,即q1q2为循环子串,且易知为最短循环子串。由以上过程可知,若len可以被len - next[len]整除,则S存在循环子串,否则不存在。
解法:利用KMP算法,求字符串的特征向量next,若len可以被len - next[len]整除,则最大循环次数n为len/(len - next[len]),否则为1。

   1: #include <cstdio>

   2: #include <cstring>

   3: #include <algorithm>

   4: using namespace std;

   5: const int maxn=1e6;

   6: char s[maxn+10];

   7: int next[maxn+10];

   8:  

   9: void get_next(char s[],int len)

  10: {

  11:     int i=0,j=-1;

  12:     next[0]=-1;

  13:     while(i<len)

  14:     {

  15:         if(j==-1||s[i]==s[j]) {j++; i++; next[i]=j;}

  16:         else j=next[j];

  17:     }

  18: }

  19:  

  20: int main()

  21: {

  22:     while(scanf("%s",s),s[0]!='.')

  23:     {

  24:         int len=strlen(s);

  25:         get_next(s,len);

  26:         if(len%(len-next[len])==0)

  27:             printf("%d\n",len/(len-next[len]));

  28:         else

  29:             printf("1\n");

  30:     }

  31:     return 0;

  32: }

POJ 2406 Power Strings (KMP)的更多相关文章

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

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

  2. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  3. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

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

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

  5. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  8. poj 2406 Power Strings KMP匹配

    对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...

  9. KMP POJ 2406 Power Strings

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

随机推荐

  1. IIS启动网站出错的几个解决方法

    在ASP.NET项目中使用了IIS服务器,由于系统是XP的,而在装系统的时候IIS没有一起装,所以从网上下载的IIS5.0版本(其它版本XP是用不了的).但是在使用的过程中老是出问题,每次调试好后,过 ...

  2. csharp:Chart

    http://www.dotnetperls.com/chart using System; using System.Windows.Forms; using System.Windows.Form ...

  3. mysql学习笔记 第九天

    order by ,limit 和where子查询的使用 order by: order by 列名1,[列名2],[列名3]...(结果先按列1进行排序,在列1的相同的情况下,再按照列2的排序,以此 ...

  4. mysql学习笔记 第八天

    where,group by,having重新详解 where的用法: where与in的配合使用,in(值1,值2,...)表示结果在值1,值2,...其中任何一个. 聚合函数和group by的用 ...

  5. 「C语言」单链表/双向链表的建立/遍历/插入/删除

    最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ...

  6. C# 分层 三层架构

    Hello! 三层架构↓↓↓↓↓↓ 三层架构分为:表现层(UI(User Interface)).业务逻辑层(BLL(Business Logic Layer)).数据访问层(DAL(Data Acc ...

  7. new 小记

    new运算符 能根据需求来创建对象的实例 通过与构造函数和一系列初始化过程中使用的可选参数来创建对象的实例,对象创建完成后,新创建的对象继承自构造函数的原型 function Person(name) ...

  8. ASP.NET MVC下判断用户登录和授权的方法

    日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在asp. ...

  9. AloneJs.confirmbox() —— 确认框

    一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...

  10. 初学Node(五)文件I/O

    文件读写 Node的出现的一个亮点就是让JS也有了读写文件的能力,而且实现起来要比其他语言更简单,对文件的一些操作我们都可通过fs模块来完成.fs即fileSystem的缩写,fs模块可以完成对文件的 ...