poj1363
http://poj.org/problem?id=1363
通过栈模拟,检验有无可能实现输入和输出
1 #include<stdio.h>
2 int sum;
3 int *in,*out,*stack;
4 int inpoint,outpoint,stackpoint;
5 int is_ok()
6 {
7 inpoint=-1,outpoint=0,stackpoint=-1;
8 while(outpoint<sum)
9 {
10 if(stackpoint>-1)
11 {
12 while(out[outpoint]!=stack[stackpoint]&&inpoint<sum)
13 stack[++stackpoint]=in[++inpoint];
14 if(inpoint<sum)
15 {
16 outpoint++;
17 stackpoint--;
18 }
19 else return 0;
20 }
21 else
22 {
23 if(inpoint<sum) stack[++stackpoint]=in[++inpoint];
24 else return 0;
25 }
26 }
27 return 1;
28 }
29 void ini()
30 {
31 while(scanf("%d",&sum)==1&&sum)
32 {
33 in=new int [sum];
34 out=new int [sum];
35 stack=new int [sum];
36 while(scanf("%d",&out[0])==1&&out[0])
37 {
38 for(int i=1;i<sum;i++) scanf("%d",&out[i]);
39 for(int i=1;i<=sum;i++) in[i-1]=i;
40 if(is_ok()) printf("Yes\n");
41 else printf("No\n");
42 }
43 printf("\n");
44 }
45 }
46 int main()
47 {
48 ini();
49 return 0;
50 }