/
ps_double_encoding_BF.cpp
230 lines (199 loc) · 3.44 KB
/
ps_double_encoding_BF.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
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
/*
Need to be optimized
*/
#include <stdio.h>
#define SZ_S 1001
struct RLE {
char ch;
int len;
};
struct RLE a[SZ_S], b[SZ_S], c[SZ_S];
int a_cnt, b_cnt, c_cnt;
int matched_cnt;
bool no_match;
int ed_idx;
static void
clear_buf()
{
int i;
for (i = 0; i < SZ_S; i++)
{
a[i].ch = b[i].ch = c[i].ch = 0;
a[i].len = b[i].len = c[i].len = 0;
}
}
static void
print_target_words(int a_cnt, int b_cnt, int c_cnt)
{
int i;
for (i = 0; i < a_cnt; i++)
printf("%c%d ", a[i].ch, a[i].len);
printf("\n");
for (i = 0; i < b_cnt; i++)
printf("%c%d ", b[i].ch, b[i].len);
printf("\n");
for (i = 0; i < c_cnt; i++)
printf("%c%d ", c[i].ch, c[i].len);
printf("\n");
}
static void
input_target_words()
{
int abc_cnt = 0;
// input
do {
char x;
int l;
struct RLE tmp;
scanf(" %c", &x);
if (x == '$')
{
abc_cnt++;
if (abc_cnt != 3) continue;
else break;
}
else
{
scanf("%d", &l);
if (abc_cnt == 0)
{
tmp.ch = x, tmp.len = l;
a[a_cnt++] = tmp;
}
else if (abc_cnt == 1)
{
tmp.ch = x, tmp.len = l;
b[b_cnt++] = tmp;
}
else
{
tmp.ch = x, tmp.len = l;
c[c_cnt++] = tmp;
}
}
} while (1);
}
static void
check_matched()
{
int i, j, k;
int nx = 0;
for (i = 0; i < b_cnt; i++)
{
for (j = nx; j < a_cnt; j++)
{
if (b[i].ch == a[j].ch)
{
if (a[j].len >= b[i].len)
{
a[j].len = a[j].len - b[i].len;
//if (st_idx == -1)
//st_idx = j;
nx = j + 1;
ed_idx = j;
matched_cnt++;
break;
}
}
if (matched_cnt > 0 && matched_cnt < b_cnt)
{
no_match = true;
break;
}
}
if (no_match) break;
}
}
static void
print_outputs()
{
int i, j, k;
if (no_match || (matched_cnt < b_cnt))
{
// just print A only
for (i = 0; i < a_cnt; i++)
printf("%c %d ", a[i].ch, a[i].len);
}
else if (matched_cnt == b_cnt)
{
// replace with C from st_idx to ed_idx
int cnt = 0;
bool rep_flag = false;
struct RLE replaced[SZ_S];
for (i = 0; i < a_cnt; i++)
{
if (a[i].len == 0 && !rep_flag)
{
//printf("->> D1:\n");
for (j = 0; j < c_cnt; j++)
{
if (replaced[cnt - 1].ch == c[j].ch)
replaced[cnt - 1].len += c[j].len;
else
replaced[cnt++] = c[j];
}
rep_flag = true;
}
else
{
if (ed_idx > -1 && i == ed_idx)
{
if (!rep_flag)
{
for (j = 0; j < c_cnt; j++)
{
//printf("->>>>> D4:\n");
if (replaced[cnt - 1].ch == c[j].ch)
replaced[cnt - 1].len += c[j].len;
else
replaced[cnt++] = c[j];
}
}
//printf("->>> D2:\n");
if (replaced[cnt - 1].ch == a[i].ch)
replaced[cnt - 1].len += a[i].len;
else
replaced[cnt++] = a[i];
}
else if (a[i].len == 0)
{
// do nothing
}
else
{
//printf("->>>> D3:\n");
replaced[cnt++] = a[i];
}
}
}
for (k = 0; k < cnt; k++)
printf("%c %d ", replaced[k].ch, replaced[k].len);
}
printf("$\n");
}
int main()
{
freopen("double_encodings.txt", "r", stdin);
setbuf(stdout, NULL);
int T, tc, i, j, k;
scanf("%d", &T);
for (tc = 1; tc <= T; tc++)
{
// input:
a_cnt = 0, b_cnt = 0, c_cnt = 0;
input_target_words();
//print_target_words(a_cnt, b_cnt, c_cnt);
// solve:
matched_cnt = 0;
no_match = false;
ed_idx = -1;
/* check matched with B and C*/
check_matched();
//print_target_words(a_cnt, b_cnt, c_cnt);
// output:
print_outputs();
// clear buf:
clear_buf();
}
return 0;
}