zoukankan      html  css  js  c++  java
  • HDU 4763 Theme Section(KMP灵活应用)

    Theme Section
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4876    Accepted Submission(s): 2439


    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

    题意:求出在字符串前、中、后均出现的最长字符的长度
    分析:利用next数组求出相同前后缀,再用kmp验证中间是否存在该前缀

    #include<iostream>
    #include<string.h>
    using namespace std;
    char p[1000001];
    int nex[1000001];
    int len;
    void getnex(int pp)
    {
        int i=0,k=-1;
        nex[0]=-1;
        while(i<pp)
        {
            if(k==-1||p[i]==p[k])
                nex[++i]=++k;
             else
                k=nex[k];
        }
    }
    bool kmp(int st)//验证中间是否存在与前后缀匹配的字符串
    {
        int i=st,j=0;
        int n=strlen(p);
        while(i<n-st)//st~n-st区间
        {
            if(j==-1||p[i]==p[j])
                i++,j++;
            else
                j=nex[j];
            if(j==st)//有一段与前缀相等的
                return 1;
        }
        return 0;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",p);
            len=strlen(p);
            if(len<3)
            {
                printf("0
    ");continue;
            }
            getnex(len);
            int ans=0;
            while(nex[len]!=-1)
            {
                if(nex[len]>ans&&kmp(nex[len]))
                {
                    ans=nex[len];
                    break;
                }
                len=nex[len];
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    使用select2插件并添加拼音首字母检索
    sql id 或使用nolock
    .net 开源组件
    EF 创建数据库的策略 codefist加快效率!【not oringin!】
    个人拾遗!数组的拷贝等
    编程拾遗:集合类型的函数,返回值,如果没有,就返回默认集合new,或者 default(T)好一些。
    C# datatable to list
    npoi导出excel 导出List<T>
    display:inline、block、inline-block的区别 摘】
    ie下,jquery为动态添加的节点添加事件,用live
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9502673.html
Copyright © 2011-2022 走看看