zoukankan      html  css  js  c++  java
  • HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763

    Theme Section

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


    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

    题解:

    给定一个字符串,找出能构成"EAEBE"形式的字符串的E的最长长度。其中A和B任意。

    1.可知next[len]就是:即是前缀又是后缀的最长子串。所以我们就解决的首尾两个E(如果大于三分之一长度,可以通过next数组回退)。

    2.剩下的问题就是:在两个E之间的连续子串中,是否能找到第三个E。

    3.怎么找到?枚举里面的每个位置(注意不能与首尾的E有重叠),通过next数组的回退,一直找。详情还是看代码吧。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 1e6+10;
    19 
    20 char x[MAXN];
    21 int Next[MAXN];
    22 
    23 void get_next(char x[], int m)
    24 {
    25     int i, j;
    26     j = Next[0] = -1;
    27     i = 0;
    28     while(i<m)
    29     {
    30         while(j!=-1 && x[i]!=x[j]) j = Next[j];
    31         Next[++i] = ++j;
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     int T;
    38     scanf("%d", &T);
    39     while(T--)
    40     {
    41         scanf("%s", x);
    42         int len = strlen(x);
    43         get_next(x, len);
    44         int r = Next[len];
    45         while(r>0 && len/r<3) r = Next[r];
    46 
    47         bool hav_ans = false;
    48         for(; Next[r]>=1; r = Next[r])
    49         {
    50             for(int i = 2*r; i<=len-r; i++)
    51             {
    52                 int k = i;  //不要写成Next[i];
    53                 while(Next[k]>r) k = Next[k];
    54                 if(Next[k]==r)
    55                 {
    56                     hav_ans = true;
    57                     break;
    58                 }
    59             }
    60             if(hav_ans) break;
    61         }
    62         printf("%d
    ", r);
    63     }
    64 }
    View Code
  • 相关阅读:
    IPFS实践之初体验
    自己写的屏幕录像及播放软件
    绿色ip扫描工具
    ASIHTTPRequest 编码问题
    ios开发中,A valid provisioning profile for this executable was not found,的解决方法
    毕业设计之蚁群算法的研究——起始篇
    分享两个模拟get和post方法的工具类,让应用能够与服务器进行数据交互
    安卓中生成二维码和扫描二维码
    分享一个安卓中异步获取网络图片并自适应大小的第三方程序(来自github)
    不只是个程序员
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7891899.html
Copyright © 2011-2022 走看看