P7915 [CSP-S 2021] 回文
题目

分析
其实想得暴力一点就能发现正解。。。考场上看都没看就是个暴力爬了。。
我们发现,如果第一个数确定了,那么最后一个也就确定了,如果第二个数确定了,那么倒数第二个数也就确定了。
那么我们可以想到,假设现在已经确定了第一个,第二个数显然有两种可能,而倒数第二个数显然也只有两种可能!(因为第二个数可以是开头或者结尾,倒数第二个数必须和倒数第一个数挨在一起!)
那么我们显然可以贪心地选择,如果第二个数可以是开头(就是倒数第二个数的两个可能的位置可以和当前的开头匹配),那么就选择开头,否则第二个数一定是当前的结尾,如果倒数第二个数的两个可能的位置这时依然不等于结尾,则不合法。
然后我们就可以类似两个双指针来做,每次贪心匹配即可,不能匹配说明无解。
代码
#include<bits/stdc++.h>
using namespace std;
//#ifdef ONLINE_JUDGE
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
//#endif
template<typename T>
inline void read(T &x){
x=0;bool f=false;char ch=getchar();
while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();}
x=f?-x:x;
return ;
}
template<typename T>
inline void write(T x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10^48);
return ;
}
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define PII pair<int,int>
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define dep(i,y,x) for(register int i=(y);i>=(x);i--)
const int MOD=1e9+7;
inline int inc(int x,int y){x+=y;return x>=MOD?x-MOD:x;}
inline int dec(int x,int y){x-=y;return x<0?x+MOD:x;}
inline void incc(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
inline void decc(int &x,int y){x-=y;if(x<0) x+=MOD;}
inline void chkmin(int &x,int y){if(y<x) x=y;}
inline void chkmax(int &x,int y){if(y>x) x=y;}
const int N=1e6+5,M=2e5+5,INF=1e9+7;
int t,n,a[N],b[N],p1,p2,sta1[N],top1,sta2[N],top2;
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// double ST=clock();
read(t);
while(t--){
read(n);n<<=1;
for(int i=1;i<=n;i++) read(a[i]);
for(int i=1;i<=n;i++){
if(a[i]==a[1]&&i!=1) p1=i;
if(a[i]==a[n]&&i!=n) p2=i;
}
int l1=2,l2=p1-1,r1=n,r2=p1+1;
top1=top2=0;
sta1[++top1]=0,sta2[++top2]=0;
bool flag=false;
for(int i=1;i<=n-2;i+=2){
if(a[l1]==a[l2]&&l1<l2) sta1[++top1]=0,sta2[++top2]=0,l1++,l2--;
else if(a[l1]==a[r2]&&l1<r2) sta1[++top1]=0,sta2[++top2]=1,l1++,r2++;
else if(a[r1]==a[l2]&&r1>l2) sta1[++top1]=1,sta2[++top2]=0,r1--,l2--;
else if(a[r1]==a[r2]&&r1>r2) sta1[++top1]=1,sta2[++top2]=1,r1--,r2++;
else{
flag=true;
break;
}
}
if(!flag){
for(int i=1;i<=top1;i++) pc(sta1[i]?'R':'L');
for(int i=top2;i>=1;i--) pc(sta2[i]?'R':'L');
pc('\n');
continue;
}
flag=false;
l1=1,l2=p2-1,r1=n-1,r2=p2+1;
top1=top2=0;
sta1[++top1]=1,sta2[++top2]=0;
for(int i=1;i<=n-2;i+=2){
if(a[l1]==a[l2]&&l1<l2) sta1[++top1]=0,sta2[++top2]=0,l1++,l2--;
else if(a[l1]==a[r2]&&l1<r2) sta1[++top1]=0,sta2[++top2]=1,l1++,r2++;
else if(a[r1]==a[l2]&&r1>l2) sta1[++top1]=1,sta2[++top2]=0,r1--,l2--;
else if(a[r1]==a[r2]&&r1>r2) sta1[++top1]=1,sta2[++top2]=1,r1--,r2++;
else{
flag=true;
break;
}
}
if(flag) puts("-1");
else{
for(int i=1;i<=top1;i++) pc(sta1[i]?'R':'L');
for(int i=top2;i>=1;i--) pc(sta2[i]?'R':'L');
pc('\n');
}
}
//#ifndef ONLINE_JUDGE
// cerr<<"\nTime:"<<(clock()-ST)/CLOCKS_PER_SEC<<"s\n";
//#endif
return 0;
}

浙公网安备 33010602011771号