hdu--1022--Train Problem I

题目链接

 1 #include <iostream> 
 2 #include <stack>
 3 using namespace std;
 4 int main() {
 5     int n;
 6     while (cin>>n) {
 7         string a, b;
 8         cin>>a>>b;
 9         string tag;
10         stack<int> s;
11         for (int i=0, j=0; i<a.size(); i++) {
12         /*
13             如果a[i]  = b[j]说明是进去再出来,如果不相等,说明进去没出来
14             如果遇到相等的,那么判断栈空,不空则判断后面出站的是否是栈顶元素,如果是继续出栈
15             123 321
16             匹配到3,判断2=栈顶2,1=栈顶1,判断完毕,YES 
17         */ 
18             if (a[i] != b[j]) {
19                 tag += "0";
20                 s.push(a[i]);
21             } else {
22                 tag += "01";
23                 j++;
24                 while (!s.empty()) {
25                     if (s.top() == b[j]) {
26                         tag += "1";
27                         s.pop();
28                         j++;
29                     } else break;
30                 }
31             }
32         }
33         if (s.empty()) {
34             cout<<"Yes."<<endl;
35             for (int i=0; i<tag.size(); i++) {
36                 if (tag[i] == '1') {
37                     cout<<"out
";
38                 } else {
39                     cout<<"in
";
40                 }
41             }
42             cout<<"FINISH
";
43         } else {
44             cout<<"No.
FINISH
";
45         }
46     }
47     return 0;
48 }
原文地址:https://www.cnblogs.com/langyao/p/7251932.html