记录编号 574105 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [CSP 2021S]回文 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.525 s
提交时间 2022-07-30 17:46:50 内存使用 10.69 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
const int N = 1000010;
int t,n;
int a[N],p[N];
int l1,r1,l2,r2,id;
bool flag;
int main(){
	freopen("2021palin.in","r",stdin);
	freopen("2021palin.out","w",stdout);
    scanf("%d",&t);
    while (t--){
    	scanf("%d",&n);
    	memset(a,0,sizeof(a));
    	memset(p,0,sizeof(p));
    	for(int i = 1;i <= 2 * n;i++)scanf("%d",&a[i]);
    	for(int i = 2;i <= 2 * n;i++)if(a[i] == a[1]){id = i;break;}
    	p[1] = 1,p[2*n] = 1;
    	l1 = 2,r1 = id-1,l2 = id+1,r2 = 2*n;
    	flag = 1;
    	for(int i = 2;i <= n;i++){
    		if(a[l1] == a[r1] && l1 < r1){
    			p[i] = 1,p[2*n-i+1] = 1;
    			l1++,r1--;
    			continue;
			}
			if(a[l1] == a[l2] && l1 <= r1 && l2 <= r2){
				p[i] = 1,p[2*n-i+1] = 2;
				l1++,l2++;
				continue;
			}
			if(a[r2] == a[r1] && l1 <= r1 && l2 <= r2){
				p[i] = 2,p[2*n-i+1] = 1;
				r2--,r1--;
				continue;
			}
			if(a[r2] == a[l2] && l2 < r2){
				p[i] = 2,p[2*n-i+1] = 2;
				r2--,l2++;
				continue;
			}
			flag = 0;
		}
		if(flag){
			for(int i = 1;i <= 2*n;i++)printf("%c",(p[i] == 1? 'L' : 'R'));
			printf("\n");
			continue;
		}
    	for(int i = 1;i < 2 * n;i++)if(a[i] == a[2*n]){id = i;break;}
    	p[1] = 2,p[2*n] = 1;
    	l1 = 1,r1 = id-1,l2 = id+1,r2 = 2*n-1;
    	flag = 1;
    	for(int i = 2;i <= n;i++){
    		if(a[l1] == a[r1] && l1 < r1){
    			p[i] = 1,p[2*n-i+1] = 1;
    			l1++,r1--;
    			continue;
			}
			if(a[l1] == a[l2] && l1 <= r1 && l2 <= r2){
				p[i] = 1,p[2*n-i+1] = 2;
				l1++,l2++;
				continue;
			}
			if(a[r2] == a[r1] && l1 <= r1 && l2 <= r2){
				p[i] = 2,p[2*n-i+1] = 1;
				r2--,r1--;
				continue;
			}
			if(a[r2] == a[l2] && l2 < r2){
				p[i] = 2,p[2*n-i+1] = 2;
				r2--,l2++;
				continue;
			}
			printf("-1\n");
			flag = 0;
			break;
		}
		if(flag){
			for(int i = 1;i <= 2*n;i++)printf("%c",(p[i] == 1? 'L' : 'R'));
			printf("\n");
			continue;
		}
	}
	
	return 0;
}