-
Notifications
You must be signed in to change notification settings - Fork 0
/
1129 - Consistency Checker.cpp
executable file
·93 lines (88 loc) · 1.22 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
//1129 - Consistency Checker
//By MD.Imam Hossain Johny
//Dept. ICE, NSTU-11 batch
#include<bits/stdc++.h>
using namespace std;
#define SZ 100005
#define MX 100000000
int ans=0;
struct node
{
bool endmark;
node* next[10];
node()
{
endmark=false;
for(int i=0;i<10;i++)
{
next[i]=NULL;
}
}
}*root;
bool insert(char* str,int len)
{
node* curr=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'0';
if(curr->next[id]==NULL)
{
curr->next[id]=new node();
}
if(curr->endmark==true)
{
return false;
}
curr=curr->next[id];
}
for(int i=0;i<10;i++)
{
if(curr->next[i]!=NULL)
return false;
}
if(curr->endmark==true)
{
return false;
}
curr->endmark=true;
return true;
}
void del(node* cur)
{
for (int i = 0; i < 10; i++)
if (cur->next[i])
del(cur->next[i]);
delete (cur);
}
int main()
{
int x;
int n,len;
char number[15];
scanf("%d",&x);
for(int t=1;t<=x;t++)
{
root = new node();
ans=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",number);
len=strlen(number);
if(!insert(number,len))
{
ans=0;
}
}
if(ans)
{
printf("Case %d: YES\n",t);
}
else
{
printf("Case %d: NO\n",t);
}
del(root);
}
return 0;
}