3 123 321 3 123 312
Yes. in in in out out out FINISH No. FINISHFor the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".HintHint
栈的模拟,用2个队列记录进出顺序,每次进火车时判断能否发车,并记录顺序,最后判断火车站是否为空即可。
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <ctime> #include <cassert> #include<string.h> using namespace std; #define LL long long int main() { <span style="white-space:pre"> </span>int n, i; <span style="white-space:pre"> </span>while (scanf("%d", &n) != EOF) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>stack<int>station; <span style="white-space:pre"> </span>char inc[20], outc[20]; <span style="white-space:pre"> </span>queue<int>in, out; <span style="white-space:pre"> </span>queue<string>order; <span style="white-space:pre"> </span>scanf("%s%s", inc,outc); <span style="white-space:pre"> </span>for (i = 0; i < n; i++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>in.push(inc[i] - '0'); <span style="white-space:pre"> </span>out.push(outc[i] - '0'); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>while (in.size()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>station.push(in.front()); <span style="white-space:pre"> </span>in.pop(); <span style="white-space:pre"> </span>order.push("in");<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>while (station.size()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>if (station.top() != out.front())break; <span style="white-space:pre"> </span>station.pop(); <span style="white-space:pre"> </span>out.pop(); <span style="white-space:pre"> </span>order.push("out");<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>if (!station.empty()) <span style="white-space:pre"> </span>printf("No.\nFINISH\n"); <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>printf("Yes.\n"); <span style="white-space:pre"> </span>while (!order.empty()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>cout << order.front() << endl; <span style="white-space:pre"> </span>order.pop(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>printf("FINISH\n"); <span style="white-space:pre"> </span>}<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} return 0; }