POJ 3974 Palindrome(manacher)
2016-06-03 09:49
302 查看
思路:manacher模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 3e6;
int p[maxn];
char s[maxn],str[maxn];
int len1,len2,ans;
void init()
{
str[0]='$';
str[1]='#';
for(int i = 0;i<len1;i++)
str[i*2+2]=s[i],str[i*2+3]='#';
len2 = len1*2+2;
str[len2]='*';
}
void manacher()
{
int id = 0,mx = 0;
for(int i = 1;i<len2;i++)
{
if(mx>i)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
for(;str[i+p[i]]==str[i-p[i]];p[i]++);
if (p[i]+i > mx)
{
mx = p[i]+i;
id = i;
}
}
}
int main()
{
int cas = 1;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"END")==0)
break;
printf("Case %d: ",cas++);
len1 = strlen(s);
init();
ans = 0;
manacher();
for(int i = 0;i<len2;i++)
ans = max(ans,p[i]);
cout << ans -1 << endl;
}
}
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.
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
Sample Output
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 3e6;
int p[maxn];
char s[maxn],str[maxn];
int len1,len2,ans;
void init()
{
str[0]='$';
str[1]='#';
for(int i = 0;i<len1;i++)
str[i*2+2]=s[i],str[i*2+3]='#';
len2 = len1*2+2;
str[len2]='*';
}
void manacher()
{
int id = 0,mx = 0;
for(int i = 1;i<len2;i++)
{
if(mx>i)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
for(;str[i+p[i]]==str[i-p[i]];p[i]++);
if (p[i]+i > mx)
{
mx = p[i]+i;
id = i;
}
}
}
int main()
{
int cas = 1;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"END")==0)
break;
printf("Case %d: ",cas++);
len1 = strlen(s);
init();
ans = 0;
manacher();
for(int i = 0;i<len2;i++)
ans = max(ans,p[i]);
cout << ans -1 << endl;
}
}
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.
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
相关文章推荐
- pl/sql创建Database links
- linux下收集系统全面信息(基于sosreport以及supportconfig)
- 文本挖掘常用评价方法讲解
- 两个html之间传值
- 345. Reverse Vowels of a String
- Dom4j-操作节点和属性及写入其他xml文件
- R回归诊断广义线性模型非线性模型
- ubuntu eclipse正常显示XML文件
- 批量修改rrd文件配置
- 软件工程课程建议
- Swift iOS中的Nullability Annotations
- Linux常用命令及工具
- Titanic challenge on Kaggle with decision trees (party) and SVMs (kernlab)
- hdu 3068 最长回文(manacher)
- 实习入职第十四天:怎么从view里面跳转到activity中,再把数据返回来
- 自定义控件
- Google 官方 侧滑 drawerlayout
- 各种测试方式总结
- asp.net 使用Application和Session对象统计在线人数
- Js类的静态方法与实例方法区分及jQuery拓展的两种方法