forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1129-consistency_checker.cpp
123 lines (81 loc) · 1.68 KB
/
1129-consistency_checker.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
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
#include <bits/stdc++.h>
#define ll long long
#define null NULL
#define MAX 100100
using namespace std;
vector <ll > vk;
struct node {
int end;
struct node* links[10];
};
struct node *head;
struct node* initilizer()
{
struct node * naah = new node();
naah -> end = -1;
for(int i =0;i < 10;i++){
naah -> links[i] = NULL;
}
return naah;
}
int addtotrie(struct node* curr, string s,int index)
{
if(index == s.size())
{
if(curr -> end == 0 || curr -> end == 1)
return 0;
curr -> end = 1;
return 1;
}
if(curr -> end == 1)
return 0;
int num = s[index] - '0';
if(curr -> links[num] == NULL){
curr -> end = 0;
curr -> links[num] = initilizer();
return addtotrie(curr -> links[num],s,index+1);
}else {
return addtotrie(curr -> links[num],s,index+1);
}
}
void freememory(struct node* curr)
{
if(curr != NULL){
for(int i =0;i < 10;i++){
if(curr -> links[i] != NULL)
freememory(curr -> links[i]);
}
free(curr);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/sameer/sam.sam","r",stdin);
#endif
ios_base::sync_with_stdio(false);
ll i,j,k,n,m,t,cont,a,b;
cin >> t;
ll cases = t;
string s,sa;
int flg = 0;
while(t--){
cin >> n;
head = initilizer();
flg = 0;
for(i =0;i < n;i++) {
cin >> s;
k =addtotrie(head,s,0);
if(k == 0) {
//cout <<"Case " << cases-t << ": NO" << endl;
flg = 1;
}
}
if(flg == 0)
cout <<"Case " << cases-t << ": YES" << endl;
else
cout <<"Case " << cases-t << ": NO" << endl;
freememory(head);
}
return 0;
}