记录编号 566888 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [CSP 2021S]回文 最终得分 100
用户昵称 Gravatar遥时_彼方 是否通过 通过
代码语言 C++ 运行时间 1.071 s
提交时间 2021-11-18 07:59:23 内存使用 5.38 MiB
显示代码纯文本
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
int tc; 
int nc;
int n[2000005];
deque<int> a;
deque<int> b;
deque<char> ans1;
deque<char> ans2;
void BJ()
{
	while(a.size()||b.size())
	{
//		cout<<"LOOK:"<<a.size()<<":"<<a.front()<<" "<<a.back()<<" "<<b.size()<<":"<<b.front()<<" "<<b.back()<<endl;
		if(a.size())
		{
			if(a.size()>1)  
			{
				if(a.front()==a.back())
				{
					ans1.push_back('L');
					ans2.push_front('L');
					a.pop_back();
					a.pop_front();
					continue;
				}
			}	
			if(b.size())
			{
				if(a.front()==b.back())
				{
					ans1.push_back('L');
					ans2.push_front('R');
					b.pop_back();
					a.pop_front();
					continue;
				}
			}
		}
		if(b.size())
		{
			if(b.size()>1)  
			{
				if(b.front()==b.back())
				{
					ans1.push_back('R');
					ans2.push_front('R');
					b.pop_back();
					b.pop_front();
					continue;
				}
			}	
			if(a.size())
			{
				if(b.front()==a.back())
				{
					ans1.push_back('R');
					ans2.push_front('L');
					a.pop_back();
					b.pop_front();
					continue;
				}
			}
		}
//		cout<<"SIZE:"<<a.size()<<" "<<b.size()<<endl; 
		break;
	}
	return;
}
bool L()
{
	bool re=true;
	a.clear();
	b.clear();
	for(int i=2;i<=nc;i++)
	{
		if(n[i]==n[1])
		{
			for(int o=2;o<i;o++) a.push_back(n[o]);
			for(int o=nc;o>i;o--) b.push_back(n[o]);
			break;
		}
	}
//	cout<<"a:\n";
//	for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
//	cout<<endl; 
//	cout<<"b:\n";
//	for(int i=0;i<b.size();i++) cout<<b[i]<<" ";
//	cout<<endl; 
	ans1.clear();
	ans2.clear();
	ans1.push_front('L');
	ans2.push_front('L');
	BJ();
	if(a.empty()&&b.empty())
	{
		while(!ans1.empty()) 
		{
			printf("%c",ans1.front());
			ans1.pop_front();
		}
		while(!ans2.empty()) 
		{
			printf("%c",ans2.front());
			ans2.pop_front();
		}
	}
	else re=false;
	return re;
}
bool R()
{
	bool re=true;
	a.clear();
	b.clear();
	for(int i=nc-1;i>=1;i--)
	{
		if(n[i]==n[nc])
		{
			for(int o=1;o<i;o++) a.push_back(n[o]);
			for(int o=nc-1;o>i;o--) b.push_back(n[o]);
			break;
		}
	}
	ans1.clear();
	ans2.clear();
	ans1.push_front('R');
	ans2.push_front('L');
	BJ();
	if(a.empty()&&b.empty())
	{
		while(!ans1.empty()) 
		{
			printf("%c",ans1.front());
			ans1.pop_front();
		}
		while(!ans2.empty()) 
		{
			printf("%c",ans2.front());
			ans2.pop_front();
		}
	}
	else re=false;
	return re;
}
void CL()
{
	scanf("%d",&nc);
	nc<<=1;
	for(int i=1;i<=nc;i++) scanf("%d",&n[i]);
	if(L()){}
	else if(!R()) printf("-1");
	printf("\n");
	return;
}
int main()
{
    freopen("2021palin.in","r",stdin);
    freopen("2021palin.out","w",stdout);
    scanf("%d",&tc);
    for(int i=1;i<=tc;i++) CL();
    return 0;
}