MLE求解
查看原帖
MLE求解
581411
sunyuyang楼主2022/7/24 17:47

大佬求助

#include<bits/stdc++.h>
using namespace std;
int N,n[105],hw[1000005],s[105][1000005],vis[1000005];
bool flag;
char vhz[1000005];
void input()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&n[i]);
		for(int j=1;j<=n[i]*2;j++)
		{
			scanf("%d",&s[i][j]);
		}
	}
}
bool check(int m)
{
	for(int i=1;i<=m;i++)
	{
		if(hw[i]!=hw[m*2+1-i])
		return 0;
	}
	return 1;
}
bool dfs(int x,int step)
{
	if(flag==1)
	return 1;
	if(step==n[x]*2+1)
	{
		int h=1,l=1,r=n[x]*2;
		for(int i=1;i<step;i++)
		{
			if(vis[i]==0)
			{
				hw[h]=s[x][l];
				l++;h++;
			}
			else
			{
				hw[h]=s[x][r];
				r--;h++;
			}
		}
		if(check(h/2)==1)
		{
			flag=1;
			for(int i=1;i<step;i++)
			printf("%c",vhz[i]);
			printf("\n");
			return 1;
		}
		else
		{
			return 0;
		}
	}
	vis[step]=0;vhz[step]='L';
	dfs(x,step+1);
	vis[step]=1;vhz[step]='R';
	dfs(x,step+1);
}

void work()
{
	for(int i=1;i<=N;i++)
	{
		flag=0;
		if(dfs(i,1)==0)
		printf("-1\n");
	}
}
int main()
{
	input();
	work();
}
2022/7/24 17:47
加载中...