POJ_2752 Seek the Name, Seek the Fame 【KMP】
一、题目
二、分析
比较明显的KMP运用。
但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀。
可以不断的运用KMP得出答案,但是会超时。
寻找优化,发现答案在处理过的next数组中,因为题目中的条件就是前缀和后缀交集,那么前缀的串肯定与后缀的串相同,那么我们只需要改变长度继续分析就可以了。
三、AC代码
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 #include <set>
5 using namespace std;
6 const int maxn = 4e5 + 14;
7 char s[maxn];
8 int Next[maxn], Len;
9 int ans[maxn], cnt;
10
11 void get_next()
12 {
13 Next[0] = -1;
14 int i = 0, j = -1;
15 while(i <= Len)
16 {
17 if(j == -1 || s[i] == s[j])
18 {
19 i++;
20 j++;
21 Next[i] = j;
22 }
23 else
24 {
25 j = Next[j];
26 }
27 }
28 }
29
30 void solve()
31 {
32 get_next();
33 cnt = 0;
34 while(Len > 0)
35 {
36 ans[cnt++] = Len;
37 Len = Next[Len];
38 }
39 cnt--;
40 while(cnt > 0)
41 {
42 printf("%d ", ans[cnt--]);
43 }
44 printf("%d\n", ans[0]);
45 }
46
47 int main()
48 {
49 //freopen("input.txt", "r", stdin);
50 while(scanf("%s", s) != EOF)
51 {
52 Len = strlen(s);
53 solve();
54 }
55 return 0;
56 }
POJ_2752 Seek the Name, Seek the Fame 【KMP】的更多相关文章
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- poj2752seek the name, seek the fame【kmp】
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...
- POJ--2752--Seek the Name, Seek the Fame【KMP】
链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
随机推荐
- spring再学习之整合JDBC
一.JDBCTemplate JDBC模板 user类 package cn.itcast.bean; import java.util.Date; public class User { priva ...
- C# 类 (7) - 抽象 Abstract
Abstract 抽象类,关键字Abstract ,最典型的应用就是在 继承机制里 作为base类,抽象类是不能被实例化的(前面说的static 类也不能被实例化)它必须作为 基类,被别人继承,然后必 ...
- springboot( 三)redis demo
redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Red ...
- sdutoj2887
#include <stdio.h> #include <math.h> int main(){ int px,tx;double alpha; int T;scanf(&qu ...
- hdu2333-贪心,如何去后效性,背包太大怎么办,如何最大化最小值,从无序序列中发掘有序性质
补充一下我理解的中文题意.. 你要重新组装电脑..电脑有一些部件..你的预算有b,b(1~1e9),有n个部件..每个部件有类型和名称以及价钱和质量现在你要在不超过预算b的情况下..每个类型都买一个部 ...
- 浏览器缓存 All In One
浏览器缓存 All In One HTTP 缓存 强缓存 expired Cache-Control max-age s-maxage 协商缓存 E-tag last-modified 本地缓存 co ...
- HTML5 drag & drop & H5 DnD
HTML5 drag & drop H5 DnD https://html5demos.com/ demos https://html5demos.com/dnd-upload https:/ ...
- Axios 取消 Ajax 请求
Axios 取消 Ajax 请求 Axios XMLHttpRequest https://caniuse.com/?search=XMLHttpRequest https://developer.m ...
- MySQL 8.x
MySQL 8.x SQL & NoSQL $ mysql --version # mysql Ver 8.0.21 for osx10.15 on x86_64 (Homebrew) # M ...
- how to config custom process.env in node.js
how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...