博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2752 Seek the Name, Seek the Fame(next数组的运用)
阅读量:6283 次
发布时间:2019-06-22

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

题目链接:

Seek the Name, Seek the Fame
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12018   Accepted: 5906

Description

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 
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

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcababaaaaa

Sample Output

2 4 9 181 2 3 4 5

Source

,Zeyuan Zhu

题意:寻找前子串与后子串相等的子串。

代码例如以下:

#include 
#include
#include
#include
using namespace std;#define MAXN 1000017int next[MAXN];int ans[MAXN];int len;void getnext( char T[]){ int i = 0, j = -1; next[0] = -1; while(i < len) { if(j == -1 || T[i] == T[j]) { i++,j++; next[i] = j; } else j = next[j]; }}int main(){ char ss[MAXN]; int length; while(~scanf("%s",ss)) { len = strlen(ss); getnext(ss); int n = 0 ;int i = len; ans[0]=len; while(next[i] > 0 )//倒着扫描next数组 {//递归查找前子串和后子串相等的子串 i = next[i]; ans[++n] = i ; } for( i = n ; i >= 0 ; i--) printf("%d ",ans[i]); printf("\n"); } return 0;}

转载地址:http://gcxva.baihongyu.com/

你可能感兴趣的文章
onclick事件
查看>>
存储过程加密
查看>>
[再寄小读者之数学篇] (2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]一个秩等式)...
查看>>
hrbustoj 1179:下山(DFS+剪枝)
查看>>
C#进程启动实例
查看>>
Atitit .html5刮刮卡的gui实现总结
查看>>
android精品开源项目整理
查看>>
jQuery同步Ajax带来的UI线程阻塞问题及解决办法
查看>>
Python格式化输出
查看>>
mysql oracle静默 一键安装脚本
查看>>
微服务-分解应用程序从而实现更好的部署特性及可伸缩性
查看>>
mac 连接windows 共享内容
查看>>
GPS模块编程之NMEA0183协议
查看>>
Linux常用命令_(安装包管理)
查看>>
成都亚马逊AWSome Day回顾
查看>>
scaletype
查看>>
System.Runtime.InteropServices 命名空间
查看>>
浅谈百度司南大数据企业的风向标
查看>>
[原] Intellij IDEA开发Android,祝还在使用eclipse的早日脱离苦海
查看>>
在 NetBeans IDE 6.0 中分析 Java 应用程序性能
查看>>