POJ2752【KMP-next数组含义】
题意:
给一个字符串,求满足既是这个字符串的前缀,又是这个字符串的后缀,从小到大输出长度
思路:
细讲next数组含义博文:点我
首先要满足前缀的呀。
KMP的next数组干的是子串最长后缀。
所以从最后一个next一直往前跳,next存的就是最长后缀,知道为0||-1;
//#include<bits/stdc++.h>
#include<cstdio>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL; const int N=4e5+10;
int Next[N],len;
char s[N];
//ababcababababcabab
//2 4 9 18
void GetNext()
{
int i=0,j=-1;
Next[0]=-1;
while(i<len)
{
if(j==-1||s[i]==s[j])
Next[++i]=++j;
else
j=Next[j];
}
} int ans[N],num;
int main()
{
while(~scanf("%s",s))
{
len=strlen(s);
GetNext();
int x=len;
num=0;
while(Next[x]!=0&&Next[x]!=-1)
{
ans[num++]=Next[x];
x=Next[x];
}
sort(ans,ans+num);
for(int i=0;i<num;i++)
printf("%d ",ans[i]);
printf("%d\n",len);
}
return 0;
}
POJ2752【KMP-next数组含义】的更多相关文章
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- POJ-2752(KMP算法+前缀数组的应用)
Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...
- POJ2406Power Strings (最小循环节)(KMP||后缀数组)
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串
题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- kmp next数组的理解(挺好的一篇文章 ,原来kmp最初的next是这样的啊,很好理解)
KMP算法的next[]数组通俗解释 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
随机推荐
- commons.cli.jar 作用
对命令行进行处理的jar包.处理的步骤主要包括定义.分析和询问.(There are three stages to command line processing. They are the def ...
- ASP.Net MVC upload file with record & validation - Step 6
Uploading file with other model data, validate model & file before uploading by using DataAnnota ...
- JS/PHP字符串截取
<script> var str="首都医科大学附属北京同仁医院-156"; var index = str.indexOf('-');//获取-的索引值,从0开始算, ...
- 设置netbeans文件编码格式
在项目ecmall上右键 选择属性,然后在项目属性里设置
- linux socket详解
1 linux socket编程的固定模式 server端,bind.listen.accept client端,connect client端和server端之间的一次通信: client端,wri ...
- r testifying that your code will behave as you intend.
https://github.com/stretchr/testify Testify - Thou Shalt Write Tests Go code (golang) set of pack ...
- Memcached中的存取命令详解
本文和大家分享的主要是Memcached中常用的一些存取命令相关用法,一起来看看吧,希望对大家学习Memcached有所帮互助. 存储命令 set:不管key存在与否,强制进行set操作: add:必 ...
- STL之队列的运用
卡片游戏:非常好地介绍了队列的特点和应用 桌上有一叠牌,从第一张牌開始从上往下依次编号1~n.当至少还剩两张牌时进行例如以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后. 输入n,输出每次 ...
- Window 64位下的客户机配置PLSQL链接远程Oracle
此文章记录的是艰难探索. 完成如下工作: 服务器A为Windows Serve 2016:安装Oracle. 客户机B为Win7 x64位,安装PLSQLDevelop,链接A上的Oracle. 首先 ...
- tflearn数据预处理
#I just added a function for custom data preprocessing, you can use it as: minmax_scaler = sklearn.p ...