Seek the Name, Seek the Fame

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 13619 Accepted: 6755

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

ababcababababcabab

aaaaa

Sample Output

2 4 9 18

1 2 3 4 5

题意是求一个字符串,前缀和后缀相等的有多少,分别由小到大输出其长度。

想到方法的时候觉得这题出得真是亮,之前学KMP算法的时候,没觉得next数组有那么多的用处,这回做了三道KMP题之后,也算是好好理解了next数组。

一个字符串s,长度为n。其中字符使用s[0],s[1],s[2]—-s[n-1]。

其中next[i]表示的含义是字符串s在位置i之前,有多少个 和字符串从头开始数 相等的数量,具体来说什么意思,就是比方说next[i]=k ,说明s[0]s[1]s[2]–s[k-1] = s[i-k] –s[i-3]s[i-2]s[i-1]。这个也是求next数组时候的方法,判断s[i]==s[j],之后i++,j++。最后赋值next[i]=j。所以next[i]实际表示的是在字符i之前的情况。

所以说我next[n]=k的话 (n是整个字符串的长度),表示的就是这个字符串里面最大有多少k个字符满足s[0]s[1]s[2]–s[k-1] = s[n-k] –s[n-3]s[n-2]s[n-1],而这不恰恰是所满足的其中一项吗?

求了这个next[n]=k之后,接下来要求的就是next[k]了,因为你都满足了s[0]s[1]s[2]–s[k-3]s[k-2]s[k-1] = s[n-k] --s[n-3]s[n-2]s[n-1]这个条件,即后缀字符串是相同的,那我只需缩小范围即可,缩小到s[0]–s[k-1],求next[k]即可。之后就以此类推,不断查询,知道next[]=0。因为题目要求从大到小输出,所以把每一个记录下来,再重新输出即可。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; char a[400005];
int next1[400005];
int len;
vector<int> result; void cal()
{
int i,j=-1;
next1[0]=-1;
for(i=0;i<len+1;)
{
if(j==-1||a[i]==a[j])
{
i++;
j++;
next1[i]=j;
}
else
{
j=next1[j];
}
} } int main()
{
while(cin>>a)
{
len = strlen(a);
cal(); int temp = len;
while(next1[temp])
{
result.push_back(next1[temp]);
temp=next1[temp];
}
sort(result.begin(),result.end()); int i;
for(i=0;i<result.size();i++)
{
cout<<result[i]<<" ";
}
cout<<len<<endl;
result.clear();
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2752:Seek the Name, Seek the Fame的更多相关文章

  1. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  2. 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 ...

  3. 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 ...

  4. (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 ...

  5. 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的简单应 ...

  6. 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 ...

  7. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

    Seek the Name, Seek the Fame Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 24000 ...

  8. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  9. POJ:2753-Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...

随机推荐

  1. Python 基础之循环结构 while

    一.while循环介绍 while 循环 可以提高代码的效率,减少代码的冗余 while 条件表达式:    code1    code2如果条件表达式成立,返回Ture,就执行其中的代码块 1.基本 ...

  2. AtCoder agc007_d Shik and Game

    洛谷题目页面传送门 & AtCoder题目页面传送门 有\(1\)根数轴,Shik初始在位置\(0\).数轴上有\(n\)只小熊,第\(i\)只在位置\(a_i\).Shik每秒可以向左移动\ ...

  3. iOS直播集成和问题总结(阿里云直播)

    https://www.jianshu.com/p/714ce954e628 最近接手公司的直播项目,对以前遗留的问题做处理和优化, 于是顺便看了下阿里云直播的文档,在下面写下对直播的理解和遇到的问题 ...

  4. iOS性能优化-内存优化

    https://blog.csdn.net/a184251289/article/details/82589128 2018年09月10日 14:25:31 xingshao1990 阅读数:328 ...

  5. pytorch神经网络解决回归问题(非常易懂)

    对于pytorch的深度学习框架,在建立人工神经网络时整体的步骤主要有以下四步: 1.载入原始数据 2.构建具体神经网络 3.进行数据的训练 4.数据测试和验证 pytorch神经网络的数据载入,以M ...

  6. 1.Neo4j简介(Neo4j系列)

    简介 Neo4j是一个高性能.高可靠性.可扩展.支持ACID事务的图数据库,它基本由Java语言实现,支持数据平台的平滑扩展和过渡,同时能够在多种系统上完成部署,它使用Cypher查询语言对数据进行增 ...

  7. Java服务端对Cookie的简单操作

    Java服务端对Cookie的简单操作 时间 2016-04-07 10:39:44 极客头条 原文  http://www.cuiyongzhi.com/index.php/post/15.html ...

  8. Verilog有符号整型数(signed int)比大小

    本文参考了https://blog.csdn.net/wenxinwukui234/article/details/42119265/ 关于2进制补码的思考和讨论. ================= ...

  9. js左右选项移动

    <!--网页代码--><div class="modal" id="modal-primary7"> <div class=&qu ...

  10. 吴裕雄--天生自然JAVA数据库编程:处理大数据对象

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...