不是很能李姐
查看原帖
不是很能李姐
304713
The_Dog_of_Hoshiguma楼主2022/8/18 23:37

本人认为下面两个代码是相同的……有dalao能举出反例证明这两个代码为什么不同吗……

第一个AC

#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

#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;
}

唯一的差别就在于这个地方的差异

	while(p.top()==b[tot]&&(!p.empty()))
	{
		tot++;
		p.pop();
	} 
	while(p.top()==b[tot])
	{
		tot++;
		p.pop();
      if(p.empty())
      {
         break;
      }
	} 
2022/8/18 23:37
加载中...