From c416fe659ee8f54bf5ebd55ee5dc711af5b07ff0 Mon Sep 17 00:00:00 2001 From: oncsr Date: Thu, 20 Mar 2025 10:04:31 +0900 Subject: [PATCH] =?UTF-8?q?[20250320]=20BOJ=20/=20G2=20/=20=EC=95=88?= =?UTF-8?q?=ED=8B=B0=20=ED=8C=B0=EB=A6=B0=EB=93=9C=EB=A1=AC=20/=20?= =?UTF-8?q?=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\353\246\260\353\223\234\353\241\254.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "khj20006/202503/20 BOJ G2 \354\225\210\355\213\260 \355\214\260\353\246\260\353\223\234\353\241\254.md" diff --git "a/khj20006/202503/20 BOJ G2 \354\225\210\355\213\260 \355\214\260\353\246\260\353\223\234\353\241\254.md" "b/khj20006/202503/20 BOJ G2 \354\225\210\355\213\260 \355\214\260\353\246\260\353\223\234\353\241\254.md" new file mode 100644 index 00000000..5f78dae1 --- /dev/null +++ "b/khj20006/202503/20 BOJ G2 \354\225\210\355\213\260 \355\214\260\353\246\260\353\223\234\353\241\254.md" @@ -0,0 +1,95 @@ +```java + +import java.util.*; +import java.io.*; + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st = new StringTokenizer(""); + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static String nextToken() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + + static int[] cnt; + static int N; + static String ans; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + String s = br.readLine(); + N = s.length(); + cnt = new int[26]; + for(char c : s.toCharArray()) cnt[c-'a']++; + + } + + static void solve() throws Exception{ + + for(int i=0;i<26;i++) if(cnt[i] > (N+1)/2) { + bw.write("-1"); + return; + } + + ans = new String(); + for(int i=0;i<26;i++) if(cnt[i] > 0) { + if(ans.length() > (N+1)/2 || ans.length() + cnt[i] <= (N+1)/2) { + while(cnt[i] > 0) { + ans += (char)('a'+i); + cnt[i]--; + } + } + else if(N%2 == 1 && ans.length() == N/2) { + while(cnt[i] > 0) { + ans += (char)('a'+i); + cnt[i]--; + } + } + else { + int need = 0; + while(ans.length() < (N+1)/2) { + ans += (char)('a'+i); + need++; + } + cnt[i] -= need; + if(N%2 == 1) need--; + + for(int j=i+1;j<26;j++) while(cnt[j] > 0 && need > 0) { + cnt[j]--; + ans += (char)('a'+j); + need--; + } + while(cnt[i] > 0) { + ans += (char)('a'+i); + cnt[i]--; + } + + } + } + bw.write(ans); + + } + +} + +```