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;
    }
  • 相关阅读:
    Docker 命令收集
    Linux环境变量总结
    Docker 安装Hadoop HDFS命令行操作
    Docker 搭建Spark 依赖singularities/spark:2.2镜像
    Docker 搭建Spark 依赖sequenceiq/spark:1.6镜像
    kill命令
    每天一个linux命令:ps命令
    swoole执行外部程序称为进程
    php休眠微秒
    php监控文件变化
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9502673.html
Copyright © 2011-2022 走看看