0)
根据先序和中序建树,然后再后续遍历应该也可以。
大体看了看其他人的代码行数,决定找规律做:根据先序往后走,返回当前字符所在中序中的位置,如果前后都没有入栈(前后都入栈或者是边界则出栈)则入栈,将先序所有字符走过一遍后,如果栈不为空,再POP,直到栈为空。
1)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
string str1;
string str2;
int bj[30];
stack <char> ss;
int len,cur;
int Find(char temp){
//char temp=str1[x];
for(int i=0;i<str2.size();i++){
if(temp==str2[i]){
return i;
}
}
return -1;
}
int main(){
while(cin>>str1>>str2){
len=str1.size();
cur=0;
memset(bj,0,sizeof(bj));
int pos;
for(int i=0;i<len;i++){
pos=Find(str1[i]);
if(pos==-1){
cout<<"chucuo"<<endl;
break;
}
if((pos-1<0||bj[pos-1]==1)&&(pos+1>(len-1)||bj[pos+1]==1)){
cout<<str1[i];
bj[pos]=1;
char temp=ss.top();
pos=Find(temp);
while(!ss.empty()&&((pos-1<0||bj[pos-1]==1)&&(pos+1>(len-1)||bj[pos+1]==1))){
cout<<temp;
ss.pop();
if(!ss.empty()){
temp=ss.top();
pos=Find(temp);
}
}
}
else{
ss.push(str1[i]);
bj[pos]=1;
}
}
while(!ss.empty()){
char temp=ss.top();
ss.pop();
cout<<temp;
}
cout<<endl;
}
return 0;
}
2)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=118967#problem/C