POJ_2752 Seek the Name, Seek the Fame 【KMP】
2024-09-08 05:58:45
一、题目
二、分析
比较明显的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 }
最新文章
- Linux 操作mysql数据库 创建库 导入、删除表
- hibernate------java-delete-insert-update
- iOS技巧,宏定义
- Android 中SimpleDateFormat的使用注意
- B. Pasha and String
- 使用stringstream时的清空操作
- mysql dump 参数
- loadrunner解决浏览器死机问题
- Lvs+keepAlived实现负载均衡高可用集群(DR实现)
- CSS精心整理的面试题
- [转]MTK6252 11B添加模块、task实例
- hive 使用反射函数
- c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage
- InstallShield12的安装破解方法
- 小记SharePoint REST API Search和COM
- Spring MVC 中急速集成 Shiro 实践
- multiselect2side:jQuery多选列表框插件
- 搭建本地离线yum仓库
- 原!上线遇到的问题, java序列化关键字transient 修饰的属性变成null了
- 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
热门文章
- 操作系统 part2
- js load more select
- LVS : Linux Virtual Server 负载均衡,集群,高并发,robust
- 如何使用 js 实现相似图片搜索
- CSS3 &; transition &; animation
- Angular Routing
- svg opacity &; fill-opacity &; stroke-opacity
- Flutetr flutter_downloader 1.3.1
- Flutter: 使用相机拍照
- USDN稳定币应用区块链旅游业