結果

問題 No.238 Mr. K's Another Gift
ユーザー htensai
提出日時 2020-05-12 16:43:09
言語 Java
(openjdk 23)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,490 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 77,856 KB
実行使用メモリ 42,580 KB
最終ジャッジ日時 2024-09-13 14:54:12
合計ジャッジ時間 11,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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);
		StringBuilder sb = new StringBuilder(sc.next());
		int length = sb.length();
		boolean result;
		if (length % 2 == 0) {
		    result = (getResult(sb, length / 2 - 1, length / 2 - 1, true) || getResult(sb, length / 2, length / 2, false));
		} else {
		    result = (getResult(sb, length / 2 - 1, length / 2, true) || getResult(sb, length / 2, length / 2 + 1, false));
		}
		if (result) {
		    System.out.println(sb);
		} else {
		    System.out.println("NA");
		}
	}
	
	static boolean getResult(StringBuilder sb, int left, int right, boolean isLeft) {
	    int idx = -1;
	    char ch = (char)0;
	    while (left >= 0 && right < sb.length()) {
	        if (sb.charAt(left) != sb.charAt(right)) {
	            if (idx >= 0) {
	                return false;
	            }
	            if (isLeft) {
	                idx = left + 1;
	                ch = sb.charAt(right);
	                right++;
	            } else {
	                idx = right;
	                ch = sb.charAt(left);
	                left--;
	            }
	        } else {
	            left--;
	            right++;
	        }
	    }
	    if (idx < 0) {
	        if (isLeft) {
	            idx = 0;
	            ch = sb.charAt(sb.length() - 1);
	        } else {
	            idx = sb.length();
	            ch = sb.charAt(0);
	        }
	    }
	    sb.insert(idx, ch);
	    return true;
	}
}
0