zoukankan      html  css  js  c++  java
  • kmp(前中后最长相同长度)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763

    Theme Section

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


    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
     
    Source
     
    Recommend
    liuyiding
     题意:求前中后最长相同串长度,不能有重叠,
    我还以为我这种方法会超时,没想到过了。!!
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <string.h>
    #define INF  10000000
    using namespace std;
    char a[1000009] , b[1000009], str[200009];
    int ans = 0 ;
    
    void getnext(char *a , int len , int *next)
    {
        next[0] = -1 ;
        int k = -1 , j = 0 ;
        while(j < len)
        {
            if(k == -1 || a[j] == a[k])
            {
                k++;
                j++;
                next[j] = k ;
            }
            else
            {
                k = next[k];
            }
        }
    }
    
    int main()
    {
        int n ;
        scanf("%d" , &n);
        while(n--)
        {
            int next[1000009];
            int next1[1000009];
            scanf("%s" , a);
            int len = strlen(a);
            getnext(a , len , next);
            int q = next[len];
            while(q > 0)
            {
                if(q * 3 > len)
                {
                    q = next[q];
                    continue ;
                }
                for(int i = 0 ; i < q ; i++)
                {
                    str[i] = a[i] ;
                }
                getnext(a , q , next1);
                int j = 0 , i = q ;
                while(i < len - q && j < q)
                {
                    if(j == -1 || str[j] == a[i])
                    {
                        i++;
                        j++;
                    }
                    else
                    {
                        j = next[j] ;
                    }
                }
                if(j == q)
                {
                    ans = q;
                    break ;
                }
                q = next[q];
    
            }
            printf("%d
    " , q);
        }
    
        return 0 ;
    }
     
  • 相关阅读:
    ajax
    异步加载js的方法
    node的特点,优缺点及应用场景
    ajax面试题
    jQuery实现手风琴效果
    jQuery简介
    原型
    string 对象属性和方法
    函数声明和函数表达式
    JavaScript 基本语法
  • 原文地址:https://www.cnblogs.com/nonames/p/11296313.html
Copyright © 2011-2022 走看看