forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.java
59 lines (53 loc) · 1.73 KB
/
A.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
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
/**
* KickStart 2017 Round C
* Problem A. Ambiguous Cipher
*/
public class Main {
public String solve(Scanner scanner) {
String s=scanner.next();
if (s.length()%2!=0) return "AMBIGUOUS";
char[] chars=new char[s.length()];
for (int i=1;i<s.length();i+=2) {
if (i==1) chars[i]=s.charAt(i-1);
else {
// chars[i-2]+chars[i]=s[i-1]
// chars[i]=s[i]-chars[i-2]
chars[i]=i2c((c2i(s.charAt(i-1))-c2i(chars[i-2])+26)%26);
}
}
for (int i=s.length()-2;i>=0;i-=2) {
if (i==s.length()-2) chars[i]=s.charAt(i+1);
else {
chars[i]=i2c((c2i(s.charAt(i+1))-c2i(chars[i+2])+26)%26);
}
}
return new String(chars);
}
private char i2c(int i) {
return (char)('A'+i);
}
private int c2i(char c) {
return c-'A';
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=Integer.parseInt(scanner.nextLine());
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
try {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
catch (Throwable e) {
System.err.println("ERROR in case #"+t);
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}