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;
    }
     
  • 相关阅读:
    IOS debug网络PonyDebugger 实践篇
    基于S7-200的PLC对里程轮(增量式码盘)解码的应用
    SICP 习题 (1.14)解题总结
    TQ210裸机编程(2)——LED流水灯
    DP练习(初级):ZigZag
    java 十六进制数的转换
    Oracle DB 执行表空间时间点恢复
    html中文乱码(解决办法)
    html的列表
    MySQL的模糊搜索
  • 原文地址:https://www.cnblogs.com/program-ccc/p/6047465.html
Copyright © 2011-2022 走看看