POJ 2752 Seek the Name, Seek the Fame next数组理解加深
题意:给你一个字符串,寻找前缀和后缀相同的子串(包括原串)、 从小到大排列输出其子串的长度
思路:KMP next 数组应用、
其实就是一个数学推导过程、
首先由next数组 可知s(ab) = s(bd) 此时next[d]=b 而此时 next[b]=f,意味着s(ef)=s(gh)
s(gh)属于s(ab) s(ab)=s(bd); 所以可以推出 s(gh)属于s(bd) 且s(ef)=s(gh) 所以 s(ef)属于s(cd)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
const int qq=400000+10;
char node[qq];
int next[qq];
int get[qq];
int len;
void getnext()
{
int i,j;
i=0;j=-1;
next[0]=-1;
while(i<len){
if(j==-1||node[i]==node[j])
next[++i]=++j;
else
j=next[j];
}
return;
}
int main()
{
while(~scanf("%s",node)){
len=strlen(node);
getnext();
int k=len;
int i=0;
while(next[k]!=0){ // 那个公式的推导体现在这里、
get[i++]=next[k];
k=next[k];
}
for(int j=i-1;j>=0;--j)
printf("%d ",get[j]);
printf("%d\n",len);
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame 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 ...
- 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 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 ...
随机推荐
- reverse 的用法
直接对数组或是数据结构使用 #include<bits/stdc++.h> using namespace std; ]={,,,,,};//申请6个元素,下标从0开始,最后一个下标是5 ...
- 基于keras中IMDB的文本分类 demo
本次demo主题是使用keras对IMDB影评进行文本分类: import tensorflow as tf from tensorflow import keras import numpy a ...
- Ubuntu卸载通过apt-get命令安装的软件
卸载一个已安装的软件包(删除配置文件): apt-get --purge remove packagename
- 前端如何实现图片懒加载(lazyload) 提高用户体验
定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力. 惰性加载是程序人性化的一种体现,提高用户体验,防 ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...
- 【51NOD1304】字符串的相似度
题目描述 我们定义2个字符串的相似度等于两个串的相同前缀的长度.例如 "abc" 同 "abd" 的相似度为2,"aaa" 同 " ...
- Leetcode821.Shortest Distance to a Character字符的最短距离
给定一个字符串 S 和一个字符 C.返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组. 示例 1: 输入: S = "loveleetcode", C ...
- More Effective C++: 05技术(29)
29:引用计数 本章首先实现一个带引用计数String,然后逐步优化,介绍引用计数的常规实现. 实现引用计数的String,首先需要考虑:引用计数在哪存储.这个地方不能在String对象内部,因为需要 ...
- RNN与 LSTM 网络
循环神经网络(RNN) 人们的每次思考并不都是从零开始的.比如说你在阅读这篇文章时,你基于对前面的文字的理解来理解你目前阅读到的文字,而不是每读到一个文字时,都抛弃掉前面的思考,从头开始.你的记忆是有 ...
- python系列之(2)PyQuery的用法
1.了解 pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好. 2.安装 pip install pyquery 3引用 ...