P1087-FBI树

 1 #include <bits/stdc++.h>
 2 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 3 typedef long long ll;
 4 using namespace std;
 5 string S;
 6 int N;
 7 inline ll read()
 8 {
 9     ll ans = 0;
10     char ch = getchar(), last = ' ';
11     while(!isdigit(ch)) last = ch, ch = getchar();
12     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
13     if(last == '-') ans = -ans;
14     return ans;
15 }
16 inline void write(ll x)
17 {
18     if(x < 0) x = -x, putchar('-');
19     if(x >= 10) write(x / 10);
20     putchar(x % 10 + '0');
21 }
22 struct TreeNode
23 {
24     char val;
25     TreeNode *left;
26     TreeNode *right;
27 };
28 char judge(int le,int ri)
29 {
30     bool oflag = false;
31     bool zflag = false;
32     _for(i,le,ri+1)
33     {
34         if(S[i]=='0')
35             zflag = true;
36         else if(S[i]=='1')
37             oflag = true;
38     }
39     if(zflag && !oflag)
40         return 'B';
41     else if(!zflag && oflag)
42         return 'I';
43     else
44         return 'F';
45     return -1;
46 }
47 TreeNode* build(int le,int ri)
48 {
49     TreeNode* tmp = (TreeNode*)malloc(sizeof(TreeNode));
50     int mi = (le+ri)/2;
51     tmp->val = judge(le,ri);
52     if(le!=ri)
53     {
54         tmp->left = build(le,mi);
55         tmp->right = build(mi+1,ri);
56     }
57     else
58         tmp->left = tmp->right = NULL;
59     return tmp;
60 }
61 void postorder(TreeNode *root)
62 {
63     if(!root)
64         return ;
65     postorder(root->left);
66     postorder(root->right);
67     printf("%c",root->val);
68 }
69 int main()
70 {
71     TreeNode* root;
72     scanf("%d",&N);
73     cin >> S;
74     root = build(0,pow(2,N)-1);
75     postorder(root);
76     return 0;
77 }

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/11286525.html