| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
so easy,一把AC的感觉好爽,虽然这题非常之水。。。C++编译204K,0MSAC的代码
#include<iostream>
#include<Cstdio>
using namespace std;
char pre[500], in[500];
struct Binary
{
char ch;
Binary *left, *right;
};
Binary* build(char a1[], char a2[], int length)
{
Binary *root = new Binary();
root -> ch = a1[0];
root -> left = NULL;
root -> right = NULL;
int i = 0;
while(a2[i] != a1[0])
i ++;
int leftLength = i;
int rightLength = length - i - 1;
if(leftLength > 0)
root -> left = build(a1 + 1, a2, leftLength);
if(rightLength > 0)
root -> right = build(a1 + 1 + leftLength, a2 + 1 + leftLength, rightLength);
return root;
}
void visit(Binary* root)
{
if(root->left != NULL)
visit(root->left);
if(root->right != NULL)
visit(root->right);
cout << root->ch;
}
int main()
{
while(cin >> pre)
{
cin >> in;
int i = 0;
while(1)
{
if(pre[i] == '\0')
break;
i++;
}
Binary* root = build(pre, in, i);
visit(root);
cout << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator