forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1056(AC).cpp
117 lines (105 loc) · 1.49 KB
/
POJ1056(AC).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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 100
#define MAXLEN 100
char *codes[MAXNUM];
int prefix(char *s, char *t)
{
char tmp;
int lens;
int lent;
if(strlen(s) > strlen(t))
{
return prefix(t, s);
}
else
{
lens = strlen(s);
lent = strlen(t);
tmp = t[lens];
t[lens] = 0;
if(strcmp(s, t) == 0)
{
t[lens] = tmp;
return 1;
}
else
{
t[lens] = tmp;
return 0;
}
}
}
int main()
{
int counter;
int casecounter;
int i;
int j;
char *tmp;
for(i = 0; i < MAXNUM; i++)
{
codes[i] = (char *)malloc(MAXLEN * sizeof(char));
}
casecounter = 0;
while(1)
{
casecounter++;
counter = 0;
while(1)
{
if(gets(codes[counter]) == NULL)
{
return 0;
}
if(strcmp(codes[counter], "9") == 0)
{
break;
}
else
{
counter++;
}
}
if(counter <= 1)
{
printf("Set %d is immediately decodable\n", casecounter);
}
else
{
for(i = 0; i < counter - 1; i++)
{
for(j = i + 1; j < counter; j++)
{
if(strcmp(codes[i], codes[j]) > 0)
{
tmp = codes[i];
codes[i] = codes[j];
codes[j] = tmp;
}
}
}
for(i = 0; i < counter - 1; i++)
{
if(prefix(codes[i], codes[i + 1]))
{
break;
}
}
if(i == counter - 1)
{
printf("Set %d is immediately decodable\n", casecounter);
}
else
{
printf("Set %d is not immediately decodable\n", casecounter);
}
}
}
for(i = 0; i < MAXNUM; i++)
{
free(codes[i]);
}
return 0;
}