hdu 1022 Train Problem I(栈)
生活随笔
收集整理的這篇文章主要介紹了
hdu 1022 Train Problem I(栈)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
標記現在已經匹配到o1的第幾列車了,o2從頭開始,如果不匹配把o1就放入棧中,匹配后出棧并比較棧頂與o1的下一輛,匹配繼續出,不匹配就繼續進棧
?
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <stack> 5 using namespace std; 6 int main() 7 { 8 int n; 9 while (scanf("%d", &n) != EOF) 10 { 11 string o1, o2; 12 cin >> o1 >> o2; 13 stack<int> s; 14 bool flag[1000] = {0}; 15 int i = 0, j = 0, k = 1; //i,j,k分別為O1,O2,result下標 16 s.push(o1[0]); 17 flag[0] = 1; 18 while (i < n && j < n) 19 { 20 if (!s.empty() && s.top() == o2[j]) 21 { 22 s.pop(); 23 flag[k++] = 0; 24 ++j; 25 } 26 else 27 { 28 s.push(o1[++i]); 29 flag[k++] = 1; 30 } 31 } 32 if (i == n) 33 cout << "No." << endl; 34 else 35 { 36 cout << "Yes." << endl; 37 for (int i = 0; i < k; ++i) 38 if (flag[i]) 39 cout << "in" << endl; 40 else 41 cout << "out" << endl; 42 } 43 cout << "FINISH" << endl; 44 } 45 system("pause"); 46 return 0; 47 }?
轉載于:https://www.cnblogs.com/PegasusWang/archive/2013/03/27/2983944.html
總結
以上是生活随笔為你收集整理的hdu 1022 Train Problem I(栈)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css阻止input select默认事
- 下一篇: Ruby学习之类2