UVA - 10298 Power Strings (KMP求字符串循环节)

Description

Problem D: Power Strings

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).

Each test case is a line of input representing s, a string of printable characters. For each
s you should print the largest n such that s = a^n for some string
a. 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.

Sample Input

abcd
aaaa
ababab
.

Output for Sample Input

1
4
3

题意:求循环节

思路:KMP模板求循环节

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1000000;

char str[MAXN];
int next[MAXN];

int main() {
	while (scanf("%s", str) != EOF && str[0] != '.') {
		int len = strlen(str);
		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];
		}
		printf("%d\n", len/(len-next[len]));
	}
	return 0;
}

UVA - 10298 Power Strings (KMP求字符串循环节),布布扣,bubuko.com

时间: 2024-10-08 12:35:16

UVA - 10298 Power Strings (KMP求字符串循环节)的相关文章

UVA 10298 - Power Strings(KMP)

UVA 10298 - Power Strings 题目链接 题意:本意其实就是,给定一个字符串,求出最小循环节需要几次循环出原字符串 思路:利用KMP中next数组的性质,n - next[n]就是最小循环节,然后n / 循环节就是答案 代码: #include <cstdio> #include <cstring> const int N = 1000005; char str[N]; int next[N]; void getnext() { int n = strlen(s

POJ 2406 Power Strings (求字符串循环节出现的次数)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44217   Accepted: 18449 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 = "

UVa 10298 - Power Strings

题目:求一个串的最大的循环次数. 分析:dp,KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到最近的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 如果存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配: 则S[next[len] ~ len-1]就是循环节(且最小),否则next[len]为0: 因此,最大循环次数为len/(len-next[len]),最小循环节为S[next[len] ~

UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

题意:定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路:如果完全不循环,顶多就是类似于abc1这样咯,即n=1.但是如果循环出现了,比如abab,那就可以表示成(ab)2.还有一点,就是要使得n尽量大,那么当出现abababab时,应该要这么表示(ab)4,而不是(abab)2. 此题用神奇的KMP解决,也就是主要利用next数组.举例说明. 一般出现循环的都会大概是这样的:abcabcabc.而这样

KMP + 求最小循环节 --- POJ 2406 Power Strings

Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Accepted* Submission Date: 20

POJ--2406Power Strings+KMP求字符串最小周期

题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个题目的关键点. 代码例如以下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+100; char str[maxn]; in

(Incomplete) UVa 10298 Power Strings

方法:暴力 / kmp 先说一下暴力的方法,就是尝试把原来的string不断分割,在substring 里求解.code 如下 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitse

模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. 例: abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 analyse: 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len]. 证明: 举个例子:abcab

KMP + 求最小循环节 --- HDU 1358 Period

Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期. analyse: 还是next数组的运用,详见上一篇博客. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Acce