1poj1363(栈)

来源:互联网 发布:淘宝奢侈品代购 编辑:程序博客网 时间:2024/04/29 19:29

http://poj.org/problem?id=1363

Rails
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 18749Accepted: 7463

Description

There is a famous railwaystation in PopPush City. Country there is incredibly hilly. Thestation was built in last century. Unfortunately, funds wereextremely limited that time. It was possible to establish only asurface track. Moreover, it turned out that the station could beonly a dead-end one (see picture) and due to lack of availablespace it could have only one track.
1poj1363(栈)

The local tradition is that every train arriving from the directionA continues in the direction B with coaches reorganized in someway. 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 ispossible to marshal coaches continuing in the direction B so thattheir order will be a1, a2, ..., aN. Help him and write a programthat decides whether it is possible to get the required order ofcoaches. You can assume that single coaches can be disconnectedfrom the train before they enter the station and that they can movethemselves until they are on the track in the direction B. You canalso suppose that at any time there can be located as many coachesas necessary in the station. But once a coach has entered thestation it cannot return to the track in the direction A and alsoonce it has left the station in the direction B it cannot returnback to the station.

Input

The input consists of blocks oflines. Each block except the last describes one train and possiblymore requirements for its reorganization. In the first line of theblock there is the integer N described above. In each of the nextlines of the block there is a permutation of 1, 2, ..., N. The lastline of the block contains just 0.

The last block consists of just one line containing 0.

Output

The output contains the linescorresponding to the lines with permutations in the input. A lineof the output contains Yes if it is possible to marshal the coachesin the order required on the corresponding line of the input.Otherwise it contains No. In addition, there is one empty lineafter the lines corresponding to one block of the input. There isno line in the output corresponding to the last ``null'' block ofthe input.

Sample Input

51 2 3 4 55 4 1 2 3066 5 4 3 2 100

Sample Output

YesNoYes

Source

Central Europe 1997
题意:判断一个栈是否能输出相应的数据
用白书92页的代码做的,不行。不知道为什么,按照这题多加个地方就行了,不知道为什么
#include<stdio.h>
#include<string.h>
const int MAXN=1100;
intn,target[MAXN],stack[MAXN];
int main()
{
       while(scanf("%d",&n),n)
       {
               while(scanf("%d",&target[1]),target[1])
               {
                       inttop=0;
                       intA=1,B=1;
                       int i;
                       //memset(stack,0,sizeof(stack));
                       for(i=2;i<=n;i++)
scanf("%d",&target[i]);
                       boolflag=true;
                       while(B<=n)
                       {
                               if(A==target[B])
                               {
                                       A++;
                                       B++;
                               }
                               elseif(top&&stack[top]==target[B])
                               {
                                       top--;
                                       B++;
                               }
                               elseif(A<=n)
                               {
                                       stack[++top]=A++;
                               }
                               else
                               {
                                       flag=false;
                                       break;
                               }
                       }
                       printf("%s\n",flag?"Yes":"No");
               }
               printf("\n");
       }
       return 0;
}