社区讨论

不是很能李姐

P4387【深基15.习9】验证栈序列参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo88vskc
此快照首次捕获于
2023/10/27 14:41
2 年前
此快照最后确认于
2023/10/27 14:41
2 年前
查看原帖
本人认为下面两个代码是相同的……有dalao能举出反例证明这两个代码为什么不同吗……
第一个AC
CPP
#include<iostream>
#include<stack>
using namespace std;
stack<int> p;
int a[3000001],b[3000001];
int main()
{
	int n,q;
	cin>>q;
	while(q--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
		}
		int tot=1;
		for(int i=1;i<=n;i++)
		{
			p.push(a[i]);
			while(p.top()==b[tot])
			{
				tot++;
				p.pop();
                if(p.empty())
                {
                    break;
                }
			 } 
		}
		if(p.empty())
		{
			cout<<"Yes"<<endl;
		}
		else
		{
			cout<<"No"<<endl; 
		}
		while(!p.empty())
		{
			p.pop();
		}
	}
	return 0;
}
第二个RE
CPP
#include<iostream>
#include<stack>
using namespace std;
stack<int> p;
int a[3000001],b[3000001];
int main()
{
	int n,q;
	cin>>q;
	while(q--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
		}
		int tot=1;
		for(int i=1;i<=n;i++)
		{
			p.push(a[i]);
			while(p.top()==b[tot]&&(!p.empty()))
			{
				tot++;
				p.pop();
			 } 
		}
		if(p.empty())
		{
			cout<<"Yes"<<endl;
		}
		else
		{
			cout<<"No"<<endl; 
		}
		while(!p.empty())
		{
			p.pop();
		}
	}
	return 0;
}
唯一的差别就在于这个地方的差异
CPP
	while(p.top()==b[tot]&&(!p.empty()))
	{
		tot++;
		p.pop();
	} 
CPP
	while(p.top()==b[tot])
	{
		tot++;
		p.pop();
      if(p.empty())
      {
         break;
      }
	} 

回复

2 条回复,欢迎继续交流。

正在加载回复...