一、题目

  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 }

最新文章

  1. Linux 操作mysql数据库 创建库 导入、删除表
  2. hibernate------java-delete-insert-update
  3. iOS技巧,宏定义
  4. Android 中SimpleDateFormat的使用注意
  5. B. Pasha and String
  6. 使用stringstream时的清空操作
  7. mysql dump 参数
  8. loadrunner解决浏览器死机问题
  9. Lvs+keepAlived实现负载均衡高可用集群(DR实现)
  10. CSS精心整理的面试题
  11. [转]MTK6252 11B添加模块、task实例
  12. hive 使用反射函数
  13. c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage
  14. InstallShield12的安装破解方法
  15. 小记SharePoint REST API Search和COM
  16. Spring MVC 中急速集成 Shiro 实践
  17. multiselect2side:jQuery多选列表框插件
  18. 搭建本地离线yum仓库
  19. 原!上线遇到的问题, java序列化关键字transient 修饰的属性变成null了
  20. 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

热门文章

  1. 操作系统 part2
  2. js load more select
  3. LVS : Linux Virtual Server 负载均衡,集群,高并发,robust
  4. 如何使用 js 实现相似图片搜索
  5. CSS3 &amp; transition &amp; animation
  6. Angular Routing
  7. svg opacity &amp; fill-opacity &amp; stroke-opacity
  8. Flutetr flutter_downloader 1.3.1
  9. Flutter: 使用相机拍照
  10. USDN稳定币应用区块链旅游业