您的位置:首页 > 其它

USACO 3.4 American Heritage

2017-01-02 17:19 489 查看
American Heritage

Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear `tree in-order' and `tree pre-order' notations.

Your job is to create the `tree post-order' notation of a cow's heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes.

Here is a graphical representation of the tree used in the sample input and output:

C
/   \
/     \
B       G
/ \     /
A   D   H
/ \
E   F

The in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree.

The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree.

The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root.

PROGRAM NAME: heritage

INPUT FORMAT

Line 1: The in-order representation of a tree.
Line 2: The pre-order representation of that same tree.

SAMPLE INPUT (file heritage.in)

ABEDFCHG
CBADEFGH

OUTPUT FORMAT

A single line with the post-order representation of the tree.

SAMPLE OUTPUT (file heritage.out)

AEFDBHGC 

—————————————————————
用前序遍历中某个字母的位置可以得到它的左子树的长度
然后就可以了
存代码
1 /*
2 ID: ivorysi
3 PROG: heritage
4 LANG: C++
5 */
6 #include <iostream>
7 #include <cstdio>
8 #include <cstring>
9 #include <algorithm>
10 #include <queue>
11 #include <set>
12 #include <vector>
13 #include <string.h>
14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
18 #define inf 0x3f3f3f3f
19 #define MAXN 400005
20 #define ivorysi
21 #define mo 97797977
22 #define ha 974711
23 #define ba 47
24 #define fi first
25 #define se second
26 #define pii pair<int,int>
27 using namespace std;
28 typedef long long ll;
29 int id[30];
30 int root,lson[30],rson[30],fa[30];
31 string str;
32 int dfs(string s,int fa) {
33     if(s.length()<1) return 0;
34     int u=s[0]-'A'+1;
35     if(s.length()==1) {return u;}
36     lson[u]=dfs(s.substr(1,id[u]-id[fa]-1),fa);
37     rson[u]=dfs(s.substr(id[u]-id[fa]),u);
38     return u;
39 }
40 void ans(int u) {
41     if(u==0) return;
42     ans(lson[u]);
43     ans(rson[u]);
44     printf("%c",u+'A'-1);
45 }
46 void init() {
47     cin>>str;
48     xiaosiji(i,0,str.length()) {
49         id[str[i]-'A'+1]=i+1;
50     }
51     cin>>str;
52     root=str[0]-'A'+1;
53 }
54 void solve() {
55     init();
56     dfs(str,0);
57     ans(root);
58     puts("");
59 }
60 int main(int argc, char const *argv[])
61 {
62 #ifdef ivorysi
63     freopen("heritage.in","r",stdin);
64     freopen("heritage.out","w",stdout);
65 #else
66     freopen("f1.in","r",stdin);
67 #endif
68     solve();
69 }

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: