結果

問題 No.238 Mr. K's Another Gift
ユーザー tenten
提出日時 2021-01-20 18:06:18
言語 Java
(openjdk 23)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,173 bytes
コンパイル時間 2,682 ms
コンパイル使用メモリ 77,560 KB
実行使用メモリ 42,372 KB
最終ジャッジ日時 2024-12-23 08:01:44
合計ジャッジ時間 12,700 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int left = 0;
		int right = s.length() - 1;
		int idx = -1;
		char c = ' ';
		while (left <= right) {
		    if (s.charAt(left) == s.charAt(right)) {
		        left++;
		        right--;
		        continue;
		    }
		    if (s.charAt(left + 1) == s.charAt(right)) {
		        if (idx >= 0) {
		            System.out.println("NA");
		            return;
		        }
		        idx = right + 1;
		        c = s.charAt(left);
		        left++;
		    } else if (s.charAt(left) == s.charAt(right - 1)) {
		        if (idx >= 0) {
		            System.out.println("NA");
		            return;
		        }
		        idx = left;
		        c = s.charAt(right);
		        right--;
		    } else {
		        System.out.println("NA");
		        return;
		    }
		}
		if (idx < 0) {
		    if (s.length() % 2 == 0) {
		        idx = s.length() / 2;
		        c = 'a';
		    } else {
		        idx = s.length() / 2;
		        c = s.charAt(idx);
		    }
		}
		s = new StringBuilder(s).insert(idx, c).toString();
		System.out.println(s);
	}
}
0