POJ 3974 Palindrome

时间:2022-12-19 14:39:40
D - Palindrome

Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

求最长回文串的长度。

(manacher算法)

#include<cstdio>
#include<cstring>
#include<iostream>
#define m(s) memset(s,0,sizeof s);
using namespace std;
const int N=1e6+;
int l,cas,len,p[N<<];
char s[N],S[N<<];
void manacher(){
int ans=,id=,mx=-;
for(int i=;i<l;i++){
if(id+mx>i) p[i]=min(p[id*-i],id+mx-i);
while(i-p[i]->=&&i+p[i]+<=l&&S[i-p[i]-]==S[i+p[i]+]) p[i]++;
if(id+mx<i+p[i]) id=i,mx=p[i];
ans=max(ans,p[i]);
}
printf("Case %d: %d\n",++cas,ans);
}
int main(){
while(scanf("%s",s)==){
if(s[]=='E') break;
len=strlen(s);m(p);m(S);
l=-;
for(int i=;i<len;i++) S[++l]='#',S[++l]=s[i];
S[++l]='#';
manacher();
}
return ;
}

//====================================================

//hash有点慢
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef int i64;
const int N=1e6+;
int n,m,cas,ans,a[N<<];char s[N];
i64 P,pow[N<<],hash_l[N<<],hash_r[N<<];
void get_hash(){
pow[]=;hash_r[]=hash_l[m+]=;
for(int i=;i<=m;i++) pow[i]=pow[i-]*P;
for(int i=;i<=m;i++) hash_r[i]=hash_r[i-]*P+a[i];
for(int i=m;i>=;i--) hash_l[i]=hash_l[i+]*P+a[i]; }
int main(){
P=;
while(scanf("%s",s+)==){
if(s[]=='E') break;
n=strlen(s+);ans=m=;
for(int i=;i<=n;i++){
a[++m]='#';
a[++m]=s[i]-'a';
}
a[++m]='#';
get_hash();
int l,r,mid;
for(int i=;i<=m;i++){
l=;
if(i-<m-i) r=i;
else r=m-i+;
while(r-l>){
mid=l+r>>;
i64 hash_to_l=hash_r[i-]-hash_r[i-mid-]*pow[mid];
i64 hash_to_r=hash_l[i+]-hash_l[i+mid+]*pow[mid];
if(hash_to_l==hash_to_r) l=mid;
else r=mid;
}
ans=max(ans,l);
}
printf("Case %d: %d\n",++cas,ans);
}
return ;
}

POJ 3974 Palindrome的更多相关文章

  1. POJ 3974 - Palindrome - &lbrack;字符串hash&plus;二分&rsqb;

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  2. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  3. ●POJ 3974 Palindrome&lpar;Manacher&rpar;

    题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...

  4. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  5. poj 3974 Palindrome &lpar;manacher&rpar;

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

  6. 后缀数组 POJ 3974 Palindrome &amp&semi;&amp&semi; URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  7. POJ 3974 Palindrome &lpar;算竞进阶习题&rpar;

    hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...

  8. POJ 3974 Palindrome &vert; 马拉车模板

    给一个字符串,求最长回文字串有多长 #include<cstdio> #include<algorithm> #include<cstring> #define N ...

  9. POJ 1159 Palindrome&lpar;字符串变回文&colon;LCS&rpar;

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

随机推荐

  1. SAM&sol;BAM文件处理

    当测序得到的fastq文件map到基因组之后,我们通常会得到一个sam或者bam为扩展名的文件.SAM的全称是sequence alignment/map format.而BAM就是SAM的二进制文件 ...

  2. Activity 生命周期

    Activity 的四种基本状态 1.运行态(Running) Activity 处于屏幕最前端,用户可见且获得焦点. 2.暂停态(Paused) Activity被置于后台,用户可见,但失去焦点 3 ...

  3. ACM&sol;ICPC 之 DP-整数划分问题初探 &lpar;POJ1221&rpar;

    写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...

  4. SU Demos-02Filtering-05Suk1k2filter

    本人数学不咋地,本demo也是一知半解,敬请谅解. 这是生成的脉冲数据

  5. TCP&sol;IP 中的二进制反码求和算法

    对于这个算法,很多书上只是说一下思路,没有具体的实现.我在这里举个例子吧 以4bit(计算方便一点,和16bit是一样的)做检验和来验证. 建设原始数据为 1100 , 1010 , 0000(校验位 ...

  6. hdu2044java递推

    一只小蜜蜂... Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. HDU1284钱币兑换问题&lpar; 母函数打表&rpar;

    题意:在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. 原题http://acm.hdu.edu.cn/showproblem.php?pid=128 ...

  8. base64图片存储

    将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方. Base64编码在ora ...

  9. &lbrack;POJ3630&rsqb;Phone List &lpar;Tire&rpar;

    题意 trie字典树模板 LOJ有中文翻译https://loj.ac/problem/10049 思路 TIRE 代码 之前在LOJ上做过 直接交了 #include<cstdio> # ...

  10. AtCoder Regular Contest 063 F &colon; Snuke’s Coloring 2 &lpar;线段树 &plus; 单调栈&rpar;

    题意 小 \(\mathrm{C}\) 很喜欢二维染色问题,这天他拿来了一个 \(w × h\) 的二维平面 , 初始时均为白色 . 然后他在上面设置了 \(n\) 个关键点 \((X_i , Y_i ...