最长回文
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11771 Accepted Submission(s): 4315
Problem Description
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
回文就是正反读都是一样的字符串,如aba, abba等
Input
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
Output
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
Sample Input
aaaa abab
Sample Output
4 3
Source
思路:Manacher算法模板;
对于算法的学习请点击:>>Manacher<<
转载请注明出处:寻找&星空の孩子
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 110010*2
int P[maxn];
//(p.s. 可以看出,P[i]-1正好是原字符串中回文串的总长度)
char s1[maxn];
char s2[maxn];
void manacher(char* s)
{
int i,id=0,mx=0;
P[0]=0; //P[0]位置没用
for(i=1;s[i];i++) //对串进行线性扫描
{
if(mx > i) //如果mx比当前i大,分为两种情况,详细致请看文章开头推荐的blog上的图示,非常给力的图
P[i] = min(P[2*id-i],mx-i);
else //如果mx比i小,没有可以利用的信息,那么就只能从头开始匹配
P[i] = 1;
while(s[i+P[i]]==s[i-P[i]] )P[i]++; //匹配
if(mx < P[i] + i) //坚持是否有更新mx以及id
{
mx = P[i] + i;
id = i;
}
}
}
void init()
{
int i, j = 2;
s2[0] = '$', s2[1] = '#';
for(i=0;s1[i];i++)
{
s2[j++] = s1[i];
s2[j++] = '#';
}
s2[j] = '\0';
}
int main()
{
while(scanf("%s",s1)!=EOF)
{
init();
manacher(s2);
int ans=0;
for(int i=1;s2[i]!='\0';i++)
{
ans=max(ans,P[i]);
}
printf("%d\n",ans-1);
}
return 0;
}
Palindrome
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 5868 | Accepted: 2128 |
Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcba abacacbaaaab END
Sample Output
Case 1: 13 Case 2: 6
Source
题目链接:http://poj.org/problem?id=3974
同上题一样。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 1000010*2
int P[maxn];
//(p.s. 可以看出,P[i]-1正好是原字符串中回文串的总长度)
char s1[maxn];
char s2[maxn];
void manacher(char* s)
{
int i,id=0,mx=0;
P[0]=0; //P[0]位置没用
for(i=1;s[i];i++) //对串进行线性扫描
{
if(mx > i) //如果mx比当前i大,分为两种情况,详细致请看文章开头推荐的blog上的图示,非常给力的图
P[i] = min(P[2*id-i],mx-i);
else //如果mx比i小,没有可以利用的信息,那么就只能从头开始匹配
P[i] = 1;
while(s[i+P[i]]==s[i-P[i]] )P[i]++; //匹配
if(mx < P[i] + i) //坚持是否有更新mx以及id
{
mx = P[i] + i;
id = i;
}
}
}
void init()
{
int i, j = 2;
s2[0] = '$', s2[1] = '#';
for(i=0;s1[i];i++)
{
s2[j++] = s1[i];
s2[j++] = '#';
}
s2[j] = '\0';
}
int main()
{
int ca=1;
while(scanf("%s",s1)!=EOF)
{
if(strcmp(s1,"END")==0) return 0;
init();
manacher(s2);
int ans=0;
for(int i=1;s2[i]!='\0';i++)
{
ans=max(ans,P[i]);
}
printf("Case %d: %d\n",ca++,ans-1);
}
return 0;
}
/*
abcbabcbabcba
abacacbaaaab
END
*/