CodeForces_1326D Prefix-Suffix Palindrome(Manachar)

Prefix-Suffix Palindrome

time limit per test:2 seconds
memory limit per test:512 megabytes
Problem Description

You are given a string s s s, consisting of lowercase English letters. Find the longest string, t t t, which satisfies the following conditions:

  • The length of t t t does not exceed the length of s s s.
  • t t t is a palindrome.
  • There exists two strings a a a and b b b (possibly empty), such that t = a + b t=a+b t=a+b ( “+” represents concatenation), and a a a is prefix of s while b b b is suffix of s s s.
Input

The input consists of multiple test cases. The first line contains a single integer t ( 1 ≤ t ≤ 1 0 5 1≤t≤10^5 1t105), the number of test cases. The next t lines each describe a test case.

Each test case is a non-empty string s, consisting of lowercase English letters.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed 106.

Output

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

Sample Input

5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

Sample Output

a
abcdfdcba
xyzyx
c
abba

题意

字符串S,设a为S的前缀,b为S的后缀,t =a+b(a,b可为空),|t|<=|S| && t为回文串。求最长的t。

题解:

l e n = ∣ S ∣ len=|S| len=S,下标从0开始,答案一定是选择字符串原本的一个回文串 [ n l , n r ] [nl,nr] [nl,nr]。分情况考虑:

  • 将nr作为前缀的最后一个字符, a l e n = n l alen=nl alen=nl 。则需要满足 [ 0 , n l ) [0,nl) [0,nl) [ l e n − a l e n , l e n − 1 ] [len-alen, len-1] [lenalen,len1]连接为回文串(可通过预处理判断),同时 n r < l e n − a l e n nr<len-alen nr<lenalen(保证字符未被重复使用)。满足条件则字符串为 [ 0 , n r ] + [ l e n − a l e n , l e n − 1 ] [0,nr]+[len-alen,len-1] [0,nr]+[lenalen,len1];
  • 将al作为后缀的第一个字符, b l e n = l e n − 1 − n r blen=len-1-nr blen=len1nr,则需要满足 [ 0 , b l e n ) 与 [ l e n − b l e n , l e n − 1 ] [0,blen)与[len- blen, len-1] [0,blen)[lenblen,len1]连接为回文串,同时 b l e n < = n 1 blen<=n1 blen<=n1。满足条件字符串为 [ 0 , b l e n ) + [ n l , l e n − 1 ] [0,blen)+[nl, len-1] [0,blen)+[nl,len1]

manachar,遍历处理后的数组,求最大即可。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 1000100;
const int mod = 998244353;
char s[maxn], str[maxn*2];
int len, len2, p[maxn*2], a[maxn*2], b[maxn];
void init(char s[]);

int main()
{
    int t, n, i, j, mx, id, alen, l, r, al, ar;
    scanf("%d", &t);
    while(t--)
    {
        scanf(" %s", s);
        init(s);
        for(i=0;i<=len;i++)
            b[i] = 0;
        alen = 1, l = 0, r = len;
        mx = 0;
        al = 0, ar = len-1;
        while(al<ar && s[al] == s[ar]){
            b[al] = b[ar] = 1;
            al++, ar--;
        }
        for(i=1;i<len2;i++)
        {
            p[i] = mx>i?min(mx-i, p[2*id-i]):1;
            while(str[i-p[i]] == str[i+p[i]])
                p[i]++;
            if(i+p[i]>mx){
                id = i;
                mx = i+p[i];
            }
            int nl, nr, x;
            nl = a[i-p[i]]+1;
            nr = a[i+p[i]]-1;
            if(nr<nl)continue;
            if(nl == 0 && nr == len-1){
                alen = len;
                l = len-1, r = len;break;
            }
            if((nl == 0 || b[nl-1] == 1) && nr+nl<len && nr-nl+1+2*nl>alen){
                alen = nr-nl+1+2*nl;
                l = nr, r = len-nl;
            }
            if((nr == len-1 || b[nr+1] == 1) && len-1-nr<=nl && nr-nl+1+2*(len-1-nr) > alen){
                alen = nr-nl+1+2*(len-1-nr);
                r = nl, l = len-1-nr-1;
            }
                
        }
        for(i=0;i<=l;i++)
            printf("%c", s[i]);
        for(i=r;i<len;i++)
            printf("%c", s[i]);
        printf("\n");
    }
    return 0;
}

void init(char s[])
{
    str[0]=')';
    str[1]='*';
    a[0] = -1;
    int i, j=2;
    for(i=0;s[i];i++){
        str[j] = s[i];
        a[j] = i;
        str[j+1] = '*';
        j += 2; 
    }
    len = i;
    len2 = j;
    str[j] = 0;
    a[j] = len;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值