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. [CEOI 2004]锯木厂选址

    Description 题库链接 从山顶上到山底下沿着一条直线种植了 \(n\) 棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂. 木材只能朝山下运.山脚下有一个 ...

  2. 46 求1+2+3+...+n 静态成员函数和静态变量

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路: 1)使用构造函数的方法,需要使用sta ...

  3. intelliJ IDEA 全屏键盘手

    从MyEclipse到IntelliJ IDEA --让你脱键盘,全键盘操作 从MyEclipse转战到IntelliJ IDEA的经历 我一个朋友写了一篇"从Eclipse到Android ...

  4. 「JLOI2011」飞行路线

    前言 看到好多大佬都在跑分层图最短路,\(\text{DP}\) 解法的我瑟瑟发抖... 题目描述 给定一张 \(N\) 个点(点编号从 \(0\) 到 \(N-1\)),\(M\) 条边的无向带权图 ...

  5. 125、Java面向对象之引用传递实例三,int类型按值传递

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. python读取文件用b模式读取

    f = open('aaa','rb')    返回的是字节 字符串编码 python中所有的字符串编码为Unicode,如果从一个文件读取字符串,那么该字符串的编码就是该文件的编码. f.tell( ...

  7. 【剑指Offer面试编程题】题目1524:复杂链表的复制--九度OJ

    题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点). 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第 ...

  8. react基础总结

    React的特点: 1.声明式: 可以声明式的在js中写html结构: 注意: react是用很像 js 的语言写标签; const jsx = <div className="app ...

  9. Manjro i3 桌面 添加输入法 及无声音配置方法(This sound device does not have any capture controls.问题)

    一.i3桌面添加输入法 1.把配置写在 /etc/environment中 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export XM ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:缩略图功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...