标签:
贪心法:
若栈为空或者栈顶元素不等于or2[j],则入栈,否则出栈。
#include<iostream> #include<cstdio> using namespace std; int main() { int n; char or1[10]={‘\0‘}; char or2[10]={‘\0‘}; while(scanf("%d %s %s",&n,or1,or2)!=EOF) { int top=-1; char stack[10]; const char *op[20]; int cnt=0; int i=0; int j=0; while(j<n) { if(top==-1||stack[top]!=or2[j]) { if(i>=n) break; stack[++top]=or1[i++]; op[cnt++]="in"; } if(stack[top]==or2[j]) { top--; op[cnt++]="out"; j++; } } if(top==-1) { printf("Yes.\n"); for(int i=0;i<cnt;i++) printf("%s\n",op[i]); } else { printf("No.\n"); } printf("FINISH\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/4691725.html