poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】

问题如下:

问题 B: Rails



时间限制: 1 Sec  内存限制: 64 MB

提交: 14  解决: 4

[提交][状态][讨论版]

题目描述



There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.





The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.



输入



The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0. The last block consists of just one line containing 0.



输出



The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains "Yes" if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains "No". In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.



样例输入



5

1 2 3 4 5

5 4 1 2 3

0

6

6 5 4 3 2 1

0

0

样例输出



Yes

No



Yes

提示
View question

 

利用递归的思想,可以这么写:

 1 //模拟法:搭建stack堆栈模拟a堆栈出栈顺序以判断a堆栈是否合法出栈

 2 

 3 #include<stdio.h>

 4 #include<string.h>

 5 #include<malloc.h>

 6 

 7 int main()

 8 {

 9     int t,n,i,len,flag;

10     int a[1001],stack[1001];

11     int cur,pos,top;

12     while(scanf("%d",&t)!=EOF)

13     {

14         if(t==0)

15             return 0;

16         while(1)

17         {

18             cur=1;pos=0;top=0;

19             for(i=0;i<t;i++)

20             {

21                 scanf("%d",&n);

22                 if(i==0 && n==0)

23                 {

24                     printf("\n\n");

25                     t=0;

26                 }

27                 a[i]=n;

28             }

29 

30             flag=0;

31             if(t)

32             {

33 

34                 //cur=1;pos=0;top=0;

35                 //top代表 stack 堆栈中有几个数

36                 //pos代表已从堆栈中取出的数目,也代表a堆栈中待取数

37                 //cur代表欲往stack堆栈中加入的新数

38 

39                 stack[top] = cur;

40                 while (pos < t && top < t)

41                 {

42                     if (a[pos] == stack[top])//如果stack栈顶数 等于 a堆栈中待取数

43                     {

44                         pos++;//则取出a堆栈中待取数

45                         top--;//并在a堆栈中往后移一位 , 同时将 stack 堆栈前移一位

46 

47                         //针对 当前stack 为空时, 则在stack堆栈中添加入新数cur

48                         if (top < 0)

49                         {

50                             top = 0;

51                             stack[top] = ++cur;

52                         }

53                     }

54                     else//否则在 stack堆栈中加入新的数

55                         stack[++top] = ++cur;

56                     

57                     //检查状态

58                     /*printf("cur =%d pos=%d top=%d flag=%d\n",cur,pos,top,flag++);

59                     for(i=0;i<=top;i++)

60                         printf("%d ",stack[i]);

61                         printf("\n");*/

62                 }

63                 if (top == t)

64                     printf("No\n");

65                 else

66                     printf("Yes\n");

67             }

68             if(t==0)

69                 break;

70         }

71     }

72     return 0;

73 }

 

除此之外,还有一个问题,比如要求堆栈中合法出栈顺序次数

 

 1 //摘自http://hi.baidu.com/onlys_c/item/be805c428dde9dd6c0a59213

 2 //整理by Jeremy Wu

 3 /*sum作为全局变量记录共可能多少种情况*/

 4 int sum = 0;

 5 /*描述:递归计算共可能出现的出栈序列,

 6 无返回值参数:inStack

 7 -- 目前存放于栈中的元素个数wait

 8 -- 目前还未进栈的元素个数out

 9 -- 目前已经出栈的元素个数num

10 -- 一共有多少个元素n*/

11 void f(int inStack, int wait, int out, int num){

12     

13     /*如果全部元素都出栈,表明有了一种新情况,总数加一*/ 

14     if (out == num)

15         sum++; /*否则继续递归衍生新的状态*/ 

16     else{

17             if (inStack > 0)/*衍生方法1:让一个元素出栈*/ 

18                 f(inStack-1, wait, out+1, num);

19             if (wait > 0)/*衍生方法2:让一个元素进栈*/ 

20                 f(inStack+1, wait-1, out, num); 

21         }

22             

23 }

24 /*用main函数调用*/

25 int main()

26 {

27     /*一共有n个元素*/ 

28     int n = 5;

29     /*刚开始时栈中有0个元素,有n个元素等待,有0个元素出了栈,共有n个元素*/

30     f(0, n, 0, n);

31     printf ("%d\n", sum);

32     return 0;

33 }

 

你可能感兴趣的:(Rails)