-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem382A.java
More file actions
128 lines (116 loc) · 2.5 KB
/
Copy pathProblem382A.java
File metadata and controls
128 lines (116 loc) · 2.5 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
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
package CodeForces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Problem382A
{
public static void main(String[] args) throws IOException
{
fastinput.init(System.in);
String input = fastinput.next();
String input2 = fastinput.next();
int delim = 0;
for (int i = 0; i < input.length(); i++)
{
if (input.charAt(i) == '|')
{
delim = i;
break;
}
}
String left = input.substring(0, delim);
String right = input.substring(delim+1);
while (true)
{
if (left.length() > right.length())
{
right = right + input2;
if (right.length() == left.length())
{
System.out.println(left+"|"+right);
break;
}
else if (right.length() < left.length())
{
System.out.println("Impossible");
break;
}
else
{
int excess = right.length() - left.length();
input2 = right.substring(right.length()-excess);
right = right.substring(0, right.length() - excess);
continue;
}
}
if (left.length() < right.length())
{
left = left + input2;
if (left.length() == right.length())
{
System.out.println(left+"|"+right);
break;
}
else if (left.length() < right.length())
{
System.out.println("Impossible");
break;
}
else
{
int excess = left.length() - right.length();
input2 = left.substring(left.length()-excess);
left = left.substring(0, left.length()-excess);
continue;
}
}
else
{
if (input2.length() % 2 != 0)
{
System.out.println("Impossible");
break;
}
else
{
left = left + input2.substring(0, input2.length()/2);
right = right + input2.substring(input2.length()/2);
System.out.println(left+"|"+right);
break;
}
}
}
}
public static class fastinput
{
static BufferedReader bf;
static StringTokenizer st;
static void init(InputStream input)
{
bf = new BufferedReader(new InputStreamReader(input));
st = new StringTokenizer("");
}
static String nextline() throws IOException
{
return bf.readLine();
}
static String next() throws IOException
{
while (!st.hasMoreTokens())
{
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException
{
return Integer.parseInt(next());
}
static long nextLong() throws IOException
{
return Long.parseLong(next());
}
}
}