• 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;
    }
     
  • 相关阅读:
    python 获取当前执行的命令 处于什么文件内
    FlatBuffers
    flink
    auto_ptr,unique_ptr,shared_ptr,weak_ptr
    Java DES 加解密文件
    quartz Web项目基础最简单配置
    C# webbrowser 修改useragent
    bat产生随机数并复制文件及生成文件列表
    outlook 用宏发邮件
    SSL证书请求文件(CSR)生成指南
  • 原文地址:https://www.cnblogs.com/program-ccc/p/6047465.html
Copyright © 2020-2023  润新知