[CSP-S2021] 回文
链接:
题意:
给出一个长度为 \(2n\) 的序列 \(a\),其中 \(1\sim n\) 每个数出现了 2 次。有 L,R 两种操作分别是将 \(a\) 的开头或末尾元素加入到初始为空的序列 \(b\) 里,目标是让 \(b\) 成为一个回文串。
需要判断无解或求出字典序最小的方案。
\(T(T\leq100)\) 组数据,对于每组数据 \(n\leq5\times10^5\)。\(\sum n\leq5\times10^5\)。
分析:
首先对于 \(a\) 串,一定存在一个分界点使得左边的元素是通过 L 操作弹出的,右边的元素是通过 R 操作弹出的,显然 \(b\) 的末尾就在这个分界点的两边,显然 \(b\) 的末尾一定是 \(a\) 的开头或结尾,这里先假设是开头。
那么找到这个分界点,也就是与 \(a_1\) 相同的位置。发现序列被分成了两段,左边一段是从左往右通过 L 操作弹出的,右边一段是从右往左通过 R 操作弹出的,那么我们把这两段拉出来,这里有个细节就是分界点要分在左边一段,因为要求字典序最小。
假设与 \(a_1\) 相等的地方是 \(i\),\(i\) 的下一个位置是 \(j\),大概就是:

变成了两个队列,队首在 \(b\) 的前面,队尾在 \(b\) 的后面,那么只要队首和队尾能配对,就贪心选择字典序小的并加入到 \(b\) 里就可以了。
然后如果 \(b\) 的末尾是 \(a\) 的末尾,就重新找分界点再做一遍就行了。
另外,可能会存在 \(a\) 的开头等于 \(a\) 的末尾的情况,此时除非原始串就是回文串,否则是无解的,特判一下就行了。
算法:
找到分界点,维护两个双端队列,贪心选择即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in read()
inline int read(){
int p=0,f=1;
char c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){p=p*10+c-'0';c=getchar();}
return p*f;
}
const int N=5e5+5;
int T,n,a[N<<1];
int flag,s,e;
int A[N<<1],ai,an;
int B[N<<1],bi,bn;
int ans[N<<1];
signed main(){
freopen("palin.in","r",stdin);
freopen("palin.out","w",stdout);
T=in;
while(T--){
flag=1;n=in;
for(int i=1;i<=(n<<1);i++)a[i]=in;
for(int i=1;i<=n;i++)
if(a[i]!=a[(n<<1)-i+1])flag=0;
for(int i=2;i<(n<<1);i++){
if(a[i]==a[1])s=i;
if(a[i]==a[(n<<1)])e=i;
}
if(a[1]==a[(n<<1)]){
if(flag){for(int i=1;i<=(n<<1);i++)putchar('L');puts("");}
else puts("-1");
continue;
}
for(int i=1;i<=s;i++)A[i]=a[i];an=s;bn=(n<<1)-s;
for(int i=(n<<1);i>=s+1;i--)B[(n<<1)-i+1]=a[i];
ai=1,bi=1,flag=0;
for(int i=1;i<=n;i++){
if(ai<an&&A[ai]==A[an])
ans[i]=ans[(n<<1)-i+1]='L',ai++,an--;
else if(ai<=an&&bi<=bn&&A[ai]==B[bn])
ans[i]='L',ans[(n<<1)-i+1]='R',ai++,bn--;
else if(bi<bn&&B[bi]==B[bn])
ans[i]=ans[(n<<1)-i+1]='R',bi++,bn--;
else if(ai<=an&&bi<=bn&&B[bi]==A[an])
ans[i]='R',ans[(n<<1)-i+1]='L',bi++,an--;
else flag=1;
}
if(!flag){
for(int i=1;i<=(n<<1);i++)
cout<<(char)ans[i];
puts("");
}
else{
s=e;
for(int i=1;i<=s;i++)A[i]=a[i];an=s;bn=(n<<1)-s;
for(int i=(n<<1);i>=s+1;i--)B[(n<<1)-i+1]=a[i];
ai=1,bi=1,flag=0;
for(int i=1;i<=n;i++){
if(ai<an&&A[ai]==A[an])
ans[i]=ans[(n<<1)-i+1]='L',ai++,an--;
else if(ai<=an&&bi<=bn&&A[ai]==B[bn])
ans[i]='L',ans[(n<<1)-i+1]='R',ai++,bn--;
else if(bi<bn&&B[bi]==B[bn])
ans[i]=ans[(n<<1)-i+1]='R',bi++,bn--;
else if(ai<=an&&bi<=bn&&B[bi]==A[an])
ans[i]='R',ans[(n<<1)-i+1]='L',bi++,an--;
else flag=1;
}
if(!flag){
for(int i=1;i<=(n<<1);i++)
cout<<(char)ans[i];
puts("");
}
else puts("-1");
}
}
return 0;
}
题外话:
没想到会这么简单,失策了
[CSP-S2021] 回文的更多相关文章
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Redis——set,hash与列表
一.List列表 基于Linked List实现 元素是字符串类型 列表头尾增删快,中间增删慢,增删元素是常态 元素可以重复出现 最多包含2^32-1元素 列表的索引 从左至右,从0开始 从右至左,从 ...
- CentOS安装oh-my-zsh并配置语法高亮和命令自动补全
安装zsh 和 oh-my-zsh 安装zsh yum install zsh 安装git yum install git 切换默认shell chsh -s /bin/zsh clone from ...
- pip更新升级和删除包
pip检测更新命令:pip list –outdated pip升级包命令:pip install --upgrade packagename pip卸载包命令:pip uninstall packa ...
- PHP中的垃圾回收相关函数
之前我们已经学习过 PHP 中的引用计数以及垃圾回收机制的概念.这些内容非常偏理论,也是非常常见的面试内容.而今天介绍的则是具体的关于垃圾回收的一些功能函数.关于之前的两篇介绍文章,大家可以到文章底部 ...
- 5ucms 调用当前文章的评论,以及评论列表
<div class="center_"> <h2>共有 {field:comments} 位网友发表了评论</h2> <ul class ...
- ci框架驱动器
1.驱动器什么是 驱动器是一种特殊类型的类库,它有一个父类和任意多个子类.子类可以访问父类, 但不能访问兄弟类.在你的控制器中,驱动器为你的类库提供了 一种优雅的语法,从而不用将它们拆成很多离散的类. ...
- 【Go】Golang实现gRPC的Proxy的原理
背景 gRPC是Google开始的一个RPC服务框架, 是英文全名为Google Remote Procedure Call的简称. 广泛的应用在有RPC场景的业务系统中,一些架构中将gRPC请求都经 ...
- 【理解OS】1.保护模式概述
这个系列文章主要目的是为了记录我个人学习保护模式后的总结与一点点的思考.我也是一个学习者,其中由错误在所难免,若各位朋友指出将不胜感激. 1. Intel CPU的运行模式概述 这里我将粗略介绍Int ...
- 架构师必备:MySQL主从同步原理和应用
日常工作中,MySQL数据库是必不可少的存储,其中读写分离基本是标配,而这背后需要MySQL开启主从同步,形成一主一从.或一主多从的架构,掌握主从同步的原理和知道如何实际应用,是一个架构师的必备技能. ...
- FastAPI(64)- Settings and Environment Variables 配置项和环境变量
背景 在许多情况下,应用程序可能需要一些外部设置或配置,例如密钥.数据库凭据.电子邮件服务凭据等. 大多数这些设置都是可变的(可以更改),例如数据库 URL,很多可能是敏感数据,比如密码 出于这个原因 ...