-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJUKEBOX.c
285 lines (240 loc) · 5.35 KB
/
JUKEBOX.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
SPOJ SOLUTION
Copyright (c) 2011-2013 Péricles Lopes Machado (gogo40)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/*
Titulo do problema: JUKEBOX
Link: http://br.spoj.com/problems/JUKEBOX/
Solução: KMP, HASHING e BUSCA EXAUSTIVA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INF 1000000
typedef struct{
char musica[31], artista[31];
int len_m, len_a, i_a;
}Playlist;
char id[13][31];
int marc[13];
int key[13];
int tam;
int hash(const char* v, int len){
int p, i, pv;
pv=0;
for(i=0;i<len;i++) pv+=(v[i]);
pv%=13;
p=pv;
for(;;){
if(key[p]==-1){
strcpy(id[p],v);
key[p]=pv;
marc[p]=tam;
tam++;
return p;
}else if(key[p]==pv){
if(!strcmp(id[p],v)) return p;
}
p=(p+1)%13;
}
}
int lengsm[30][450];
char gsm[30][450][31];
int ngsm[30];
int lengsa[30][450];
char gsa[30][450][31];
int ngsa[30];
int libA[13];
int ans, n;
Playlist P[31];
char match[31];
/*1 custo deu usar o artista se ele for permitido
0 custo deu n�o usar o artista se ele for permitido
*/
int calc[31][2], A;
int sum0[13];
int diff[13];
int kmpNext[32];
void prekmp(char *x, int m) {
int i, j;
i = 0;
j = kmpNext[0] = -1;
while (i < m) {
while (j > -1 && x[i] != x[j])
j = kmpNext[j];
i++;
j++;
if (x[i] == x[j])
kmpNext[i] = kmpNext[j];
else
kmpNext[i] = j;
}
}
int kmp(char *y, int n, char* x, int m) {
int i, j;
if(n<m) return 0;
i = j = 0;
while (j < n) {
while (i > -1 && x[i] != y[j])
i = kmpNext[i];
i++; j++;
if (i >= m) {
return 1;
i = kmpNext[i];
}
}
return 0;
}
/*Verifica que golden strings s�o uteis de verdade com base nos artistas liberados*/
void find_min()
{
int i, j, k, len;
int lmin, sum, s;
for(i=0;i<tam;i++) sum0[i]=0, diff[i]=INF;
A=0;
for(i=0;i<n;i++){
lmin=INF;
for(j=0;j<ngsm[i];j++){
len=lengsm[i][j];
prekmp(gsm[i][j],len);
for(k=0;k<n;k++){
if(k!=i){
if(libA[P[k].i_a])
if(kmp(P[k].artista,P[k].len_a,gsm[i][j],len)) break;
}
}
if(k==n) break;
}
if(j<ngsm[i]) lmin=len;
calc[i][0]=lmin;
if(libA[P[i].i_a]){
for(j=0;j<ngsa[i];j++){
len=lengsa[i][j];
prekmp(gsa[i][j],len);
for(k=0;k<n;k++){
if(k!=i){
if(libA[P[k].i_a] && P[k].i_a!=P[i].i_a)
if(kmp(P[k].artista,P[k].len_a,gsa[i][j],len)) break;
}
}
if(k==n) break;
}
if(j<ngsa[i])
if(lmin>len) lmin=len;
}
A+=lmin;
if(lmin==INF) break;
if(A>=ans) break;
calc[i][1]=lmin;
sum0[P[i].i_a]+=calc[i][0];
if(calc[i][1]-calc[i][0]<diff[P[i].i_a]) diff[P[i].i_a]=calc[i][1]-calc[i][0];
}
if(i==n){
sum=0;
for(i=0;i<tam;i++){
if(libA[i]){
s=sum0[i]+diff[i];
if(s<INF) sum+=s;
else{ sum=INF; break;}
}else{
s=sum0[i];
if(s<INF) sum+=s;
else {sum=INF; break;}
}
if(sum>=ans) break;
}
if(sum<ans){ ans=sum; }
}
}
/*Testa todas configura��es de artistas liberados*/
void bckt(int io)
{
find_min();
for(;io<tam;io++){
libA[io]=1;
bckt(io+1);
libA[io]=0;
}
}
/*Encontra todas golden strings*/
void find_gs()
{
int i, len, j, lim, p, l, k;
for(i=0;i<n;i++){
for(len=1;len<=P[i].len_m;len++){
lim=P[i].len_m-len+1;
for(j=0;j<lim;j++){
for(p=0,l=j;p<len;p++, l++)
match[p]=P[i].musica[l];
match[p]='\0';
prekmp(match,len);
for(k=0;k<n;k++){
if(k!=i){
if(kmp(P[k].musica,P[k].len_m,match,len)) break;
}
}
if(k==n){
strcpy(gsm[i][ngsm[i]],match);
lengsm[i][ngsm[i]]=len;
ngsm[i]++;
}
}
}
for(len=1;len<=P[i].len_a;len++){
lim=P[i].len_a-len+1;
for(j=0;j<lim;j++){
for(p=0,l=j;p<len;p++, l++)
match[p]=P[i].artista[l];
match[p]='\0';
prekmp(match,len);
for(k=0;k<n;k++){
if(k!=i){
if(kmp(P[k].musica,P[k].len_m,match,len)) break;
}
}
if(k==n){
strcpy(gsa[i][ngsa[i]],match);
lengsa[i][ngsa[i]]=len;
ngsa[i]++;
}
}
}
}
}
int main()
{
int i;
while(scanf("%d\n",&n),n){
tam=0;
for(i=0;i<13;i++) { libA[i]=0; key[i]=id[i][0]=-1; }
for(i=0;i<n;i++){
scanf("%s\n",P[i].musica); P[i].len_m=strlen(P[i].musica);
scanf("%s\n",P[i].artista); P[i].len_a=strlen(P[i].artista);
P[i].i_a=marc[hash(P[i].artista,P[i].len_a)];
ngsa[i]=ngsm[i]=0;
}
ans=INF;
find_gs();
bckt(0);
printf("%d\n",ans);
}
return 0;
}