hdu 1022 Train Problem I(栈)
题目链接: hdu 1022 Train Problem I
题目大意: 看图片应该就知道什么意思了
样例输入:
3 123 321
3 123 312
样例输出:
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
题解思路: 思路就是用栈来模拟这个in和out的过程,判断Yes或者No是根据栈顶元素和栈顶元素下面的元素的序号大小来判断的。
代码如下:
#include<bits/stdc++.h>
using namespace std;
string r, s;
int st[20];
int rr[20], ss[20];
int ans[20];
int main()
{
int n;
int top = 0;
while(~scanf("%d", &n)){
cin >> r >> s;
for(int i = 0; i < n; i++){
rr[i] = r[i] - '0';
}
for(int i = 0; i < n; i++){
ss[s[i] - '0'] = i;
}
top = 0;
int cnt = 0;
int pos = 0;
int t = 0;
bool flag = true;
while(1){
if(cnt == n){
break;
}
else{
if(top > 1 && ss[st[top - 1]] > ss[st[top - 2]]){
flag = false;
break;
}
if(top > 0 && ss[st[top - 1]] == cnt){
top--;
cnt++;
ans[t++] = 0;
}
else{
st[top++] = rr[pos++];
ans[t++] = 1;
}
}
}
if(flag){
cout << "Yes." << endl;
for(int i = 0; i < t; i++){
if(ans[i] == 1){
cout << "in" << endl;
}
else{
cout << "out" << endl;
}
}
}
else{
cout << "No." << endl;
}
cout << "FINISH" << endl;
}
return 0;
}