hdu4763 kmp

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?

InputThe 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.OutputThere 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题意:找三个子串,第一个必须在开头题解:kmp,刚开始想错了,以为是枚举,tle了,题目又看了一遍才明白意思,用next处理能快很多不用遍历了

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const int N=1000000+5,maxn=1000000+5,inf=1e9+5;

int Next[N];

void getnext(string str)
{
    int k=-1;
    Next[0]=-1;
    for(int i=1;i<str.size();i++)
    {
        while(k>-1&&str[k+1]!=str[i])k=Next[k];
        if(str[k+1]==str[i])k++;
        Next[i]=k;
    }
}
bool kmp(string ptr,string str)
{
    int k=-1,ans=0;
    for(int i=0;i<ptr.size();i++)
    {
        while(k>-1&&str[k+1]!=ptr[i])k=Next[k];
        if(str[k+1]==ptr[i])k++;
        if(k==str.size()-1)return 1;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        string ptr;
        cin>>ptr;
        getnext(ptr);
        int k=Next[ptr.size()-1],ans=0;
        while(k!=-1){
            string str=ptr.substr(0,k+1),p=ptr.substr(k+1,ptr.size()-2*k-2);
          //  cout<<p<<" "<<str<<endl;
            if((k+1)*3<=ptr.size()&&kmp(p,str))
            {
                ans=k+1;
                break;
            }
            k=Next[k];
        }
        cout<<ans<<endl;
    }
    return 0;
}

时间: 2024-10-06 01:21:56

hdu4763 kmp的相关文章

hdu-4763(kmp+拓展kmp)

题意:给你一个串,问你满足最大字串既是前后缀,也在字符串除去前后缀的位置中出现过: 思路:我用的是拓展kmp求的前后缀,只用kmp也能解,在字符串2/3的位置后开始遍历,如果用一个maxx保存前2/3的最大的next(kmp),也就是最大字串的前后缀,在与拓展kmp的next[i]进行比较: 代码: #include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define ma

hdu4763(KMP的应用)

题意:给一个字符串,问最长的一个子串A,他是前缀,同时是后缀,并且中间也出现过A.并且出现的三个A都不没有重叠部分. 解法:先KMP求出失配数组,然后将所有的是后缀且是前缀的打上标记,然后遍历整个next数组,(对于每个位置的next来说,一直next向前取就是找到此前缀的一个个是整个字符串前缀的后缀,比较绕)暴力枚举判断每个串的所有匹配前缀的后缀是否合法. 代码: /**************************************************** * author:xi

HDU4763 Theme Section 【KMP】

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1114    Accepted Submission(s): 579 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

hiho 1015 KMP算法 &amp;&amp; CF 625 B. War of the Corporations

#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一只河蟹,于是河蟹就向小Hi和小Ho提出了那个经典的问题:“小Hi和小Ho,你们能不能够判断一段文字(原串)里面是不是存在那么一些……特殊……的文字(模式串)?” 小Hi和小Ho仔细思考了一下,觉得只能想到很简单的做法,但是又觉得既然河蟹先生这么说了,就

hdu2328 Corporate Identity 扩展KMP

Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for

KMP算法详解

这几天学习kmp算法,解决字符串的匹配问题,开始的时候都是用到BF算法,(BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果.BF算法是一种蛮力算法.)虽然也能解决一些问题,但是这是常规思路,在内存大,数据量小,时间长的情况下,还能解决一些问题,但是如果遇到一些限制时间和内存的字符串问

2016——3——16 kmp习题

1.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2725 题目大意:找一个串在另一个串中出现的次数 题解:kmp(纯裸题) 2.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2732 题目大意:给你一个字符串,让你求出最大重复周期(最大周期不和本身重合) 题解:kmp或者扩展kmp(但我并未用这种方法),我用的是kmp,但是一直WA,原来是求next数组把while写成了

KMP算法

1 /* next数组是KMP算法的关键,next数组的作用是:当模式串T和主串S失配 2 * ,next数组对应的元素指导应该用T串中的哪一个元素进行下一轮的匹配 3 * next数组和T串相关,和S串无关.KMP的关键是next数组的求法. 4 * 5 * ——————————————————————————————————————————————————————————————————— 6 * | T | 9 | a | b | a | b | a | a | a | b | a | 7