-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
92 lines (82 loc) · 2.38 KB
/
Copy pathSolution.java
File metadata and controls
92 lines (82 loc) · 2.38 KB
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface Encryption {
public void encrypt();
public void decrypt();
}
class FooEncryption implements Encryption {
String str;
FooEncryption(String str) {
this.str = str;
}
public void encrypt() {
String res = "";
for(int i = 0; i < this.str.length(); ++i) {
char c = (char) (((int) this.str.charAt(i)) - (i % 2) + 3);
res = res + c;
}
this.str = res;
}
public void decrypt() {
String res = "";
for(int i = 0; i < this.str.length(); ++i) {
char c = (char) (((int) this.str.charAt(i)) + (i % 2) - 3);
res = res + c;
}
this.str = res;
}
}
class BarEncryption implements Encryption {
String str;
BarEncryption(String str) {
this.str = str;
}
public void encrypt() {
String res = String.valueOf(this.str.charAt(0));
for(int i = 1; i < this.str.length(); ++i) {
char c = (char) (((int) this.str.charAt(i)) - ((int) this.str.charAt(i - 1)) + 80);
res = res + c;
}
this.str = res;
}
public void decrypt() {
String res = String.valueOf(this.str.charAt(0));
for(int i = 1; i < this.str.length(); ++i) {
char c = (char) (((int) this.str.charAt(i)) + ((int) this.str.charAt(i - 1)) - 80);
res = res + c;
}
this.str = res;
}
}
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
String fb = input.split(" ")[0], ed = input.split(" ")[1], str = input.split(" ")[2];
if(fb.equals("foo")) {
FooEncryption x = new FooEncryption(str);
if(ed.equals("en")) {
x.encrypt();
System.out.println(x.str);
}
else {
x.decrypt();
System.out.println(x.str);
}
}
else {
BarEncryption x = new BarEncryption(str);
if(ed.equals("en")) {
x.encrypt();
System.out.println(x.str);
}
else {
x.decrypt();
System.out.println(x.str);
}
}
}
}