求合法栈序列的代码找错....
小村长 2011-05-14 10:43:17 Description
给定两个整数序列a,b,判断b是否为a的一个合法出栈序列。
Input
多组测试实例,
每组占三行,第一行为一个整数n(n<=20),表示序列长度。第二行为序列a,第三行为序列b。每个序列中的n个数两两不同。输入以文件终止符结束。
Output
若b是a的一个合法出栈序列,则输出yes,否则输出no。每组输出占一行。
我的代码::
#include <iostream>
using namespace std;
const int maxsize=20;
typedef struct stack{
int data[maxsize];
stack(){top=-1;}
int top;
};
int hf(int a[],int b[], int n)
{
stack s;
int c[n];
int i=0;
int j=0;
int m=1;
while(i<n){
if(a[i]!=b[j]){
s.top=s.top+1;
s.data[s.top]=a[i];
i=i+1;
}
else{
c[j]=a[i];
i=i+1;
j=j+1;
}
}
while(s.top!=-1){
c[j]=s.data[s.top];
s.top=s.top-1;
j=j+1;
}
for(i=0;i<n && m!=0;i++){
if(c[i]!=b[i])
m=0;}
return m;
}
int main()
{
int n;
int m;
int a[maxsize];
int b[maxsize];
while(cin>>n){
int i;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
m=hf(a,b,n);
if(m==0)cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
return 0;
}