本机测试没毛病,上机就RE
查看原帖
本机测试没毛病,上机就RE
857343
hzy_jw楼主2023/8/18 23:15

求助

#include<bits/stdc++.h>
using namespace std;

int t,n;
int num[500003];
bool ch=1;
string s;
stack<int> mem;
set<int> check;

inline bool dfs(int i,int j)
{
	if(i>=j)
		return 1;
	s+='L';
	if(mem.top()==num[i])
	{
		ch=0;
		check.insert(num[i]);
		mem.pop();
		if(dfs(i+1,j))
			return 1;
		mem.push(num[i]);
		check.erase(num[i]);
		s=s.substr(0,s.length()-1);
		ch=1;
	}
	else if(!check.count(num[i])&&ch)
	{
		check.insert(num[i]);
		mem.push(num[i]);
		if(dfs(i+1,j))
			return 1;
		check.erase(num[i]);
		mem.pop();
		s=s.substr(0,s.length()-1);
	}
	else
		s=s.substr(0,s.length()-1);
	s+='R';
	if(mem.top()==num[j])
	{
		ch=0;
		mem.pop();
		check.insert(num[j]);
		if(dfs(i,j-1))
			return 1;
		mem.push(num[j]);
		check.erase(num[j]);
		s=s.substr(0,s.length()-1);
		ch=1;
	}
	else if(!check.count(num[j])&&ch)
	{
		check.insert(num[j]);
		mem.push(num[j]);
		if(dfs(i,j-1))
			return 1;
		check.erase(num[j]);
		mem.pop();
		s=s.substr(0,s.length()-1);
	}
	else
		s=s.substr(0,s.length()-1);
	return 0;
}
inline void cls()
{
	memset(num,0,sizeof num);
	while(!mem.empty()) mem.pop();
	s.clear();
	check.clear();
	ch=1;
	return;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cls();
		
		cin>>n;
		int m=n*2;
		for(int i=0;i<m;i++)
			cin>>num[i];
		if(dfs(0,m-1))
			cout<<s<<endl;
		else
			cout<<-1<<endl;
	}
	return 0;
}
2023/8/18 23:15
加载中...