liumuxian @ 2023-12-24 16:46:13
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100010;
int a[N],q[N],top=0,cnt=1,n,x;
void init(){
for(int i=0;i<=N;i++){
a[i]=0;
q[i]=0;
}
top=0;
cnt=1;
}
int main(){
int t;
cin>>t;
while(t--){
init();
cin>>n;
for(int i=1;i<=n;i++) cin>>q[i];
for(int i=1;i<=n;i++){
cin>>x;
if(a[top]!=x){
while(a[top]!=x){
if(cnt>n){
cout<<"NO"<<endl;
return 0;
}
a[++top]=q[cnt];
cnt++;
}
top--;
}
else top--;
}
cout<<"YES"<<endl;
}
return 0;
}
by Y_QWQ_Y @ 2023-12-24 16:56:28
@liumuxian 这里不对
if(cnt>n){
cout<<"NO"<<endl;
return 0;
}
这样写导致你的程序一输出
改成这样,然后在外面每一层循环判断
if(cnt>n){
cout<<"NO"<<endl;
vis=0;
break;
}
判断:
if(vis)break;
直到最外层循环
by Y_QWQ_Y @ 2023-12-24 16:58:41
@liumuxian 还有一个问题,输出是
by liumuxian @ 2023-12-24 17:16:26
@Y_QWQ_Y 谢谢
by liumuxian @ 2023-12-24 17:19:09
@Y_QWQ_Y
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100010;
int a[N],q[N],top=0,cnt=1,n,x;
void init(){
for(int i=0;i<=N;i++){
a[i]=0;
q[i]=0;
n=0;
x=0;
}
top=0;
cnt=1;
}
int main(){
int t;
cin>>t;
while(t--){
init();
cin>>n;
int flag=0;
for(int i=1;i<=n;i++) cin>>q[i];
for(int i=1;i<=n;i++){
if(flag==1) break;
cin>>x;
if(a[top]!=x){
while(a[top]!=x){
if(cnt>n){
cout<<"No"<<endl;
flag=1;
break;
}
a[++top]=q[cnt];
cnt++;
}
top--;
}
else top--;
}
if(flag==0) cout<<"Yes"<<endl;
}
return 0;
}
我刚改完,只AC了一个点,还得再问一下
by Y_QWQ_Y @ 2023-12-24 17:43:35
我是直接用栈模拟的,你可以参考一下
#include<bits/stdc++.h>
using namespace std;
int main(){
int q;
cin>>q;
while(q--){
int n;
int a[100001],b[100001];
stack<int>s;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
int head=1;
for(int i=1;i<=n;i++){
s.push(a[i]);
while(s.top()==b[head]){
s.pop();
head++;
if(s.empty())
break;
}
}
if(s.empty())cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
by liumuxian @ 2023-12-24 17:47:23
@Y_QWQ_Y 我看一下,谢谢