This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

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

  • The length of tt does not exceed the length of ss.
  • tt is a palindrome.
  • There exists two strings aa and bb (possibly empty), such that t=a+bt=a+b ( "++" represents concatenation), and aa is prefix of ss while bb is suffix of ss.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105), the number of test cases. The next tt lines each describe a test case.

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

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

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.

Example

Input
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
Output
a
abcdfdcba
xyzyx
c
abba

Note

In the first test, the string s=s="a" satisfies all conditions.

In the second test, the string "abcdfdcba" satisfies all conditions, because:

  • Its length is 99, which does not exceed the length of the string ss, which equals 1111.
  • It is a palindrome.
  • "abcdfdcba" == "abcdfdc" ++ "ba", and "abcdfdc" is a prefix of ss while "ba" is a suffix of ss.

It can be proven that there does not exist a longer string which satisfies the conditions.

In the fourth test, the string "c" is correct, because "c" == "c" ++ "" and aa or bb can be empty. The other possible solution for this test is "s".

题意:

给你一个有小写字母组成的字符串s,你需要找出来一个s的前缀a,和s的后缀b,使得a、b拼接起来是一个回文串t,我们要保证a字符串的长度加上b字符串的长度要小于s字符串长度

输出最长的回文串t

题解:

可以先用while循环找出来前面和后面相等长度,再对剩下的字符串用manacher求包含边界的最长回文。

例如:abacsjsba。我们用while循环找到左边的ab和右边的ba,剩下acsjs,再找该字符串包含边界的最大回文串,显然是sjs,最后ab+sjs+ba就是答案。

这里要注意,我们求的是包含边界的最大回文串,例如剩下的字符串为aacddcb,那么我们只能输出aa而不能输出cddc,因为我们求的是最长前缀和最长后缀拼接而成的最长回文串

正确代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<set>
6 using namespace std;
7 const int maxn=1e6+10;
8 const int INF=0x3f3f3f3f;
9 const int mod=998244353;
10 using namespace std;
11 string Manacher(const string &s)
12 {
13 string t="#";
14 for(char c:s) t+=c,t+="#";
15 /*
16 两种解法的唯一区别在于第一种解法使用了
17 for (char& c : s)
18 第二种使用了
19 for (char c : s)
20 结果是第一种方法比第二种方法快得多。虽然原因很简单,只是一个很基本的道理,但是毕竟初学,还是写下来记录一下。
21 使用
22 for (char c : s)
23 时会复制一个s字符串再进行遍历操作,而使用
24 for (char& c : s)
25 时直接引用原字符串进行遍历操作,由于复制一个字符串花费了大量的时间,所以第一种解法要快于第二种解法。
26 链接:https://blog.csdn.net/edjoker2/article/details/79383536
27 */
28 int Len[t.size()]= {0};
29 int sum=0;
30 int pos=0;
31 for(int i=0; i<t.size(); i++)
32 {
33 if(i<sum)
34 Len[i]=min(Len[2*pos-i],sum-i);
35 else
36 Len[i]=1;
37 while(i-Len[i]>=0&&i+Len[i]<t.size()&&t[i-Len[i]]==t[i+Len[i]])
38 Len[i]+=1;
39 if(Len[i]+i-1>sum)
40 pos=i,sum=Len[i]+i-1;
41 }
42 int MaxLen=0;
43 for(int i=0; i<t.size(); i++)
44 if(Len[i]==i+1)
45 MaxLen=i;
46 return s.substr(0,MaxLen);
47 }
48 void solve()
49 {
50 string s;
51 cin>>s;
52 int l=0,r=s.size()-1;
53 while(l<r&&s[l]==s[r]) ++l,--r;
54 string s1=s.substr(l,r-l+1);
55 string s2=s1;
56 reverse(s2.begin(),s2.end());
57 /*
58 reverse函数功能是逆序(或反转),多用于字符串、数组、容器。头文件是#include <algorithm>
59 reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数无返回值
60 */
61 s1=Manacher(s1);
62 s2=Manacher(s2);
63 cout<<s.substr(0,l)<<(s1.size()>s2.size()?s1:s2)<<s.substr(r+1)<<"\n";
64 }
65 int main()
66 {
67 int t;
68 cin>>t;
69 while(t--)
70 solve();
71 return 0;
72 }

我原本求回文串的时候没有注意还要包含边界,导致一直错。。。这里代码也给一下,有可能其他题用得到

  1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<set>
