Power Strings POJ 2406【KMP Next的应用】

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.

题意:求子串在源字符串出现的最大次数

总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且

循环节长度为:   i - next[i]

循环次数为:       i / ( i - next[i] )
//算次数不需要next[i] != 0这个条件

#include<cstdio>
#include<cstring>
#define MAX 1000100
char str[MAX];
int next[MAX];
int len;
void Get_Next()
{
	int i=0,j=-1;
	next[0]=-1;
	while(i<len)
	{
		if(j==-1 || str[i]==str[j])
		{
			++i,++j;
			next[i]=j;
		}
		else
			j=next[j];
	}
}
int main()
{
	while(scanf("%s",str),strcmp(str,"."))
	{
		len=strlen(str);
		Get_Next();
		if((len)%((len)-next[len])==0)
			printf("%d\n",(len)/((len)-next[len]));
		else printf("1\n");
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#define MAX 1000100
char str[MAX];
int next[MAX];
int len;
void Get_Next()
{
	int i=0,j=-1;
	next[0]=-1;
	while(i<len)
	{
		if(j==-1 || str[i]==str[j])
		{
			++i,++j;
			next[i]=j;
		}
		else
			j=next[j];
	}
}
int main()
{
	while(scanf("%s",str),strcmp(str,"."))
	{
		len=strlen(str);
		Get_Next();
		(len)%((len)-next[len])==0&&next[len]!=0?printf("%d\n",(len)/((len)-next[len])):printf("1\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-29 18:17:54

Power Strings POJ 2406【KMP Next的应用】的相关文章

Power Strings (poj 2406 KMP)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   Accepted: 13804 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: 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

Power Strings POJ - 2406

Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 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".

G - Power Strings POJ 2406 (字符串的周期)

G - Power Strings Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def

(求循环节的个数)Power Strings -- poj -- 2406

链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&qu

Power Strings POJ - 2406,字符串hash

题目链接:POJ - 2406 题目描述 定义两个字符串s1和s2的乘积s1*s2为将s1和s2连结起来得到的字符串. 例如:s1="xy",s2="z",那么s1*s2="xyz". 由此可以定义s1的幂次:s1^0="",s1^n=s1*s1^(n-1),n>0. 输入 输入包含多组测试数据. 每组数据由一行构成,包含一个字符串s. 输入数据以"."结束. 输出 对于每组输入数据输出一行,找出最大

Power Strings POJ - 2406 后缀数组

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 defin

Power Strings - POJ 2406(求循环节)

题目大意:叙述的比较高大上,其实就是一个字符串B = AAAAAAA,求出来这个A最短有多长   分析:注意如果这个串不是完全循环的,那么循环节就是就是它本身.   代码如下: #include<stdio.h> #include<string.h> const int MAXN = 1e6+7; const int oo = 1e9+7; char s[MAXN]; int next[MAXN]; void GetNext(int N) { int i=0, j=-1; next

Power Strings POJ - 2406(next水的一发 || 后缀数组)

后缀数组专题的 emm.. 就next 循环节../ 有后缀数组也可以做 从小到大枚举长度i,如果长度i的子串刚好是重复了len/i次,应该满足len % i == 0和rank[0] - rank[i] == 1(整个串的等级比 i位置开始的后缀的等级大1  (i位置开始的后缀即为比总串低一个等级的后缀)) 和height[rank[0]] == len-i (整个串 和 比它低一个等级的串的最长公共前缀的长度 是总长度减去这个循环节的长度)这些条件的 #include <iostream>