HDU 4763 Theme Section

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4763

-------------------------------------------------------------------------------------------

数据范围卡了$SA$但数据却没能构造到卡一些理论复杂度更高的方法

这里说一下比较靠谱的$exkmp$的做法

根据$exkmp$的$next$数组定义

我们可以用$O(n)$的时间得到一个后缀与整个串的最长公共前缀的长度

然后我们就可以根据$next[n - i + 1]$与$i$是否相等判断前后缀是否相等

而对于中间的部分 我们知道如果 $next[i + 1]$到$next[n - i]$中的最大值不小于$i$

那么$i$即为一个可行解

但此处直接扫描的话复杂度显然不够 再观察可以发现如果我们把答案从大到小枚举

那么这个取最大值区间的范围是逐渐增大的 于是均摊下来的复杂度还是线性的

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 1e6 + 10;
 7 char s[N];
 8 int nexte[N];
 9 int t, ans, n;
10 int main()
11 {
12     scanf("%d", &t);
13     while(t--)
14     {
15         scanf("%s", s + 1);
16         n = strlen(s + 1);
17         if(n <= 2)
18         {
19             puts("0");
20             continue;
21         }
22         nexte[1] = n;
23         int id = 2, p = 1;
24         while(p < n && s[p] == s[p + 1])
25             ++p;
26         nexte[2] = p - 1;
27         for(int i = 3; i <= n; ++i)
28         {
29             int l = nexte[i - id + 1];
30             if(i + l <= p)
31                 nexte[i] = l;
32             else
33             {
34                 p = max(p, i - 1);
35                 while(p < n && s[p - i + 2] == s[p + 1])
36                     ++p;
37                 nexte[i] = p - i + 1;
38                 id = i;
39             }
40         }
41         int len = n / 3, mx = 0;
42         for(int i = len + 1; i <= n - len; ++i)
43             mx = max(mx, nexte[i]);
44         ans = 0;
45         for(int i = len; i; --i)
46         {
47             if(nexte[n - i + 1] == i && mx >= i)
48             {
49                 ans = i;
50                 break;
51             }
52             mx = max(mx, nexte[i]);
53             mx = max(mx, nexte[n - i + 1]);
54         }
55         printf("%d\n", ans);
56     }
57     return 0;
58 }
时间: 2024-08-07 04:54:48

HDU 4763 Theme Section的相关文章

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 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的next数组的应用)

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

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

【HDOJ 4763】 Theme Section (KMP+strstr)

[HDOJ 4763] Theme Section Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1999    Accepted Submission(s): 947 Problem Description It's time for music! A lot of popular musicians a

hdu4763 Theme Section

地址: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): 3491    Accepted Submission(s): 1623 Problem Description It's time for music!

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

【HDU 4763】Theme Section(KMP)

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

Theme Section HDU - 4763(些许暴力)

题意: 求出最长公共前后缀 不能重叠  而且 这个前后缀 在串的中间也要出现一次 解析: 再明确一次next数组的意思:完全匹配的最长前后缀长度 求一遍next 然后暴力枚举就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set>