[HDOJ1022]Train Problem I

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

 

手写栈,模拟出入的过程。很简单,不停入栈,如果和当前要求出站车辆相同的话就出站,否则继续入栈。

 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <set>
10 #include <stack>
11 #include <list>
12 #include <vector>
13 
14 using namespace std;
15 
16 const int maxn = 100010;
17 int n;
18 char in[maxn];
19 char out[maxn];
20 char mystack[maxn];
21 int ans[maxn];  //记下步骤
22 int top, head, stp, now;
23 
24 void init() {
25     memset(mystack, 0, sizeof(mystack));
26     memset(ans, 0, sizeof(ans));
27     stp = 0;    //步骤
28     top = 0;    //栈顶
29     head = 0;   //车头
30     now = 0;    //待出栈车辆
31 }
32 
33 void solve() {
34     while(head < n) {
35             mystack[++top] = in[head++];
36             ans[stp++] = 1;
37             while(top && mystack[top] == out[now]) {
38                 top--;
39                 now++;
40                 ans[stp++] = 0;
41             }
42     }
43     if(now == n) {
44         printf("Yes.\n");
45         for(int i = 0; i < stp; i++) {
46             if(ans[i]) {
47                 printf("in\n");
48             }
49             else {
50                 printf("out\n");
51             }
52         }
53         printf("FINISH\n");
54     }
55     else {
56         printf("No.\nFINISH\n");
57     }
58 }
59 int main() {
60     // freopen("in", "r", stdin);
61     while(~scanf("%d", &n)) {
62         scanf("%s %s", in, out);
63         init();
64         solve();
65     }
66 }

 

posted @ 2015-09-04 01:05  Kirai  阅读(174)  评论(0编辑  收藏  举报