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 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
题意就是问字符串s中有多少个长度不同的相同的前缀和后缀子串。
就是考察的一个KMP中的next数组,考察相同的前缀和后缀就是next数组,然后next数组跳转就可以了。
next[i]的意义就是:前面长度为i的字串的【前缀和后缀的最大匹配长度】
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 4e5+100;
char s[maxn];
int next[maxn];
void cal_next()
{
int k = -1;
next[0] = -1;
int n = strlen(s);
for(int i=1;i<n;i++)
{
while(k>-1 && s[i] != s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
}
int main()
{
while(scanf("%s",s) != EOF)
{
cal_next();
int len = strlen(s);
vector<int>ve;
int k = len-1;
ve.push_back(len);
while(next[k] != -1)
{
ve.push_back(next[k]+1);
k = next[k];
}
int n = ve.size();
for(int i=n-1;i>=0;i--)
{
printf("%d",ve[i]);
if(i != 0)
printf(" ");
}
printf("\n");
}
return 0;
}
POJ:2753-Seek the Name, Seek the Fame的更多相关文章
- POJ 2752:Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...
- POJ 2751:Seek the Name, Seek the Fame(Hash)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24077 Ac ...
- 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 ...
- 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 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- 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 ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- (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 ...
随机推荐
- CocoaPods 提交自己的库
今想把自己写的一个view提交到CocoaPods时候,突然发现pull request被拒了,原来从去年开始就改用trunk了... 网上那些folk 在提交的pull request的教程都不可用 ...
- 用户会话跟踪机制(session+cookie)
最近在优化之前给学校写的一个项目,发现了同一个浏览器(IE,Firefox)开多个选项卡的时候不能登录多个用户,后一个登录用户会把前一个用户给覆盖了,我的登录逻辑是把user对象存放到session中 ...
- 能挣钱的微信JSSDK+H5混合开发
H5喊了那么久,有些人都说不实用,有些人却利用在微信中开发H5应用赚得盆满钵满.微信JSSDK + HTML 5,让移动Web开发与微信结合轻而易举!跨平台.零成本,让大众创业变得更方便. 我觉得现在 ...
- [windows]win7设置wifi热点
1.启用并设定虚拟WiFi网卡:netsh wlan set hostednetwork mode=allow ssid=whylaughing key=124025621 2.开启无线wifi网络: ...
- 查询 request 对象的数据
在 EmpController 中调用 RequestInfoService ris = new RequestInfoService(); ris.saveRequestInfo(request); ...
- 重置Mysql的root密码及用户权限设置
一.重置Mysql的root密码 方法一: 直接进入localhost/phpmyadmin修改用户root的权限,设置密码: 方法二: 进入mysql控制台:mysql->use mysql ...
- Jenkins结合ant传递参数
需求: 使用Jenkins的「参数化构建过程」,由用户手动输入参数.通过ant脚本接收这个参数,并输出(当然,中间也可以进行复杂的处理,这里为了说明问题,仅做简单的输出). 1.基础环境 Jenkin ...
- 求矩阵的n次方 c语言实现
矩阵的n次方,比较容易理解的想法是递归. 思路是这样的,把n分成两部分,当n是偶数的时候,即为左右两边的乘积,如果n是奇数,即为左右两边的乘积再乘a ) matrixn())^*a else matr ...
- SQL 时间日期函数
1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到最近的秒小数部 ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.boot.system.JavaVersion
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.system.JavaVersion Invalid pro ...