| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
本题目除了用栈的库函数外,也可以直接用数组来模拟!一下是代码:
#include <iostream>
using namespace std;
int main()
{
int n;
int i,j,k;
int a[1005],pre[1005];
int stack[1005];
for(i=1;i<=1000;i++)
pre[i]=i;
cin>>n;
while( n!=0)
{
while(cin>>a[1])
{
if(a[1]==0) break ;
for(i=2;i<=n;i++)
cin>>a[i];
i=j=k=1;
while(j<=n &&i<=n)
{
if(a[i]==pre[j])
{
i++;j++;
continue;
}
if(k>1) { if(a[i]==stack[k-1]) { i++; k--; continue;}}
stack[k]=pre[j];
k++;j++;
}
if(i<=n )
{
for(j=i;j<=n;j++)
if(a[j]==stack[k-1]) k--;
else break;
}
if(j>n) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
cin>>n;
if(n!=0)
cout<<endl;
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator