結果

問題 No.273 回文分解
ユーザー uafr_cs
提出日時 2015-09-02 21:14:46
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,115 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 77,924 KB
実行使用メモリ 89,588 KB
最終ジャッジ日時 2024-12-23 11:55:52
合計ジャッジ時間 16,723 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

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){
		if(!first && is_n_palindrome(str, str.length())){
			return 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;
				}
				
				final int dfs = dfs(str.substring(n), false);
				if(dfs == 0){ continue; }
				
				
				ret =  Math.max(ret, Math.max(n, dfs));
			}
		}
		
		return Math.max(ret, !first && is_n_palindrome(str, str.length()) ? str.length() : 0);
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		System.out.println(dfs(sc.next(), true));
		
	}
	
}
0