-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path前缀码检验.cpp
86 lines (82 loc) · 1.49 KB
/
前缀码检验.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode BTree;
struct Node
{
int data;
BTree LeftChild, RightChild;
};
void CreateNewNode(BTree position,int length,int i)
{
position->LeftChild = position->RightChild = NULL;
if (i == length - 1)
position->data = 1;
else
position->data = 0;
}
int main()
{
int i,n,flag=0,length;
char code[100009];
BTree temp,tree = (BTree)malloc(sizeof(struct Node));
tree->data = 0;
tree->LeftChild = tree->RightChild = NULL;
scanf("%d\n", &n);
while (n--)
{
memset(code, 0, sizeof(code));
scanf("%s", &code);
length = strlen(code);
temp = tree;
for (i = 0; i < length; i++)
{
if (code[i] == '1')
{
if (temp->LeftChild == NULL)
{
temp->LeftChild = (BTree)malloc(sizeof(struct Node));
temp = temp->LeftChild;
CreateNewNode(temp,length,i);
}
else
{
if (temp->LeftChild->data == 1 || i == length - 1)
{
flag = 1;
break;
}
else
temp = temp->LeftChild;
}
}
else
{
if (temp->RightChild == NULL)
{
temp->RightChild = (BTree)malloc(sizeof(struct Node));
temp = temp->RightChild;
CreateNewNode(temp, length, i);
}
else
{
if (temp->RightChild->data == 1 || i == length - 1)
{
flag = 1;
break;
}
else
temp = temp->RightChild;
}
}
}
if (flag == 1)
break;
}
if (flag == 0)
printf("YES\n");
else
printf("%s\n", code);
}