-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ03028_MaHoaDRM.java
66 lines (55 loc) · 1.55 KB
/
J03028_MaHoaDRM.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
import java.io.*;
import java.math.*;
import java.util.*;
public class J03028_MaHoaDRM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
System.out.println(ptit(s));
}
}
public static String ptit(String s) {
int n = s.length();
String a = s.substring(0, n / 2);
String b = s.substring(n / 2, n);
a = rotateString(a, rotate(a));
b = rotateString(b, rotate(b));
return Merge(a, b);
}
public static String Merge(String a, String b) {
char[] c = a.toCharArray();
char[] d = b.toCharArray();
int n = a.length();
String ans = "";
for (int i = 0; i < n; i++) {
int k = c[i] - 65;
int r = d[i] - 65;
k += r;
k %= 26;
ans += (char) (k + 65);
}
return ans;
}
public static String rotateString(String a, int r) {
char[] c = a.toCharArray();
String ans = "";
for (Character i : c) {
int x = i - 65;
x += r;
x = x % 26;
char k = (char) (x + 65);
ans += k;
}
return ans;
}
public static int rotate(String a) {
char[] c = a.toCharArray();
int sum = 0;
for (Character i : c) {
sum += i - 65;
}
return sum;
}
}