結果

問題 No.599 回文かい
ユーザー roarisroaris
提出日時 2020-12-04 19:37:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 4,000 ms
コード長 1,070 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 201,080 KB
実行使用メモリ 101,900 KB
最終ジャッジ日時 2023-10-13 09:19:05
合計ジャッジ時間 3,862 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,476 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,552 KB
testcase_14 AC 145 ms
82,624 KB
testcase_15 AC 9 ms
7,640 KB
testcase_16 AC 248 ms
89,692 KB
testcase_17 AC 284 ms
101,900 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 1 ms
4,348 KB
evil_0.txt AC 53 ms
36,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;
const int MOD = 1000000007;

int n;
string s;
int memo[10000];

vector<int> z_algo(string s) {
	int c=0, n=s.size();
	vector<int> z(n, 0);
	for (int i=1; i<n; i++) {
		int l = i-c;
		if (i+z[l]<c+z[c]) z[i] = z[l];
		else {
			int j = max(0, c+z[c]-i);
			while (i+j<n && s[j]==s[i+j]) j++;
			z[i] = j;
			c = i;
		}
	}
    z[0] = n;
	return z;
}

ll f(int l) {
    if (n-2*l==0) return 1;
    if (memo[l]!=-1) return memo[l];
    
    vector<int> z = z_algo(s.substr(l, n-2*l));
    ll res = 1;
    
    for (int i=n-2*l-1; i>=0; i--) {
        int com = n-2*l-i;
        if (z[i]==com && 2*com<=n-2*l) res = (res+f(l+com))%MOD;
    }
    
    return memo[l] = res;
}

int main() {
    //ループ変数が被っていないか?
    //制約を確認しているか?
    //変数のtypoがないか?
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> s;
    n = s.size();
    fill(memo, memo+10000, -1);
    cout << f(0) << endl;
}
0