POJ-2752-Seek the Name-kmp的变形
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5 题意:求出前缀后缀一样的对应字符的位置,按递增顺序输出
prefix-suffix:前后缀的意思。。。
//ababcababababcabab
#include<stdio.h>
#include<iostream>
#include<set>
#include<string.h>
using namespace std;
const int N=; int s[N];
char t[N];
int nextt[N]; void getnext(int len)//求的是模式串的next数组
{
int i=,j=-;
nextt[]=-;
while(i<len)
{
if(j<||t[i]==t[j])
nextt[++i]=++j;
else
j=nextt[j];
}
} int main()
{
while(~scanf("%s",t))
{
int len=strlen(t);
getnext(len);
// for(int i=0; i<len; i++)
// cout<<nextt[i]<<"**"<<endl;
int p=;
for(int i=len; i!=; )
{
s[p++]=nextt[i];
i=nextt[i];
}
p--;
for(int i=p-; i>=; i--)
printf("%d ",s[i]);
printf("%d\n",len);
memset(s,'\0',sizeof(s));
memset(t,,sizeof(t));
memset(nextt,'\0',sizeof(nextt));
}
return ;
}
POJ-2752-Seek the Name-kmp的变形的更多相关文章
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点. 分析:next数组的一种很巧妙的用法 next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等 所以就会 ...
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...
- POJ 2752 Seek the Name, Seek the Fame (KMP)
传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...
- 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: 14106 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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)
题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...
- POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)
题意: 给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀.从小到大输出所有符合要求的串的长度. 分析: 首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中 ...
随机推荐
- WindowsPowerShell常用命令
zai 获得Shell权限之后,可使用如下命令对系统进行文件操作: cd 后跟相应参数: cd ../ 返回上一级目录 cd +路径 跳转至制定目录(如果路径存在且正确的话) type flag.tx ...
- JavaScript: 变量提升和函数提升
第一篇文章中提到了变量的提升,所以今天就来介绍一下变量提升和函数提升.这个知识点可谓是老生常谈了,不过其中有些细节方面博主很想借此机会,好好总结一下. 今天主要介绍以下几点: 1. 变量提升 2. 函 ...
- Vue Router基础
路由 安装 vue-router 起步 <router-link to="/foo">Go to Foo</router-link> <router- ...
- 60 cuda全局性能优化
0 引言 cuda线程模型涉及grid的块划分和线程配置,直接影响到全局运算速度.根据文档<CUDA_C_Programming_Guide>,性能优化有三个方面的基本策略. (1)最大化 ...
- NetBeans简介和简单使用
1.什么是NetBeans? NetBeans IDE:可以使开发人员利用Java平台能够快速创建Web.企业.桌面以及移动的应用程序: 支持语言:PHP.Ruby.JavaScript.Groovy ...
- pure-ftpd 配置
# Disallow anonymous connections. Only allow authenticated users. NoAnonymous yes # If you want simp ...
- 20140408 父类指针指向子类对象 ;delete ;static作用
1.父类指针可以指向子类对象 静态联翩:如果以父类指针指向派生类对象,那么经由该指针只能访问父类定义的函数 动态联编:根据指针实际指向的对象类型确定 2.面试宝典 P110 面试题5 #includ ...
- 实时查询系统架构:spark流式处理+HBase+solr/ES查询
最近要做一个实时查询系统,初步协商后系统的框架 1.流式计算:数据都给spark 计算后放回HBase 2.查询:查询采用HBase+Solr/ES
- 5-MySQL高级-事务-回滚(3)
回滚 为了演示效果,需要打开两个终端窗口,使用同一个数据库,操作同一张表 step1:连接 终端1 select * from goods_cates; step2:增加数据 终端2:开启事务,插入数 ...
- HDU 5052 /// 树链剖分+线段树区间合并
题目大意: 给定n (表示树有n个结点) 接下来n行给定n个点的点权(在这个点上买鸡或者卖鸡的价钱就是点权) 接下来n-1行每行给定 x y 表示x结点和y结点之间有一条边 给定q (表示有q个询问) ...