-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode1049.c
More file actions
60 lines (55 loc) · 1.12 KB
/
code1049.c
File metadata and controls
60 lines (55 loc) · 1.12 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*******************************************
http://hihocoder.com/problemset/problem/1049
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
char c;
struct node *left;
struct node *right;
}node_t;
node_t* tree(const char *str1,const char *str2)
{
int len = strlen(str1);
if(len == 0)
return NULL;
node_t *node = (node_t*)malloc(sizeof(node_t));
node->c = str1[0];
char lpre[30];
char lmid[30];
memset(lpre,0,30);
memset(lmid,0,30);
int count=0;
while(str2[count] != str1[0])
count++;
strncpy(lpre,str1+1,count);
strncpy(lmid,str2,count);
node->left = tree(lpre,lmid);
char rpre[30];
char rmid[30];
memset(rpre,0,30);
memset(rmid,0,30);
strncpy(rpre,str1+1+count,len-count-1);
strncpy(rmid,str2+count+1,len-count-1);
node->right = tree(rpre,rmid);
return node;
}
void ans(node_t *node)
{
if(node->left != NULL)
ans(node->left);
if(node->right != NULL)
ans(node->right);
printf("%c",node->c);
}
int main()
{
node_t *root;
char str1[30];
char str2[30];
scanf("%s%s",str1,str2);
root = tree(str1,str2);
ans(root);
return 0;
}