社区讨论
双指针过不了样例求调qaq
P7915[CSP-S 2021] 回文参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @misl99b3
- 此快照首次捕获于
- 2025/12/05 16:14 2 个月前
- 此快照最后确认于
- 2025/12/07 09:20 2 个月前
code
CPP#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n;
int t,a[N];
deque<char> qwq;
int l = 1,r = 2 * n;//当前选择
int L,R;
int main(){
cin >> t;
while(t--){
cin >> n;
l = 1;r = 2 * n;
cin >> a[1];
int ans = a[1];
for(int i = 2;i <= 2 * n;i++){
cin >> a[i];
ans ^= a[i];
if(a[i] == a[1]){
L = i + 1;
R = i - 1;
}
}
if(ans != 0){
cout<<-1<<endl;
continue;
}
bool f = 1;
for(int i = 1;i <= n;i++){//n次选择
if(l < L){//选择开头
if(a[l] == a[L - 1]&&l < L - 1){
qwq.push_back('L');
L--;
l++;
}else if(a[l] == a[R + 1]&&R + 1 < r){
qwq.push_back('L');
R++;
l++;
}
}else if(r > R){//选择结尾
if(a[r] == a[L - 1]&&l < L - 1){
qwq.push_back('R');
L--;
r--;
}else if(a[r] == a[R + 1]&&R + 1 < r){
qwq.push_back('R');
R++;
r--;
}
}else{//输出-1
f = 0;
}
}
if(f){
while(!qwq.empty()){
cout<<qwq.front();
qwq.pop_front();
}
}else{
cout<<-1;
}
cout<<endl;
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...