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. 一步一步配置docker(tomcat+jenkins+phpmyadmin+nginx)

    经过半个月的docker学习实践,今天对自己的学习成果做个总结. 貌似官方推荐的是docker compose使用DockerFile 来配置,但目前还没学习使用docker compose,先学习通 ...

  2. Day11 - M - Nim or not Nim? HDU - 3032

    Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...

  3. Python 之网络编程之进程总体概要

     一: 进程的概念:(Process) 进程就是正在运行的程序,它是操作系统中,资源分配的最小单位. 资源分配:分配的是cpu和内存等物理资源 进程号是进程的唯一标识 同一个程序执行两次之后是两个进程 ...

  4. vs code 批量替换

    源内容 .icon-user, .icon-people, .icon-user-female, .icon-user-follow, .icon-user-following, .icon-user ...

  5. Flutter | 状态管理特别篇——Provide

    前言 今天偶然发现在谷歌爸爸的仓库下出现了一个叫做flutter-provide的状态管理框架,2月8日才第一次提交,非常新鲜.在简单上手之后感觉就是一个字--爽!所以今天就跟大家分享一下这个新的状态 ...

  6. OPC DA通讯 KEP6.4 DCOM 配置脚本

    在OPC DA通讯测试时总是避免不了要配置DCOM,我习惯是在虚拟机中装这些通讯测试软件,每次安装都需要再次手动配置,感觉很是麻烦 因此,查找资料,了解到可以用dcomperm这个软件来在命令行下进行 ...

  7. CyclicBarrier 解读

    简介 字面上的意思: 可循环利用的屏障. 作用: 让所有线程都等待完成后再继续下一步行动. 举例模拟: 吃饭人没到齐不准动筷. 使用Demo package com.ronnie; import ja ...

  8. SpingBoot学习(一)

    一.概述 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置. 简单来说,它提供了一堆依赖打包, ...

  9. 用于云计算的自我更新、自我修补的Linux版本已发布!

    自动化是 IT 行业的增长趋势,其目的是消除重复任务中的手动干扰.Oracle 通过推出 Oracle Autonomous Linux 向自动化世界迈出了又一步,这无疑将使 IoT 和云计算行业受益 ...

  10. java读取ini文件

    ini工具类; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import j ...