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 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
随机推荐
- codeforces 875B
B. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- Springboot 基本认识
不管是 spring cloud alibaba 还是 spring cloud netflix,都 是基于 springboot 这个微框架来构建的,所以我希望花一 点时间来讲一下 springbo ...
- POJ 2288 Islands and Bridges(状压DP)题解
题意:n个点,m有向边,w[i]表示i的价值,求价值最大的哈密顿图(只经过所有点一次).价值为:所有点的w之和,加上,每条边的价值 = w[i] * w[j],加上,如果连续的三个点相互连接的价值 = ...
- React Hooks: useMemo All In One
React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...
- fullstack web projects in action
fullstack web projects in action web 全栈项目实战 Angular 全栈 Angular + TypeScript + Nest.js + PostgreSQL + ...
- Sketch & UI & PS
Sketch & UI & PS app ui https://sketchapp.com/learn https://www.sketch.com/docs/ https://ske ...
- how to change sketch language to chinese
how to change sketch language to Chinese https://www.sketch.com/support/troubleshooting/chinese-loca ...
- moment.js & convert timestamps to date string in js
moment.js & convert timestamps to date string in js https://momentjs.com/ moment().format('YYYY- ...
- html fragment & html template & virtual DOM & web components
html fragment & html template & virtual DOM https://developer.mozilla.org/en-US/docs/Web/API ...
- NGK项目是一个怎样的项目?区块链里算是有前景的吗?
牛市时,项目被众星捧月,优点被无限放大,缺点无限被掩盖:而当市场开始下行时,之前的赞美则变成了贬低.所以了解项目不能盲目跟风,需要有独立的思考.对于近期引起社区讨论的NGK项目,以它为例,今天就来给大 ...