結果

問題 No.238 Mr. K's Another Gift
ユーザー ぴろずぴろず
提出日時 2015-07-05 22:54:52
言語 Java19
(openjdk 21)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 2,679 ms
コンパイル使用メモリ 79,992 KB
実行使用メモリ 59,448 KB
最終ジャッジ日時 2023-09-22 06:57:18
合計ジャッジ時間 13,861 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,564 KB
testcase_01 AC 124 ms
55,592 KB
testcase_02 AC 124 ms
55,384 KB
testcase_03 AC 128 ms
55,384 KB
testcase_04 AC 158 ms
55,480 KB
testcase_05 AC 247 ms
59,448 KB
testcase_06 AC 241 ms
58,624 KB
testcase_07 AC 268 ms
58,836 KB
testcase_08 AC 261 ms
59,372 KB
testcase_09 AC 260 ms
59,092 KB
testcase_10 AC 123 ms
55,672 KB
testcase_11 AC 126 ms
55,376 KB
testcase_12 AC 124 ms
55,864 KB
testcase_13 AC 138 ms
55,604 KB
testcase_14 AC 199 ms
58,656 KB
testcase_15 AC 232 ms
58,484 KB
testcase_16 AC 235 ms
58,820 KB
testcase_17 AC 216 ms
58,536 KB
testcase_18 AC 225 ms
58,640 KB
testcase_19 AC 213 ms
58,740 KB
testcase_20 AC 125 ms
55,344 KB
testcase_21 AC 122 ms
55,600 KB
testcase_22 AC 124 ms
55,508 KB
testcase_23 AC 129 ms
55,636 KB
testcase_24 AC 153 ms
55,644 KB
testcase_25 AC 170 ms
55,516 KB
testcase_26 AC 246 ms
58,876 KB
testcase_27 AC 265 ms
59,180 KB
testcase_28 AC 249 ms
59,020 KB
testcase_29 AC 257 ms
59,200 KB
testcase_30 AC 126 ms
55,680 KB
testcase_31 AC 124 ms
55,464 KB
testcase_32 AC 128 ms
55,584 KB
testcase_33 AC 146 ms
55,600 KB
testcase_34 AC 224 ms
58,540 KB
testcase_35 AC 219 ms
58,740 KB
testcase_36 AC 231 ms
59,172 KB
testcase_37 AC 227 ms
58,484 KB
testcase_38 AC 225 ms
58,288 KB
testcase_39 AC 221 ms
58,580 KB
testcase_40 AC 124 ms
55,460 KB
testcase_41 AC 123 ms
55,460 KB
testcase_42 AC 124 ms
55,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no238;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		ArrayList<Character> ans = solve(s);
		if (ans == null) {
			System.out.println("NA");
		}else{
			StringBuilder sb = new StringBuilder();
			for(char c:ans) {
				sb.append(c);
			}
			System.out.println(sb.toString());
		}
	}

	public static ArrayList<Character> solve(String s) {
		int n = s.length();
		ArrayList<Character> a = new ArrayList<>();
		for(int i=0;i<n;i++) {
			a.add(s.charAt(i));
		}
		if (isKaibun(a)) {
			a.add(n/2, a.get(n/2));
			return a;
		}
		ArrayList<Character> b = new ArrayList<>(a);
		Collections.reverse(b);
		for(int i=0;i<n;i++) {
			char c1 = a.get(i);
			char c2 = b.get(i);
			if (c1 != c2) {
				a.add(i,c2);
				if (isKaibun(a)) {
					return a;
				}
				b.add(i,c1);
				if (isKaibun(b)) {
					return b;
				}
				return null;
			}
		}
		return null; //unreachable
	}

	public static boolean isKaibun(ArrayList<Character> a) {
		int n = a.size();
		for(int i=0;i<n/2;i++) {
			if (a.get(i) != a.get(n-1-i)) {
				return false;
			}
		}
		return true;
	}

}
0