6 using namespace std;
7 const int maxn=1e7+10;
8 const int INF=0x3f3f3f3f;
9 const int mod=998244353;
10 char str[maxn],s[maxn],ptr[maxn];
11 int len,Len[maxn],index;
12 void init()
13 {
14 memset(str,0,sizeof(str));
15 int k=0;
16 str[k++]='$';
17 for(int i=0; i<len; ++i)
18 str[k++]='#',str[k++]=s[i];
19 str[k++]='#';
20 len=k;
21 }
22 int manacher()
23 {
24 Len[0]=0;
25 int sum=0;
26 int id,mx=0;
27 for(int i=1; i<len; ++i)
28 {
29 if(i<mx) Len[i]=min(mx-i,Len[2*id-i]);
30 else Len[i]=1;
31 while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
32 if(Len[i]+i>mx)
33 {
34 mx=Len[i]+i;
35 id=i;
36 if(sum<Len[i])
37 {
38 sum=Len[i];
39 index=i;
40 }
41 }
42 }
43 return (sum-1);
44 }
45 int main()
46 {
47 int t;
48 scanf("%d",&t);
49 while(t--)
50 {
51 scanf("%s",ptr);
52 int num=0,lenn=strlen(ptr)-1;
53 if(lenn==0)
54 {
55 printf("%s\n",ptr);
56 continue;
57 }
58 while(ptr[num]==ptr[lenn-num] && num<lenn-num)
59 {
60 num++;
61 }
62 if((lenn+1)%2 && num*2+1==lenn+1)
63 {
64 printf("%s\n",ptr);
65 continue;
66 }
67 if(num*2==lenn+1 && (lenn+1)%2==0)
68 {
69 for(int i=0; i<num; ++i)
70 printf("%c",ptr[i]);
71 for(int i=num-1; i>=0; --i)
72 printf("%c",ptr[i]);
73 printf("\n");
74 continue;
75 }
76 len=0;
77 for(int i=num; i<=lenn-num; ++i)
78 {
79 s[len++]=ptr[i];
80 }
81 init();
82 int ans=manacher();
83 for(int i=0; i<num; ++i)
84 printf("%c",ptr[i]);
85 //printf("%d**%d\n",ans,index);
86 if(index%2)
87 {
88 index=index/2;
89 for(int i=(index+1)-ans/2; i<=index+ans/2; ++i)
90 printf("%c",s[i-1]);
91 }
92 else
93 {
94 index=index/2;
95 for(int i=index-ans/2; i<=index+ans/2; ++i)
96 printf("%c",s[i-1]);
97 }
98 if(num>0)
99 {
100 for(int i=num-1; i>=0; --i)
101 printf("%c",ptr[i]);
102 }
103 printf("\n");
104 }
105 return 0;
106 }
107 //0 1 2 3 4 5 6 7 8
108 // 1 2 3 4

用这个代码在codeforces上提交的时候第二组样例错了

Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)的更多相关文章

  1. Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法)

    题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> ...

  2. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  3. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  4. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  5. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  6. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  7. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  8. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  9. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

随机推荐

  1. leetcode-222完全二叉树的节点个数

    题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...

  2. Openstack Ocata 公共服务端(三)

    Openstack Ocata 公共服务端 mysql 安装: yum install mariadb mariadb-server mysql 安装过程省略 rabbit-server 安装包: # ...

  3. REUSE_ALV_FIELDCATALOG_MERGE函数

    今天使用REUSE_ALV_FIELDCATALOG_MERGE函数,就是获取不到fieldcat, 搞了半天才发现,原来参数要全部大写才行!!小写字符就是获取不到,唉,悲哀...

  4. springmvc 字符串转日期格式

    http://www.mamicode.com/info-detail-2485490.html

  5. SpringMVC下关于静态资源访问

    SpringMVC静态资源访问 聊一聊关于静态资源的访问问题 首先,我们要对web.xml里面的DispatcherServlet进行设置 <!-- 中央调度器--> <servle ...

  6. 微信登录1-OAuth2简介

    一.OAuth2解决什么问题 1.开放系统间授权 照片拥有者想要在云冲印服务上打印照片,云冲印服务需要访问云存储服务上的资源 2.图例 资源拥有者:照片拥有者 客户应用:云冲印 受保护的资源:照片 3 ...

  7. 图像分类学习:X光胸片诊断识别----迁移学习

    引言   刚进入人工智能实验室,不知道是在学习机器学习还是深度学习,想来他俩可能是一个东西,查阅之后才知道这是两个领域,或许也有些交叉,毕竟我也刚接触,不甚了解.   在我还是个纯度小白之时,写下这篇 ...

  8. linux设备

    设备初始化时同样要执行一个device_register函数,该函数传入一个struct device *类型的指针,因此要定义一个struct device类型的变量作为我们的设备. struct ...

  9. [已完结]CMU数据库(15-445)实验2-B+树索引实现(下)

    4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...

  10. 莫队/se 优雅的暴力

    莫队算法 发明者:队爷莫涛 基于分块的一种暴力算法, 复杂度最慢可以被卡到\(n^2\)正常情况下的复杂度大约在\(O(n\sqrt{n})\)左右分块的大小对复杂的影响很大其中最优分块的大小为\(\ ...