maoboxiA2 @ 2024-02-03 10:15:10
//P4387,我哪里错了
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int m;
stack<int> s;
while(n--){
cin>>m;
int a[m+1],b[m+1],sum=1;
for(int i=1;i<=m;i++)cin>>a[i];
for(int i=1;i<=m;i++)cin>>b[i];
for(int i=1;i<=m;i++){
s.push(a[i]);
while(s.top()==b[sum]&&!s.empty()){
s.pop();
sum++;
}
}
if(s.empty())cout<<"Yes";
else cout<<"No";
while(!s.empty())s.pop();
}
return 0;
}
by __yun__ @ 2024-02-03 10:19:59
不要再主函数中定义数组,不要按照输入定义数组大小
by __Rickysun__ @ 2024-02-03 10:31:42
@maoboxiA2 默默问一句,直接倒着遍历一遍不行吗
by __Rickysun__ @ 2024-02-03 10:45:34
@maoboxiA2
咳咳,正经一点,毕竟这是个求助帖
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int m;
stack<int> s;
while (!s.empty())s.pop();
while (n--) {
cin >> m;
int a[m + 1], b[m + 1], sum = 1;
for (int i = 1; i <= m; i++)cin >> a[i];
for (int i = 1; i <= m; i++)cin >> b[i];
for (int i = 1; i <= m; i++) {
s.push(a[i]);
while (s.top() == b[sum]) {//不能在这里判断是否为空,不然前面会越界
s.pop();
sum++;
if (s.empty()) break;//在这判断就没啥问题了
}
}
if (s.empty())cout << "Yes" << endl;
else cout << "No" << endl;
//另外温馨提示,下次记得换行!(WA是因为没换行,RE是因为循环条件)
while (!s.empty())s.pop();
}
return 0;
}
by __Rickysun__ @ 2024-02-03 10:46:18
@yun 是可以这样的,不过我不喜欢这样写
by dhm13659902220 @ 2024-02-03 10:53:56
@maoboxiA2 re是超时,不能在一秒内输出
by maoboxiA2 @ 2024-02-03 16:49:16
@Rickysun 懂了,Thank you.