HDU 4763 (KMP)

题意:EAEBE这种结构的字符串,求E的最大长度;

分析:我直接就从s.length()/3开始枚举,结果TLE了,正解是KMP,但是string的substr和find能够水过,只能说学弟太强了(TLE了一次就想怎么减时间,而不是找正解)

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 char s[MAXN];
25 int Next[MAXN];
26 bool flag[MAXN];
27 int main()
28 {
29     int T;
30     scanf("%d",&T);
31     while(T--)
32     {
33         scanf("%s", s);
34         int l = strlen(s);
35         int i = 0, j = -1;
36         Next[0] = -1;
37         while(i < l)
38         {
39             while(j != -1 && s[i]!=s[j]) j = Next[j];
40             Next[++i]=++j;
41         }
42         memset(flag, 0, sizeof(flag));
43         int t = l;
44         while(t > 0)
45         {
46             if(l >= 2*t)
47                 flag[t] = true;
48             t = Next[t];
49         }
50         int ans = 0;
51         for(int i = l-1; i > 1; i--)
52         {
53             t = i;
54             while(t > 0)
55             {
56                 if(flag[t] && l >= i+t && i >= 2*t)
57                 {
58                     ans = max(ans,t);
59                     break;
60                 }
61                 t = Next[t];
62             }
63         }
64         printf("%d\n",ans);
65     }
66     return 0;
67 }

正解KMP

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 int main()
25 {
26     ios::sync_with_stdio(false);
27     string s;
28     int T;
29     cin>>T;
30     while(T--)
31     {
32         cin>>s;
33         int l = s.length();
34         int h = l/3,t = 0;
35         for(int i=h; i>=1; i--)
36         {
37             if(s[i-1] != s[l-1])///数据太水了,加上这个优化就可以过,否则TLE
38             {
39                 continue;
40             }
41             string s1 = s.substr(0,i);
42             int j = l - i;
43             string s2 = s.substr(j,i);
44             if(s1 == s2)
45             {
46                 s2 = s.substr(i,l-2*i);
47                 if(s2.find(s1) != s2.npos)
48                 {
49                     t = i;
50                     break;
51                 }
52             }
53         }
54         cout<<t<<endl;
55     }
56     return 0;
57 }

string水过

时间: 2024-08-29 06:55:57

HDU 4763 (KMP)的相关文章

hdu 4763 Theme Section (简单KMP)

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

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

Cyclic Nacklace HDU 3746 KMP 循环节

Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i

HDU 4763:Theme Section(KMP)

http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section 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 progr

HDU 4763 - Theme Section(KMP)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目描述: 现有一字符串S,要求在S中找到最长的子串E,使得S满足格式“EAEBE”,其中A,B可以为任意的S子串.也就是说子串E既是S的前缀也是S的后缀,同时还在S中间出现,但不与前缀E与后缀E重叠. 解题思路: 学习KMP的第一道题.KMP的详解这篇文章写得很好:http://www.cnblogs.com/c-cloud/p/3224788.html,看完应该能理解前缀后缀概念和nex

【HDU 4763】Theme Section(KMP)

这题数据水的一B,直接暴力都可以过. 比赛的时候暴力过的,回头按照正法做了一发. 匹配的时候 失配函数 其实就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会很直接的出,但是KMP的思想经常渗透在很多题目里面,最近需要多练习一下. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1000005; int _next[max

HDU - 4763 Theme Section (KMP的next数组的应用)

给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个位置对应的前缀.然后暴力判断前缀与后缀是否相等即可. 如图,枚举的位置为 i,则Next[i] = j,.然后判断0~j 是否和 k~len是否是相同的. 注意要判断合法性,即前缀 + 中缀的长度不能比当前枚举的位置大,且后缀开始的位置一定要比当前枚举的位置大. 判定完Next[i]后,一定要判定N

hdu 1711 KMP算法模板题

题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串长度". 当发生失配的情况下,j的新值next[j]取决于模式串中T[0 ~ j-1]中前缀和后缀相等部分的长度, 而且next[j]恰好等于这个最大长度. 防止超时.注意一些细节.. 另外:尽量少用strlen.变量记录下来使用比較好,用字符数组而不用string //KMP算法模板题 //hdu