結果

問題 No.273 回文分解
ユーザー uafr_cs
提出日時 2015-09-02 21:20:14
言語 Java
(openjdk 23)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,353 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 78,020 KB
実行使用メモリ 41,756 KB
最終ジャッジ日時 2024-06-25 13:28:31
合計ジャッジ時間 7,872 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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