hdu5371 最長迴文子串變形(Manacher演算法)
http://acm.hdu.edu.cn/showproblem.php?pid=5371
Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.
Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.
Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases.
For each test case:
the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence
the second line includes N non-negative integers ,each interger is no larger than 109 , descripting a sequence.
For each test case:
the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence
the second line includes N non-negative integers ,each interger is no larger than 109 , descripting a sequence.
Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.
We guarantee that the sum of all answers is less than 800000.
We guarantee that the sum of all answers is less than 800000.
Sample Input
1
10
2 3 4 4 3 2 2 3 4 4
Sample Output
Case #1: 9
/**
hdu5371 最長迴文子串變形(Manacher演算法)
題目大意:找出一個字串可以均分為三段,第一段和第三段相同,和第二段互為迴文串
解題思路:利用Manacher算出每個位置的最長迴文子串的長度,然後列舉即可
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=210001;
int s[maxn],a[maxn];
int r[maxn],len;
void Manancher()
{
int l=0;
a[l++]=-2;
a[l++]=-1;
for(int i=0;i<len;i++)
{
a[l++]=s[i];
a[l++]=-1;
}
a[l]=-3;
int mx=0,id=0;
for(int i=0;i<l;i++)
{
r[i]=mx>i?min(r[2*id-i],mx-i):1;
while(a[i+r[i]]==a[i-r[i]])r[i]++;
if(i+r[i]>mx)
{
mx=i+r[i];
id=i;
}
//printf("%d ",r[i]);
}
// printf("\n");
}
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&len);
for(int i=0;i<len;i++)
{
scanf("%d",&s[i]);
}
Manancher();
int ans=0;
for(int i=1;i<=len*2+1;i+=2)
{
for(int j=i+r[i]-1;j-i>ans;j-=2)
{
if(j-i+1<=r[j])
{
ans=max(ans,j-i);
break;
}
}
}
printf("Case #%d: %d\n",++tt,ans/2*3);
}
return 0;
}
相關文章
- 最長迴文子串 V2(Manacher演算法)演算法
- LeetCode-5. 最長迴文子串(Manacher)LeetCode
- hihocoder 1032 最長迴文子串 (Manacher演算法 詳解+模板)演算法
- HDU 3068 最長迴文(Manacher演算法解決最長迴文串問題)演算法
- 演算法-兩最長迴文子串演算法
- java 最長迴文子串Java
- 演算法之字串——最長迴文子串演算法字串
- LEECODE 5 求最長迴文子串
- 每天一道演算法題:最長迴文子串演算法
- [動態規劃] 六、最長迴文子串動態規劃
- LeetCode 5.最長迴文子串LeetCode
- Amazon面試題:尋找最長迴文子串面試題
- 今日面試題:最長迴文子串;及迴文分割分析面試題
- Leetcode[字串] 5. 最長迴文子串LeetCode字串
- 翻譯數字串;及最長迴文子串分析字串
- leedcode-最長迴文串
- 程式碼隨想錄演算法訓練營 | 647. 迴文子串,516.最長迴文子序列演算法
- 程式碼隨想錄day46 || 647 迴文子串, 516 最長迴文子序列
- 每日一道 LeetCode (48):最長迴文子串LeetCode
- [LeetCode] Longest Palindromic Substring 最長迴文子串LeetCode
- ural 1297 最長迴文子串 字尾陣列陣列
- leetcode 解題 5. 最長迴文子串 python@ 官解,暴力法,動態法,manacher 法LeetCodePython
- 最長子串
- LeetCode - 409 - 最長迴文串LeetCode
- 程式碼隨想錄演算法訓練營day46| 647. 迴文子串 516.最長迴文子序列演算法
- 通俗易懂的最長迴文串圖解、說明及Java程式碼(中心擴散法和Manacher演算法)圖解Java演算法
- lc1771 由子序列構造的最長迴文串的長度
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- 程式碼隨想錄演算法訓練營第五十七/天 | 516. 最長迴文子序列,647. 迴文子串演算法
- Manacher-求最長迴文字串字串
- 最長迴文子串(百度筆試題和hdu 3068)筆試
- lCS(最長公共子串)
- L2-008 最長對稱子串【最長迴文字串】字串
- 找到最長迴文字串 - Manacher's Algorithm字串Go
- 演算法-無重複字元的最長子串演算法字元
- 線性dp--最長上升子序列變形
- 線性dp:最長公共子串
- LeetCode516. 最長迴文子序列LeetCode