結果

問題 No.273 回文分解
ユーザー vain0vain0
提出日時 2015-08-29 00:06:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 100,864 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 19:39:51
合計ジャッジ時間 2,192 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <functional>
#include <set>
#include <ctime>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#ifndef ONLINE_JUDGE
# include <complex>
# include <random>
# include <array>
# include <unordered_map>
# define mkt make_tuple
#endif
#ifdef _LOCAL
# include "for_local.h"
#else
# define ifdebug if (false)
# define echo ((void)0)
#endif
using namespace std;
typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull;
#define repi(_I, _B, _E) for(int _I = (_B); (_I) < (_E); ++ (_I))
#define rep(_I, _N) for(int _I = 0; (_I) < (_N); ++ (_I))
#define all(_X) (_X).begin(), (_X).end()
#define mkp make_pair
inline int scani() { int n; scanf(" %d", &n); return n; }

string s;
int sl;

static void comb(int& ma, int r)
{
	if ( !(r < 0 || r == sl) ) {
		ma = max(ma, r);
	}
}

map<pair<int,int>,int> memo;

//i: 次に見るsの位置
//j: 今考えているprefixの先頭
int dfs(int i, int j)
{
	if ( i >= sl ) return i == j ? 0 : -1;
	
	auto&& it = memo.find(mkp(i, j));
	if ( it != memo.end() ) {
		return it->second;
	}
	
	int ma = -1;
	int l = i - j; //prefixの長さ
	if ( (sl - (i + 1)) >= l - 1 ) {
		comb(ma, dfs(i + 1, j));
	}

	if ( l > 0 ) {
		string const prefix = s.substr(j, i - j);

		if ( s[i - 1] == s[i] ) {
			string postfix = s.substr(i, l);
			reverse(all(postfix));
			if ( prefix == postfix ) {
				comb(ma, l * 2);
				comb(ma, dfs(i + l, i + l));
			}
		}
		if ( l == 1 || l >= 2 && s[i - 2] == s[i] ) {
			string postfix = s.substr(i - 1, l);
			reverse(all(postfix));
			if ( prefix == postfix ) {
				comb(ma, l * 2 - 1);
				comb(ma, dfs(i + l - 1, i + l - 1));
			}
		}
	}
	return memo.emplace_hint(it, mkp(i, j), ma)->second;
}

signed main()
{
	cin >> s;
	sl = s.size();
	int l = dfs(0, 0);
	cout << l << endl;
	return 0;
}
0