【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)
题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度。
解法:利用KMP中next[ ]数组的性质,依次找到前缀、后缀匹配的字符串。
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 using namespace std;
6
7 const int N=400010;
8 int next[N],a[N];
9 int n;
10 char s[N];
11
12 void kmp()
13 {
14 memset(next,0,sizeof(next));
15 int p=0;
16 next[1]=0;
17 for (int i=2;i<=n;i++)
18 {
19 while (s[i]!=s[p+1] && p) p=next[p];
20 if (s[i]==s[p+1]) p++;
21 next[i]=p;
22 }
23 }
24 int main()
25 {
26 while (scanf("%s",s+1)!=EOF)
27 {
28 n=strlen(s+1);
29 kmp();
30 int t=0;
31 for (int i=n;i;i=next[i])
32 a[++t]=i;
33 for (int i=t;i>=1;i--)
34 printf("%d ",a[i]);
35 printf("\n");
36 }
37 return 0;
38 }
【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)的更多相关文章
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- POJ 2752:Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...
随机推荐
- 常用 .gitignore 模板
前言 每次建项目的时候可以直接复制了,也算是方便自己,以后发现少的会更新 正文 作用 git提交时忽略文件 文件名 .gitignore Python # Byte-compiled / optimi ...
- Tengine 四层代理:
Tengine 四层代理: 1 ) 安装tengine ( nginx1.9 以上版本 编译以后要支持stream 模块) 1.1 ) tengine(nginx) 一定要是nginx-1.9.X 以 ...
- python模块详解 | unittest(单元测试框架)(持续更新中)
目录: why unittest? unittest的四个重要概念 加载测试用例的三个方法 自动加载测试用例 忽略测试和预期失败 生成html测试报告 why unittest? 简介: Unitte ...
- 【Oracle】静默安装oracle 11.2.0.4 超详细
安装oracle 1.执行脚本完成初始化oracle环境 2.解压缩oracle的压缩包,单实例1个,rac是2两个压缩包 3.修改response下的db_install.rsp 修改内容如下: - ...
- leetcode 730. 统计不同回文子序列(区间dp,字符串)
题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...
- centos7安装宝塔面板
在终端下执行如下命令 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.s ...
- 一文读懂 TKE 及 Kubernetes 访问权限控制
你有了解过Kubernetes的认证授权链路吗?是否对TKE的权限控制CAM策略.服务角色傻傻分不清楚?本文将会向你介绍腾讯云TKE平台侧的访问控制.Kubernetes访问控制链路,以及演示如何将平 ...
- Java并发组件三之Semaphore
使用场景:常用于使用有限的资源,限制线程并发的最大数量.默认情况下,信号量是非公平性的(先等待先执行为公平.类似于买东西的时候大家排队付款,先来的先付款是公平的.但是这时候有人插队,那就是非公平的)设 ...
- gradles理解和文件配置
gradle gradle tasks :查看所有的taske命令 bootJar:打包,讲项目的所有依赖和主工程代码打包,一个可直接执行的一个包,不需要tomcat运行 gradle使用bootja ...
- 深入理解 MySQL 索引底层原理
https://mp.weixin.qq.com/s/qHJiTjpvDikFcdl9SRL97Q