zoukankan      html  css  js  c++  java
  • HDU-4763-Theme Section(KMP)

    链接:

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

    题意:

    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?

    思路:

    KMP得到Next数组,
    考虑原来的字符串所有前缀等于后缀 的串, 在中间部分找相同,
    判断是否Next[i] = len, 其中len为前缀长度.
    Next数组还是要多了解了解.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e6+10;
    
    char s[MAXN];
    int Next[MAXN];
    
    void GetNext(char *ori)
    {
        int len = strlen(ori);
        int j = 0, k = -1;
        Next[0] = -1;
        while (j < len)
        {
            if (k == -1 || ori[j] == ori[k])
            {
                ++j;
                ++k;
                Next[j] = k;
            }
            else
                k = Next[k];
        }
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%s", s);
            GetNext(s);
            int len = strlen(s);
            int ml = Next[len];
            while (ml > 0)
            {
                int flag = false;
                for (int i = ml*2;i <= len-ml;i++)
                {
                    if (Next[i] == ml)
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                    break;
                ml = Next[ml];
            }
            printf("%d
    ", ml);
        }
    
        return 0;
    }
    
  • 相关阅读:
    Linux安装软件时90%的人会遇到这个报错,如何解决?
    mongo下查询ObjectId(id)类型的数据
    解决nohup启动logstash生成的文件过大的问题
    ES集群服务器下线节点扩容后上线的流程
    knife4j的使用记录
    mongodb的update函数更新数据,更新文档中的某个具体字段的数据
    Python celery异步框架
    VueX插件使用
    Vue-router插件使用
    Vue 自定义指令
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11606754.html
Copyright © 2011-2022 走看看