HUST-1010 the minimum length

Source: Internet
Author: User

Description

There is a string. 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. for example, a = "abcdefg ". I got ABCD EfgabcdefgabcdeFgabcdefg... then I cut the red part: efgabcdefgabcde as string B. From B, you shoshould find out the shortest.

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

bcabcabefgabcdefgabcde

Sample output

37. There is a string a, and a is used to form a new string AAAAA ..., now we can get string B from the two different positions of the new string. The minimum length of A is calculated as follows, the ring sentence is the minimum cycle of 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;}


HUST-1010 the minimum length

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.