zoukankan      html  css  js  c++  java
  • HUST 1010 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 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. 

    InputMultiply 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. 
    OutputFor each line, output an integer, as described above.Sample Input

    bcabcab
    efgabcdefgabcde
    

    Sample Output

    3
    7

    kmp求最小循环节。

    代码:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char str[1000001];
    int Next[1000001],len,k;
    void findNext() {
        int j = -1,i = 0;
        Next[0] = -1;
        while(i < len) {
            if(j == -1 || str[i] == str[j]) {
                Next[++ i] = ++ j;
            }
            else j = Next[j];
        }
    }
    int main() {
        while(~scanf("%s",str)) {
            len = strlen(str);
            findNext();///先确立Next数组
    
            printf("%d
    ",len - Next[len]);
        }
    }
  • 相关阅读:
    华师菜鸟杯2020
    「算法」排序
    生成函数
    多项式乘法逆
    多项式牛顿迭代
    「数学」快速幂
    「算法」贪心
    「组合数学」一:什么是组合数学
    「具体数学」三:整值函数
    「图论」树上问题
  • 原文地址:https://www.cnblogs.com/8023spz/p/9656025.html
Copyright © 2011-2022 走看看