您的位置:首页 > 其它

算法竞赛 例6-2铁轨(UVa 514)

2018-02-06 23:04 190 查看
Sample Input

5

12345

54123

0

6

654321

0

0

Sample Output

Yes

No

Yes

下面代码并不能按照题述输入输出格式输出。问题在于不知道如何处理这个0

#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
const int MAXN = 1000 + 10;

int n,target[MAXN];

int main()
{
freopen("/Users/zhaohaibo/Desktop/a.txt","r",stdin);
while(scanf("%d",&n)==1)
{
stack<int> s;
int A=1,B=1;
for(int i=0; i<n; i++)
scanf("%d",&target[i]);
int ok = 1;

while( B <=n )
{
if(A == target[B])
{
A++;B++;
}
//先检查栈里有没有元素,如果有,需要判断栈顶元素是否可以出栈
else if(!s.empty() && s.top() == target[B])
{
s.pop();
B++;
}
//不满足则继续入栈
else if(A <=n )
{
s.push(A++);
}
else
{
ok = 0;
break;
}
}
printf("%s\n",ok?"Yes":"No");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: