結果

問題 No.273 回文分解
ユーザー vain0
提出日時 2015-08-29 00:06:15
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 895 ms
コンパイル使用メモリ 88,228 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-25 13:25:41
合計ジャッジ時間 1,869 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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