zoukankan      html  css  js  c++  java
  • HDOJ4763(KMP原理理解)

    Theme Section

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3237    Accepted Submission(s): 1512


    Problem Description
    It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of 'EAEBE', where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.

    To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?
     
    Input
    The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.
     
    Output
    There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
     
    Sample Input
    5
    xy
    abc
    aaa
    aaaaba
    aaxoaaaaa
     
    Sample Output
    0
    0
    1
    1
    2
     
    思路:本题考查对KMP算法中的next函数的理解。对于字符串s来说,next[l]表示在字符串s中使前l个字符的前缀与后缀相等最长长度。
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAXN = 1000005;
    char buf[MAXN], fa[MAXN], son[MAXN];
    int net[MAXN], len;
    void getNext(char s[])
    {
        int k = -1;
        int i = 0;
        net[0] = -1;
        while(i < len)
        {
            if(k == -1 || s[i] == s[k])
            {
                i++;
                k++;
                net[i] = k;
            }
            else
            {
                k = net[k];
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%s", buf);
            len = strlen(buf);
            if(len < 3)
            {
                printf("0
    ");
                continue;
            }
            getNext(buf);
            int l = net[len];
            while(l > 0 && 3 * l > len)  l = net[l];
            while(l > 0)
            {
                int size = 0;
                for(int i = l; i < len - l; i++)
                {
                    fa[size++] = buf[i];
                }
                fa[size] = '';
                for(int i = 0; i < l; i++)
                {
                    son[i] = buf[i];
                }
                son[l] = '';
                if(strstr(fa, son) != NULL)
                {
                    break;
                }
                l = net[l];
            }
            printf("%d
    ", l);
        }
        return 0;
    }
     
  • 相关阅读:
    关于ActionScript中那些你不知道的事情
    Flash Player 11 Stage3D学习大杂烩
    Qt 控制台输入输出(支持中文)
    Redis消息发布订阅的稳定性验证结论
    C++11 Lambda表达式(匿名函数)用法详解
    vue中“:”、“.”、“@”意义
    QT中printf输出不同步的解决办法
    QT5中使用SQLite
    QT 调用user32.dll移动鼠标
    10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/6047465.html
Copyright © 2011-2022 走看看