一、题目

  POJ2752

二、分析

  比较明显的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】的更多相关文章

  1. POJ2752 Seek the Name, Seek the Fame 【KMP】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Ac ...

  2. 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 ...

  3. POJ--2752--Seek the Name, Seek the Fame【KMP】

    链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...

  4. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  5. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  6. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  8. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  9. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

随机推荐

  1. spring再学习之基本概念

    二.spring之IOC与DI 注入的方式: set方法注入: 构造方法注入: 字段注入: 注入类型: 值类型注入:8中基本类型 引用类型注入: BeanFaactory是原始接口:功能比较单一. A ...

  2. linux无需root挂载iso镜像文件

    引言 起初,我在针对deepin制作一款appimage安装工具,想要其实现的功能就是自动获取图标,只需要输入软件名称和分类即可,当然以后也会寻找方案省去手动输入的麻烦. 后来我发现一个有趣的问题 o ...

  3. Socket 编程简介

    Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. 本章节我们为大家接收 Perl ...

  4. codeforce 855B

    B. Marvolo Gaunt's Ring time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. vue template

    vue template <template> <div class="custom-class"> ... </div> </templ ...

  6. webpack 性能优化 dll 分包

    webpack 性能优化 dll 分包 html-webpack-externals-plugin DLLPlugin https://www.webpackjs.com/configuration/ ...

  7. ESLint & husky & git commit limit

    ESLint & husky & git commit limit 2 == error .eslintrc { "extends": "eslint-c ...

  8. AMP ⚡️原理

    AMP ️原理 AMP 是如何运作的 https://amp.dev/zh_cn/about/how-amp-works/ AMP 瞬时加载 结合了以下优化是 AMP 页面速度之快以至于它们可以瞬时加 ...

  9. react hooks & props change & pagination current bug

    react hooks & props change & pagination current bug multi tables & pigination bug & ...

  10. 新手如何通过SPC算力生态获得多重收益?

    DeFi市场在去年的一波又一波热潮之后,在今年余温有些褪去.而资本市场也将目光从DeFi市场中转移开来,他们将目光对准了新的市场,即算力市场.算力,其实从区块链技术在大范围普及以来,就是一个常见的话题 ...