求助:为什么输出的都是-1 ,20分
查看原帖
求助:为什么输出的都是-1 ,20分
285961
魏老师楼主2024/10/10 15:28
//回文 
#include<bits/stdc++.h>
using namespace std;
int a[1000010];//只需要a[],不需要b[],模拟回文即可 
char c[1000010];//存储L或R 
int qc[1000010],qd[1000010],topc,botc,topd,botd;//双端队列的定义 
int main()
{
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)//t组测试数据 
	{
		botc=1;botd=1;topc=0;topd=0;//队列初始化 
		int n;
		cin>>n;//每组测试数据有n个数
		for(int j=1;j<=2*n;j++)
			cin>>a[j];
		c[1]='L';c[2*n]='L';//第一次一定取左端数据,保证字典序最小
		int k;
		for(k=2;k<=2*n;k++)
			if(a[k]==a[1]) break;//寻找和a[1]相等的数的位置 
		for(int j=k-1;j>=2;j--)//依次赋值到各自的队列中 
			qc[++topc]=a[j];
		for(int j=k+1;j<=2*n;j++)
			qd[++topd]=a[j];
			
	    //核心代码
	    int j,flag;
	    for(j=2;j<=n;j++)//共配对n-1次
	    {
	    	flag=0;
	    	if(topc>=botc)//c队列不空 
		    {
				if(qc[topc]==qc[botc]) 
				{
					topc--;botc++;
					c[j]='L';c[2*n+1-j]='L';
					flag=1;cout<<c[j];continue;
				}
				if(qc[topc]==qd[botd]) 
				{
					topc--;botd++;
					c[j]='L';c[2*n+1-j]='R';
					flag=1;cout<<c[j];continue;
				}
		    } 
		    if(topd>=botd)//d队列不空 
		    {
				if(qc[topd]==qc[botc]) 
				{
					topd--;botc++;
					c[j]='R';c[2*n+1-j]='L';
					flag=1;cout<<c[j];continue;
				}
				if(qc[topd]==qd[botd])
				{
					topd--;botd++;
					c[j]='R';c[2*n+1-j]='R';
					flag=1;cout<<c[j];continue;
				}
		    }
		    
			if(flag==0)
			{
				cout<<-1<<endl;break;	
			} 
		}
		if(j==n+1)
		{
			for(int m=1;m<=2*n;m++)
				cout<<c[m];
		    cout<<endl;
		}
	
	} 
	return 0;
}
2024/10/10 15:28
加载中...