题意:火车站有一个铁轨,出口和入口是同一个。给定火车入站序列,问能否调成成出站序列,如果能,输出出站操作。
题解:模拟栈即可,如果栈空,则从入站序列取出首元素压栈,如果栈顶元素与出站序列首元素不同,则从入站序列取出首元素压栈,如果栈顶元素与出站序列首元素相同,则出栈,更新出站序列首元素。如果入站序列已空,而还在尝试取出首元素压栈,则判定为不能调整。
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<string>
5 #include<vector>
6 #include<stack>
7 using namespace std;
8 stack<int> st;
9 int main()
10 {
11 int n;
12 while(scanf("%d",&n)!=EOF)
13 {
14 while(!st.empty())
15 {
16 st.pop();
17 }
18 vector<string> res;
19 res.clear();
20 string s1,s2;
21 cin>>s1>>s2;
22 int tip1=0,tip2=0;
23 while(tip2<s2.length())
24 {
25 if(st.empty()||st.top()!=s2[tip2]-'0')
26 {
27 if(tip1==s1.length())
28 {
29 break;
30 }
31 st.push(s1[tip1]-'0');
32 res.push_back("in");
33 tip1+=1;
34 }
35 else
36 {
37 res.push_back("out");
38 st.pop();
39 tip2+=1;
40 }
41 }
42 if(tip2!=s2.length())
43 {
44 printf("No.\n");
45 }
46 else
47 {
48 printf("Yes.\n");
49 vector<string>::iterator it;
50 for(it=res.begin();it!=res.end();it++)
51 {
52 cout<<*it<<endl;
53 }
54 }
55 printf("FINISH\n");
56 }
57 return 0;
58 }