結果

問題 No.273 回文分解
ユーザー uafr_csuafr_cs
提出日時 2015-09-02 21:20:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,353 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 74,096 KB
実行使用メモリ 56,032 KB
最終ジャッジ日時 2023-09-07 19:43:00
合計ジャッジ時間 8,056 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,248 KB
testcase_01 AC 124 ms
55,648 KB
testcase_02 AC 125 ms
55,544 KB
testcase_03 AC 123 ms
55,788 KB
testcase_04 AC 124 ms
55,524 KB
testcase_05 AC 122 ms
55,668 KB
testcase_06 AC 122 ms
55,596 KB
testcase_07 AC 123 ms
55,480 KB
testcase_08 AC 123 ms
55,840 KB
testcase_09 AC 122 ms
55,824 KB
testcase_10 AC 121 ms
55,392 KB
testcase_11 AC 121 ms
55,456 KB
testcase_12 AC 122 ms
55,716 KB
testcase_13 AC 121 ms
55,576 KB
testcase_14 AC 125 ms
55,756 KB
testcase_15 AC 121 ms
55,592 KB
testcase_16 AC 122 ms
55,460 KB
testcase_17 AC 121 ms
55,840 KB
testcase_18 AC 122 ms
55,760 KB
testcase_19 AC 122 ms
55,780 KB
testcase_20 AC 127 ms
55,880 KB
testcase_21 AC 126 ms
55,404 KB
testcase_22 AC 122 ms
55,652 KB
testcase_23 AC 123 ms
55,288 KB
testcase_24 AC 124 ms
55,272 KB
testcase_25 AC 125 ms
55,448 KB
testcase_26 AC 128 ms
55,712 KB
testcase_27 AC 123 ms
53,904 KB
testcase_28 AC 124 ms
55,776 KB
testcase_29 AC 124 ms
56,032 KB
testcase_30 AC 124 ms
55,732 KB
testcase_31 AC 122 ms
53,752 KB
testcase_32 AC 121 ms
55,596 KB
testcase_33 AC 123 ms
55,632 KB
testcase_34 AC 122 ms
55,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static boolean is_n_palindrome(String str, final int n){
		if(str.length() < n){ return false; }
		
		for(int start = 0, end = n - 1; start < end; start++, end--){
			if(str.charAt(start) != str.charAt(end)){
				return false;
			}
		}
		
		return true;
	}
	
	public static int dfs(String str, boolean first, int start, int[] memo){
		if(memo[start] >= 0){
			return memo[start];
		}else if(!first && is_n_palindrome(str, str.length())){
			return memo[start] = str.length();
		}
		
		int ret = 0;
		
		for(int n = str.length() - 1; n >= 1; n--){
			if(is_n_palindrome(str, n)){
				final int rest = str.length() - n;
				
				if(rest <= ret){
					continue;
				}
				
				//System.out.println(str + " => " + str.substring(0, n) + " : " + str.substring(n));
				final int dfs = dfs(str.substring(n), false, start + n, memo);
				
				if(dfs == 0){ continue; }
				
				
				ret =  Math.max(ret, Math.max(n, dfs));
			}
		}
		
		return memo[start] = ret;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final String str = sc.next();
		int[] memo = new int[str.length()];
		Arrays.fill(memo, -1);
		System.out.println(dfs(str, true, 0, memo));
		
	}
	
}
0