forked from Advanced-Programming-Course/Midterm-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap.java
291 lines (289 loc) · 6.9 KB
/
ap.java
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
282
283
284
285
286
287
288
289
290
291
import java.util.Scanner;
import java.util.Random;
class Machine {
int n;
int[][] num;
String first;
public Machine(int k, String s) {
Random rand = new Random();
n = k;
num = new int[k + 5][k + 5];
first = s;
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
num[i][j] = rand.nextInt(5) + 1;
}
private String b1(String s) {
String res = "";
for (int i = 0; i < s.length(); i++)
res = s.charAt(i) + res;
return res;
}
private String b2(String s) {
String res = "";
for (int i = 0; i < s.length(); i++)
res = res + s.charAt(i) + s.charAt(i);
return res;
}
private String b3(String s) {
return s + s;
}
private String b4(String s) {
String res = "" + s.charAt(s.length() - 1);
for (int i = 0; i < s.length() - 1; i++)
res += s.charAt(i);
return res;
}
private String b5(String s) {
String res = "";
int n;
for (int i = 0; i < s.length(); i++) {
n = 'z' - (int) s.charAt(i);
res = res + (char) (n + 'a');
}
return res;
}
private String w1(String s1, String s2) {
String res = "";
boolean fl = false;
if (s1.length() > s2.length()) {
fl = true;
String tmp = s1;
s1 = s2;
s2 = tmp;
}
for (int i = 0; i < s1.length(); i++)
if (fl == false)
res = res + s1.charAt(i) + s2.charAt(i);
else
res = res + s2.charAt(i) + s1.charAt(i);
for (int i = s1.length(); i < s2.length(); i++)
res = res + s2.charAt(i);
return res;
}
private String w2(String s1, String s2) {
String res = "";
for (int i = s2.length() - 1; i >= 0; i--)
res = res + s2.charAt(i);
return s1 + res;
}
private String w3(String s1, String s2) {
String res = "";
for (int i = 0, j = s2.length() - 1; ; i++, j--)
if (i >= s1.length() && j < 0)
break;
else if (i >= s1.length() && j >= 0)
res = res + s2.charAt(j);
else if (i < s1.length() && j < 0)
res = res + s1.charAt(i);
else
res = res + s1.charAt(i) + s2.charAt(j);
return res;
}
private String w4(String s1, String s2) {
if (s1.length() % 2 == 0)
return s1;
return s2;
}
private String w5(String s1, String s2) {
String res = "";
if (s1.length() > s2.length()) {
String tmp = s1;
s1 = s2;
s2 = tmp;
}
for (int i = 0; i < s1.length(); i++)
res = res + (char) ('a' + ((s1.charAt(i) + s2.charAt(i) - 'a' - 'a') % 26));
for (int i = s1.length(); i < s2.length(); i++)
res = res + s2.charAt(i);
return res;
}
private String green(int x, int y) {
if (x == 0 && y == 0)
return first;
else if (y == 0)
switch (num[x][y]) {
case 1:
return b1(green(x - 1, 0));
case 2:
return b2(green(x - 1, 0));
case 3:
return b3(green(x - 1, 0));
case 4:
return b4(green(x - 1, 0));
case 5:
return b5(green(x - 1, 0));
}
else if (x == 0)
switch (num[x][y]) {
case 1:
return b1(green(0, y - 1));
case 2:
return b2(green(0, y - 1));
case 3:
return b3(green(0, y - 1));
case 4:
return b4(green(0, y - 1));
case 5:
return b5(green(0, y - 1));
}
return "";
}
private String[] cyan(int x, int y) {
String[] res = new String[2];
if (x == 1 && y == 1) {
res[0] = b1(green(1, 0));
res[1] = b1(green(0, 1));
}
else if (x == 1)
switch (num[x][y]) {
case 1:
res[0] = b1(cyan(x, y - 1)[0]);
res[1] = b1(green(x - 1, y));
break;
case 2:
res[0] = b2(cyan(x, y - 1)[0]);
res[1] = b2(green(x - 1, y));
break;
case 3:
res[0] = b3(cyan(x, y - 1)[0]);
res[1] = b3(green(x - 1, y));
break;
case 4:
res[0] = b4(cyan(x, y - 1)[0]);
res[1] = b4(green(x - 1, y));
break;
case 5:
res[0] = b5(cyan(x, y - 1)[0]);
res[1] = b5(green(x - 1, y));
break;
}
else if (y == 1)
switch (num[x][y]) {
case 1:
res[0] = b1(green(x, y - 1));
res[1] = b1(cyan(x - 1, y)[1]);
break;
case 2:
res[0] = b2(green(x, y - 1));
res[1] = b2(cyan(x - 1, y)[1]);
break;
case 3:
res[0] = b3(green(x, y - 1));
res[1] = b3(cyan(x - 1, y)[1]);
break;
case 4:
res[0] = b4(green(x, y - 1));
res[1] = b4(cyan(x - 1, y)[1]);
break;
case 5:
res[0] = b5(green(x, y - 1));
res[1] = b5(cyan(x - 1, y)[1]);
break;
}
else
switch (num[x][y]) {
case 1:
res[0] = b1(cyan(x, y - 1)[0]);
res[1] = b1(cyan(x - 1, y)[1]);
break;
case 2:
res[0] = b2(cyan(x, y - 1)[0]);
res[1] = b2(cyan(x - 1, y)[1]);
break;
case 3:
res[0] = b3(cyan(x, y - 1)[0]);
res[1] = b3(cyan(x - 1, y)[1]);
break;
case 4:
res[0] = b4(cyan(x, y - 1)[0]);
res[1] = b4(cyan(x - 1, y)[1]);
break;
case 5:
res[0] = b5(cyan(x, y - 1)[0]);
res[1] = b5(cyan(x - 1, y)[1]);
break;
}
return res;
}
private String pink(int x, int y) {
if (x == n - 1 && y == n - 1)
switch (num[x][y]) {
case 1:
return w1(pink(n - 2, n - 1), pink(n - 1, n - 2));
case 2:
return w2(pink(n - 2, n - 1), pink(n - 1, n - 2));
case 3:
return w3(pink(n - 2, n - 1), pink(n - 1, n - 2));
case 4:
return w4(pink(n - 2, n - 1), pink(n - 1, n - 2));
case 5:
return w5(pink(n - 2, n - 1), pink(n - 1, n - 2));
}
else if (x == 1 && y == n - 1)
switch (num[x][y]) {
case 1:
return w1(green(0, n - 1), cyan(1, n - 2)[0]);
case 2:
return w2(green(0, n - 1), cyan(1, n - 2)[0]);
case 3:
return w3(green(0, n - 1), cyan(1, n - 2)[0]);
case 4:
return w4(green(0, n - 1), cyan(1, n - 2)[0]);
case 5:
return w5(green(0, n - 1), cyan(1, n - 2)[0]);
}
else if (x == n - 1 && y == 1)
switch (num[x][y]) {
case 1:
return w1(cyan(n - 2, 1)[1], green(n - 1, 0));
case 2:
return w2(cyan(n - 2, 1)[1], green(n - 1, 0));
case 3:
return w3(cyan(n - 2, 1)[1], green(n - 1, 0));
case 4:
return w4(cyan(n - 2, 1)[1], green(n - 1, 0));
case 5:
return w5(cyan(n - 2, 1)[1], green(n - 1, 0));
}
else if (x == n - 1)
switch (num[x][y]) {
case 1:
return w1(cyan(x - 1, y)[1], pink(x, y - 1));
case 2:
return w2(cyan(x - 1, y)[1], pink(x, y - 1));
case 3:
return w3(cyan(x - 1, y)[1], pink(x, y - 1));
case 4:
return w4(cyan(x - 1, y)[1], pink(x, y - 1));
case 5:
return w5(cyan(x - 1, y)[1], pink(x, y - 1));
}
else if (y == n - 1)
switch (num[x][y]) {
case 1:
return w1(pink(x - 1, y), cyan(x, y - 1)[0]);
case 2:
return w2(pink(x - 1, y), cyan(x, y - 1)[0]);
case 3:
return w3(pink(x - 1, y), cyan(x, y - 1)[0]);
case 4:
return w4(pink(x - 1, y), cyan(x, y - 1)[0]);
case 5:
return w5(pink(x - 1, y), cyan(x, y - 1)[0]);
}
return "";
}
public String output() {
return pink(n - 1, n - 1);
}
}
public class ap {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sz = input.nextInt();
String st = input.next();
Machine mch = new Machine(sz, st);
System.out.print(mch.output());
}
}