// Problem: P7915 [CSP-S 2021] 回文
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7915
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int ret=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-') f=-f; ch=getchar(); }
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
inline void write(int x){
if(x<0){ putchar('-'); x=-x; }
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=1e6+10;
void clear(int a[],int n){
for(int i=0;i<=n+1;++i) a[i]=0;
}
int T,n,a[N],lst[N],pre[N],pmt[N];
int res[N];// L->0, R->1
void print(){
for(int i=1;i<=n;++i){
if(res[i]) putchar('R');
else putchar('L');
}
}
void solve(int sg,int L,int R,int L1,int R1,bool &flag){
clear(res,n);
res[1]=sg,res[n]=0;
int i=2,j=n-1;
while(i<j){
if(pmt[L+1]==L1-1){
res[i++]=0,res[j--]=0;
++L,--L1;
}else if(pmt[L+1]==R1+1){
res[i++]=0,res[j--]=1;
++L,++R1;
}else if(pmt[R-1]==L1-1){
res[i++]=1,res[j--]=0;
--R,--L1;
}else if(pmt[R-1]==R1+1){
res[i++]=1,res[j--]=1;
--R,++R1;
}else return;
}
flag=1;
print();
}
int main(){
T=read();
while(T--){
n=read(),n<<=1;
clear(a,n),clear(lst,n),clear(pre,n),clear(pmt,n);
for(int i=1;i<=n;++i){
a[i]=read();
pre[i]=lst[a[i]];
lst[a[i]]=i;
if(pre[i]){
pmt[i]=pre[i];
pmt[pre[i]]=i;
}
}
bool flag=0;
solve(0,1,n+1,pmt[1],pmt[1],flag);//L......
if(!flag) solve(1,0,n,pmt[n],pmt[n],flag);//......R
if(!flag) write(-1);
puts("");
}
return 0;
}
大样例有几个没过,
所输出的字符串的字典序 ≤ 正确答案的字典序,
思路类似于这篇题解(