求所有前缀-后缀字符串相等的长度(kmp算法中的nxet数组)
2016-08-09 20:46
441 查看
Seek the Name, Seek the Fame
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
Sample Output
思路:利用next数组求解
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int Max=400005;
char str[Max];
int l,next[Max],ans[Max];
void get_next()
{
int i=0,j=-1;
next[0]=-1;
while(i<l)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(~scanf("%s",str))
{
l=strlen(str);
get_next();
ans[0]=l;
// for(int i=0;i<=l;i++)
// printf("%d ",next[i]);
// printf("\n");
int n=0,i=l;
while(next[i]>0)
{
ans[++n]=next[i];
i=next[i];
}
for(i=n;i>=0;i--)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17086 | Accepted: 8715 |
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 181 2 3 4 5
思路:利用next数组求解
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int Max=400005;
char str[Max];
int l,next[Max],ans[Max];
void get_next()
{
int i=0,j=-1;
next[0]=-1;
while(i<l)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(~scanf("%s",str))
{
l=strlen(str);
get_next();
ans[0]=l;
// for(int i=0;i<=l;i++)
// printf("%d ",next[i]);
// printf("\n");
int n=0,i=l;
while(next[i]>0)
{
ans[++n]=next[i];
i=next[i];
}
for(i=n;i>=0;i--)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- 程序员面试宝典:输入一行字符串,找出其中出现的相同且长度最长的字符串----后缀数组求解
- 字符串相关处理kmp,前缀数,后缀树,后缀数组,最长回文串,最长重复字串,最长非重复字串
- 求出长度为1, 2, 3, 4, 5....的字符串最大出现次数 后缀数组 UVA 11855 - Buzzwords
- poj_2752_kmp_nxt_application_求s 所有子串是s的前缀又是后缀的长度
- Shell 数组元素个数${#array[@]} 数组的所有元素${array[*]} 字符串长度${#str}
- 字符串相关处理kmp,前缀数,后缀树,后缀数组,最长回文串,最长重复字串,最长非重复字串
- 【二分查找】找出两个长度相等的数组中所有值的 “上中位数”
- 找出第二个文本抄袭第一个文本的所有位置和长度 后缀数组 UVA 10526 - Intellectual Property
- poj 2752 Seek the Name, Seek the Fame 【在原串中 找所有既是原串前缀又是原串后缀的子串长度】
- poj 3294 Life Forms(n个字符串中 求公共子串长度超过k得最大子串 后缀数组)
- (字符串的模式匹配4.7.19——前缀数组suffix的应用)POJ 2752 Seek the Name, Seek the Fame(求解一个字符串中前缀和后缀一样的位置)
- HDU 4436 str2int(后缀数组,一种统计n个digit字符串所有不同子串之和的方法)
- 利用后缀数组找字符串中相同且长度最长的字符串
- Hdu 5782 Cycle(给你两个长度相同的字符串,对于长度为i的前缀,如果这两个字符串的前缀是循环相等的,那么输出1,否则输出0)
- Shell 数组元素个数${#array[@]} 数组的所有元素${array[*]} 字符串长度${#str}
- HDU 4436 str2int 后缀数组 字符串哈希 前缀和
- (Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度)
- 2752 Seek the Name, Seek the Fame KMP 找出字符串中所有前缀-后缀的字符串
- Shell 数组元素个数${#array[@]} 数组的所有元素${array[*]} 字符串长度${#str}
- leetcode_14. Longest Common Prefix 求字符串数组中所有字符串的最长前缀