POJ 2752(不满足P[i]<>P[next[i]] 的next函数)
来源:互联网 发布:node 删除文件夹 编辑:程序博客网 时间:2024/04/29 20:55
Language:
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8682 Accepted: 4112
Description
给定一个字符串P,求它所有的满足P[1..i]到P[n-i+1..n]的i.
Input
数据有若干行
每行为一组数据P,1 <= Length of P <= 400000.
Output
每行一组数据,按数字升序输出所有的i.
Sample Input
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5
Source
POJ Monthly--2006.01.22,Zeyuan Zhu
要考虑KMP中Next函数的性质-P[next[i]]<>P[i]
如果这个条件不满足呢?
先在字符串后插入一个'.',表示'.'前的循环,这样就能规避这一点(因为‘.’不会与任何字母重合)
再把后面那个if从句删掉(它只能跳过一些-非全部的-P[next[i]]<>P[i]的情况,因为.后面可能出现两次'a'
这样就可以了,最后把答案(由于不考虑a[i],只考虑前面循环节的长度,故-1)
Program SeekName;const maxn=400000;var n,i,j,size:longint; a:ansistring; ans,next:array[1..maxn] of longint;begin while not seekeof do begin readln(a); a:=a+'.'; n:=length(a); j:=0;next[1]:=0;i:=1; while (i<n) do begin if (j=0) or (a[i]=a[j]) then begin inc(i); inc(j); // if (a[i]<>a[j]) then next[i]:=j else next[i]:=next[j]; next[i]:=j; end else j:=next[j]; end; size:=0; j:=n; repeat inc(size); ans[size]:=j-1; j:=next[j]; until j<=1; write(ans[size]); for i:=size-1 downto 1 do write(' ',ans[i]); writeln; end;end.
- POJ 2752(不满足P[i]<>P[next[i]] 的next函数)
- 关于p->next=p;和p=p->next;的刨根问底(也有转的内容)
- p=p->next
- p++ 与 p->next的区别
- POJ 1961 字符串 KMP (i-next[i])
- POJ 2752、2406、1961 KMP的next[](或p[])简单应用
- 有关head->next = p;和p=head->next;之间的区别
- *(p+i)【 i为位移量 】
- poj 2256 P U I 字符串
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- i++与++i//*p++与*++p
- UVa 514 - Rails
- x264中I,P,B帧和PTS,DTS的关系
- database and os was hang with ORA-240 ERROR(bug6270137,10207611 )
- Eclipse安装插件Properties Editor
- 启动MySQL 监视器(Monitor) 【数据库高效编程 - 学习笔记 第三章】
- POJ 2752(不满足P[i]<>P[next[i]] 的next函数)
- Java分页 pager_taglib标签库分页
- UVa 673 - Parentheses Balance
- Android开发获取ImageView中的图片
- 获得传入日期的下一天
- 修复 artDialog 双击遮罩层就会自动关闭的bug.... 自己动手....
- Oracle drop user xxx cascade
- Linux curl使用简单介绍
- System.arraycopt(Object src,int srcPos,Object dest,int destPos,int length)