HDU 2406 Power Strings KMP 基础
来源:互联网 发布:编程男士性格好怪 编辑:程序博客网 时间:2024/04/28 00:02
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 43073 Accepted: 17980
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
abcdaaaaababab.
Sample Output
143
题意:给出一个字符串 问该字符串最多可以有多少个相同的字串连接而成。
使用KMP算法,由于KMP的特殊性,答案可由标记数组直接的出,即sum=len/(len-1-p[len-1](前提是除的尽 否则答案为1)。
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int p[1000005],len;char b[1000005];void find();int main(){int n,i,j,k,sum;while(scanf("%s",b)){if(b[0]=='.')break;find();len=strlen(b);if(len%(len-1-p[len-1])==0)printf("%d\n",len/(len-1-p[len-1]));elseprintf("1\n");}}void find(){int i,j=-1;p[0]=-1;for(i=1;b[i]!='\0';i++){if(j>=0 && b[j+1]!=b[i])j=p[j];if(b[j+1]==b[i])j++;p[i]=j;}}
0 0
- HDU 2406 Power Strings KMP 基础
- hdu 2406 Power Strings KMP
- poj 2406 Power Strings kmp基础
- 2406 Power Strings【kmp】
- poj 2406 Power Strings---kmp
- POJ 2406 Power Strings KMP
- poj-2406-Power Strings-kmp
- POJ-2406 Power Strings KMP
- Power Strings - POJ 2406 KMP
- poj 2406 Power Strings kmp
- POJ 2406-Power Strings (KMP)
- 【KMP】 POJ 2406 Power Strings
- poj 2406 Power Strings KMP
- POJ 2406 Power Strings KMP
- poj 2406 Power Strings KMP
- poj 2406 Power Strings (KMP)
- [2406]Power Strings (POJ) KMP
- Power Strings (poj 2406 KMP)
- 多线程与线程同步
- UIScrollViewDelegate Protocol Reference
- redis 3.2 protected-mode配置
- 当spring 容器初始化完成后执行某个方法
- NOIP2010普及组 导弹拦截(重庆一中高2018级信息学竞赛测验3) 解题报告
- HDU 2406 Power Strings KMP 基础
- Android Studio中获取sha1证书指纹数据的方法
- PHP木马的防范方法
- 合并果子解题分析
- Java反射机制的学习
- AsyncTask的使用
- 滴滴的程维应该是幸福的
- iOS RunLoop 的 Mode
- ExtJs6 时间转换为'x天','x小时','x分','x秒前'并且鼠标移上去悬浮提示具体时间