-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata 20.cpp
More file actions
217 lines (200 loc) · 3.86 KB
/
data 20.cpp
File metadata and controls
217 lines (200 loc) · 3.86 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char prefix[40000]={0};
typedef struct BiNode {
int elem;
BiNode *lkid, *rkid;
}BiNode, *BiTree;
int addBiTree(BiTree T, char *str) {
if (strlen(str) == 0) {
T->elem=1;
if (T->lkid == NULL && T->rkid == NULL) {
return 1;
}
else {
return -1;
}
}
else if (strlen(str) >= 1) {
if (T->elem == 1) return -1;
int len=strlen(str);
char c=str[0];
for (int i = 0; i < len; i++) {
str[i] = str[i + 1];
}
if (c == '0') {
if (T->lkid != NULL) {
return addBiTree(T->lkid, str);
}
BiTree tmp;
tmp = (BiTree)malloc(sizeof(BiNode));
tmp->lkid = NULL;
tmp->rkid = NULL;
int index = addBiTree(tmp, str);
T->lkid = tmp;
return index;
}
else if (c == '1') {
if (T->rkid != NULL) {
return addBiTree(T->rkid, str);
}
BiTree tmp;
tmp = (BiTree)malloc(sizeof(BiNode));
tmp->lkid = NULL;
tmp->rkid = NULL;
int index = addBiTree(tmp, str);
T->rkid = tmp;
return index;
}
}
}
int main(void) {
BiTree T;
T = (BiTree)malloc(sizeof(BiNode));
T->elem = 0;
T->lkid = NULL;
T->rkid = NULL;
char tmp[40000]={0};
char temp[40000]={0};
int n = 0;
int flag = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", temp);
if(flag == 1) continue;
for(int g=0;g<=strlen(temp);g++){
tmp[g]=temp[g];
}
if (addBiTree(T, temp) == -1 && flag == 0) {
flag = 1;
for(int m=0;m<=strlen(tmp);m++){
prefix[m]=tmp[m];
}
}
}
if (flag == 1) printf("%s\n", prefix);
else if (flag == 0) printf("YES\n");
}
/*
注释里的代码是一位学长(也可能是个学姐...)的代码,总之比我的强,可以更快地处理更大的数据,我觉得我尽力了,因为我是的的确确使用链表来写的。
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
unsigned int flag = 0, lson = 0, rson = 0;
}H_Tree[50000];
char str[100000];
int main()
{
int num, StrLen;
int TreeP, TreeNum = 2;//TreeP:point for H_Tree (指针) TreeNum:the nums of H_Tree node (拓展器)
int Flag = 0;//0-YES,1-NO
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
scanf("%s", str);
if (Flag == 1) //如果Flag为1,说明第一次出现
continue;
StrLen = strlen(str);
TreeP = 1;//从1开始,0作为空
for (int j = 0; j < StrLen; j++)
{
if (H_Tree[TreeP].flag == 1)//说明别人是自己的前缀
{
Flag = 1;
printf("%s\n", str);
break;
}
if (j < StrLen - 1) //建树
{
//左子树
if (str[j] == '0')
{
if (H_Tree[TreeP].lson == 0)//左节点为空
{
H_Tree[TreeP].lson = TreeNum;//拓展左节点
TreeP = TreeNum++;//指针移动后拓展器自增
continue;
}
else
{
TreeP = H_Tree[TreeP].lson;
continue;
}
}
//右子树
else
{
if (H_Tree[TreeP].rson == 0)
{
H_Tree[TreeP].rson = TreeNum;
TreeP = TreeNum++;
continue;
}
else
{
TreeP = H_Tree[TreeP].rson;
continue;
}
}
}
else
{
if (str[j] == '0')
{
if (H_Tree[TreeP].lson == 0) //为空
{
H_Tree[TreeP].lson = TreeNum;
H_Tree[TreeNum].flag = 1;//标记最终建树完成
TreeNum++;
continue;
}
else
{
TreeP = H_Tree[TreeP].lson;
if (H_Tree[TreeP].lson == 0 && H_Tree[TreeP].rson == 0)//判定是否有重复输入的
H_Tree[TreeP].flag = 1;
else //是别人的前缀
{
Flag = 1;
printf("%s\n", str);
break;
}
}
}
else
{
if (H_Tree[TreeP].rson == 0)
{
H_Tree[TreeP].rson = TreeNum;
H_Tree[TreeNum].flag = 1;
TreeNum++;
continue;
}
else
{
TreeP = H_Tree[TreeP].rson;
if (H_Tree[TreeP].lson == 0 && H_Tree[TreeP].rson == 0)
{
H_Tree[TreeP].flag = 1;
break;
}
else
{
Flag = 1;
printf("%s\n", str);
break;
}
}
}
}
}
}
if (!Flag)
printf("YES\n");
system("pause");
return 0;
}
*/