栈和队列训练题目。
#include <stdio.h>
#include <string.h> #define MAXNUM 1005 char in[MAXNUM];
char out[MAXNUM];
char stack[MAXNUM];
char order[MAXNUM]; // 1:in, 0:out int main() {
int n;
int i, j, k, tmp, flg;
int top; while (scanf("%d %s %s", &n, in, out) != EOF) {
top = k = ;
for (i=; in[i]!=out[]&&i<n; ++i) {
stack[top++] = in[i];
order[k++] = ;
}
tmp = i+;
order[k++]=; order[k++]=;
flg = ;
for (i=; i<n; ++i) {
if (top) {
if (stack[top-] == out[i]) {
--top;
order[k++] = ;
continue;
}
for (j=; j<top-; ++j) {
if (stack[j] == out[i]) {
printf("No.\nFINISH\n");
flg = ;
break;
}
}
}
if (flg)
break;
for (j=tmp; in[j]!=out[i]&&j<n; ++j) {
stack[top++] = in[j];
order[k++] = ;
}
tmp = j+;
order[k++]=; order[k++]=;
}
if (flg)
continue;
printf("Yes.\n");
for (i=; i<k; ++i)
if (order[i])
printf("in\n");
else
printf("out\n");
printf("FINISH\n");
} return ;
}