Skip to content

Commit c416fe6

Browse files
authored
[20250320] BOJ / G2 / 안티 팰린드롬 / 권혁준
1 parent 30a80fa commit c416fe6

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
if(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int[] cnt;
26+
static int N;
27+
static String ans;
28+
29+
public static void main(String[] args) throws Exception {
30+
31+
ready();
32+
solve();
33+
34+
bwEnd();
35+
36+
}
37+
38+
static void ready() throws Exception{
39+
40+
String s = br.readLine();
41+
N = s.length();
42+
cnt = new int[26];
43+
for(char c : s.toCharArray()) cnt[c-'a']++;
44+
45+
}
46+
47+
static void solve() throws Exception{
48+
49+
for(int i=0;i<26;i++) if(cnt[i] > (N+1)/2) {
50+
bw.write("-1");
51+
return;
52+
}
53+
54+
ans = new String();
55+
for(int i=0;i<26;i++) if(cnt[i] > 0) {
56+
if(ans.length() > (N+1)/2 || ans.length() + cnt[i] <= (N+1)/2) {
57+
while(cnt[i] > 0) {
58+
ans += (char)('a'+i);
59+
cnt[i]--;
60+
}
61+
}
62+
else if(N%2 == 1 && ans.length() == N/2) {
63+
while(cnt[i] > 0) {
64+
ans += (char)('a'+i);
65+
cnt[i]--;
66+
}
67+
}
68+
else {
69+
int need = 0;
70+
while(ans.length() < (N+1)/2) {
71+
ans += (char)('a'+i);
72+
need++;
73+
}
74+
cnt[i] -= need;
75+
if(N%2 == 1) need--;
76+
77+
for(int j=i+1;j<26;j++) while(cnt[j] > 0 && need > 0) {
78+
cnt[j]--;
79+
ans += (char)('a'+j);
80+
need--;
81+
}
82+
while(cnt[i] > 0) {
83+
ans += (char)('a'+i);
84+
cnt[i]--;
85+
}
86+
87+
}
88+
}
89+
bw.write(ans);
90+
91+
}
92+
93+
}
94+
95+
```

0 commit comments

Comments
 (0)