題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=477
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <bits/stdc++.h> using namespace std;
void solve(string pre, string in){ if(pre.empty() || in.empty()) return; char root = pre[0]; int in_root_idx = in.find(root); string in_left = in.substr(0, in_root_idx); string in_right = in.substr(in_root_idx+1);
string pre_left = pre.substr(1, in_root_idx); string pre_right = pre.substr(in_root_idx+1);
solve(pre_left, in_left); solve(pre_right, in_right); printf("%c", root); }
int main(){ string pre, in; while(cin >> pre >> in){ solve(pre, in); printf("\n"); } return 0; }
|