1. 程式人生 > >hdu1022 Train Problem I---模擬棧

hdu1022 Train Problem I---模擬棧

ron std 不能 pty 結束 type blog tac long

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

題目大意:

車的進出站問題,先給出N個火車,再按序列一的方式進站,判斷能否以序列二的方式出站,若能先輸出“Yes.”,再輸出出站步驟,以FINISH結束,若不能,輸出“No.”,仍以FINISH結束。

思路:

直接模擬棧,註意細節!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6
#include<queue> 7 #include<stack> 8 using namespace std; 9 typedef long long ll; 10 const int maxn = 1e6 + 10; 11 const int INF = 1 << 30; 12 int T, n, m; 13 int v[20]; 14 int main() 15 { 16 string s1, s2; 17 while(cin >> n >> s1 >> s2) 18 { 19 int
j = 0; 20 int tot = 0, ok = 1; 21 stack<char>q; 22 for(int i = 0; i < s1.size(); i++) 23 { 24 q.push(s1[i]); 25 v[tot++] = 1; 26 while(!q.empty() && q.top() == s2[j])//一開始將j++寫在這裏,是錯的,因為每次判斷條件都會加一 27 { 28 q.pop();
29 v[tot++] = 0; 30 j++; 31 } 32 } 33 if(j == n && tot == 2 * n)printf("Yes.\n"); 34 else printf("No.\n"); 35 if(j == n && tot == 2 * n) 36 { 37 for(int i = 0; i < tot; i++)if(v[i])printf("in\n"); 38 else printf("out\n"); 39 } 40 printf("FINISH\n"); 41 } 42 return 0; 43 }

hdu1022 Train Problem I---模擬棧