博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pku2752 Seek the Name, Seek the Fame
阅读量:7116 次
发布时间:2019-06-28

本文共 1004 字,大约阅读时间需要 3 分钟。

有一个长度不超过400000的字符串s,求满足{既是s的前缀,又是s的后缀}的字串,输出每一个串的起始位置。

首先,它本身满足条件,接下来,用KMP求出next数组,每次去掉next[i]到i的一段字符,剩余字串仍满足条件,直到找到头为止。

View Code
1 program pku2752(input,output);  2 var  3    s      : ansistring;  4    next      : array[0..500000] of longint;  5    answer : array[0..500000] of longint;  6    total  : longint;  7    i,j      : longint;  8 begin  9    while not eof do 10    begin 11       readln(s); 12       fillchar(next,sizeof(next),0); 13       j:=0; 14       next[1]:=0; 15       for i:=2 to length(s) do 16       begin 17      while (s[i]<>s[j+1])and(j>0) do 18         j:=next[j]; 19      if s[i]=s[j+1] then 20         inc(j); 21      next[i]:=j; 22       end; 23       total:=0; 24       i:=length(s); 25       while i<>0 do 26       begin 27      inc(total); 28      answer[total]:=i; 29      i:=next[i]; 30       end; 31       for i:=total downto 1 do 32      write(answer[i],' '); 33       writeln; 34    end; 35 end.

转载于:https://www.cnblogs.com/neverforget/archive/2012/03/23/2413852.html

你可能感兴趣的文章
Spark源码分析之Worker
查看>>
CAS与spring3集成
查看>>
XenCenter导出虚拟机
查看>>
Cookie禁用了Session还可以用吗?
查看>>
爱创课堂每日一题七十六天- 请解释什么是事件代理?
查看>>
购买SSL证书必须考虑的五大因素
查看>>
OpenStack简单测试性能监控数据记录
查看>>
多区域ospf在帧中继环境下实现互通-配置命令(请各位多指教)
查看>>
二次战CPP链表
查看>>
我的友情链接
查看>>
Linux 5.4 源码安装---http2.4.4安装
查看>>
使用数据泵导入并重命名表名
查看>>
Subversion的安装部署与用户验证配置
查看>>
基于glusterfs和gearman的离线任务运算分布式化方案介绍
查看>>
Autoit 自动化安装软件
查看>>
ubuntu中文出现乱码
查看>>
Javascript的console.log()用法
查看>>
创建git库
查看>>
数据库---数据库查询的各种子句
查看>>
细说浏览器特性检测(1)-jQuery1.4添加部分
查看>>