HUST - 1010 The Minimum Length

Description

There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me
the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.

Input

Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.

Output

For each line, output an integer, as described above.

Sample Input

bcabcab
efgabcdefgabcde

Sample Output

3
7
题意:有一个字串A,现在利用A组成了一个新的字符串AAAAA...,现在从这个新的字符串的两个不同位置剪下去得到字符串B,问A的最小长度
思路:求A的最小长度,环句话就是说求B的最的最小循环节
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000005;

char pattern[maxn];
int next[maxn];

int main() {
	while (scanf("%s", pattern) != EOF) {
		int m = strlen(pattern);
		next[0] = next[1] = 0;
		for (int i = 1; i < m; i++) {
			int j = next[i];
			while (j && pattern[i] != pattern[j])
				j = next[j];
			next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;
		}

		printf("%d\n", m-next[m]);
	}
	return 0;
}

时间: 2024-10-01 03:04:05

HUST - 1010 The Minimum Length的相关文章

HUST 1010 The Minimum Length(KMP,最短循环节点)

题意: 有一个字符串A,假设A是“abcdefg”,  由A可以重复组成无线长度的AAAAAAA,即“abcdefgabcdefgabcdefg.....”. 从其中截取一段“abcdefgabcdefgabcdefgabcdefg”,取红色部分为截取部分,设它为字符串B. 现在先给出字符串B, 求A最短的长度. 分析: 设字符串C = AAAAAAAA....  由于C是由无数个A组成的,所以里面有无数个循环的A, 那么从C中的任意一个起点开始,也都可以有一个循环,且这个循环长度和原来的A一样

HUST - 1010

HUST - 1010 类比POJ 2406 自己的什么话都没有的传送门 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 typedef long long LL; 6 7 const int maxn = 1e6 + 10; 8 int next[maxn]; 9 10 void kmp(char *pattern) { 11 //int n

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted

Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.Examples:1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should

F - The Minimum Length HUST1010( kmp计算最小循环节)

F - The Minimum Length Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status Description There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now

The Minimum Length - HUST 1010(求最小循环节)

题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大的匹配. 代码如下. =========================================================================================================== #include<stdio.h> #include<

F - The Minimum Length

There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length

hust1010 The Minimum Length

题目描述 There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the le

KMP 总结

再次回来总结KMP,发现有点力不从心,学久了,越觉得越来越不理解了. 估计是写KMP已经不下50遍了吧.每次用都是直接默写.. KMP算法,串模式匹配算法,通过预处理得到next数组,再进行匹配. 几个要重点记忆的地方: 1. next数组的含义 next[i] = t 表示以i位置结尾的前缀串(相对于原串是前缀串)的真前缀和真后缀相等的最大值为t 2. 最小覆盖子串: 我不会证明,但是记得住结论,记len表示字符串的长度,则这个字符串的最小覆盖子串为 len - next[len] 3. KM

KMP解决字符串最小循环节相关问题

经典问题 : 给出一个由某个循环节构成的字符串,要你找出最小的循环节,例如 abababab 最小循环节当是 ab ,而类似 abab 也可以成为它的循环节,但并非最短. 分析 : 对于上述问题有两个结论 如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且 循环节长度为:    i - next[i] 循环次数为:       i / ( i - next[i] ) 水平有限,用自己的语言描述怕