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
#include <cstdio> #include <iostream> #include <cstring> #include <stack> using namespace std; int main() { int n, lenb, len; bool flag[20]; //用于标记出入栈情况 char a[20], b[20]; stack<char> s; while (~scanf("%d%s%s", &n, a, b)) { memset(flag, false, sizeof(flag)); while (!s.empty()) s.pop(); len = lenb = 0; for (int i = 0; i < n; i++) { s.push(a[i]); flag[len++] = true; //遇到符合b中序列顺序的才出栈标记,否则继续入栈 while (!s.empty() && s.top() == b[lenb]) { s.pop(); flag[len++] = false; lenb++; } } //最后看b这个序列是否都匹配了 if (lenb == n) { puts("Yes."); for (int i = 0; i < len; i++) { if (flag[i]) { puts("in"); } else { puts("out"); } } } else { puts("No."); } puts("FINISH"); } return 0; }