ACM题解报告——HD1022

  比较简单的一道进栈和出栈的题目:http://acm.hdu.edu.cn/showproblem.php?pid=1022

  题目的大体意思就是一个火车站某一时刻只能停9辆火车,分别输入进栈火车的数量、序列和火车出栈的序列,判断该出栈序列能否满足要求,如果可以满足要求,则进栈时输出in、出栈时输出out.

  代码如下:

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 int main( )
 5 {
 6   int n,i,j;
 7   char a[10],b[10],stack[10];//a,b是两个串
 8   int top;
 9   while( scanf("%d",&n)!=EOF)
10 {
11   top=-1;//栈为空
12   cin>>a>>b;
13 
14   for(i=0,j=0;i<n;i++)
15 {
16   top++;
17   stack[top]=a[i];//a[i]进栈
18   
19 while( 1)
20 {
21   if( top==-1||stack[top]!=b[j]) break;//栈为空或者不符合出栈的条件,跳出while循环
22   else
23     {//出栈
24   top--;
25   j++;
26  }
27   }
28  }
29   if( top!=-1)//若最后栈不为空,则该出栈顺序无法满足
30   printf("No.\n");
31  else
32 {
33    printf("Yes.\n");
34 
35    for(i=0,j=0;i<n;i++)
36      {//进栈输出in,出栈输出out
37   top++;
38   stack[top]=a[ i];
39   printf("in\n");
40 while( 1)
41 {
42   if( top==-1||stack[top]!=b[j]) break;
43   else
44 {
45   top--;
46   j++;
47   printf("out\n");
48  }
49   }
50  }
51  }
52  printf("FINISH\n");
53  }
54   return 0;
55 }

 

posted @ 2013-05-16 22:43  paradise in hell  Views(187)  Comments(0Edit  收藏  举报