結果

問題 No.465 PPPPPPPPPPPPPPPPAPPPPPPPP
ユーザー pekempeypekempey
提出日時 2016-12-16 08:21:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,254 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 176,016 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-05-07 16:02:08
合計ジャッジ時間 5,116 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,884 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
32_ratsliveonnoevilstar.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

bool isPalindrome(const string &s, int l, int r) {
    for (int i = 0; i < r - l; i++) {
        if (s[l + i] != s[r - 1 - i]) {
            return false;
        }
    }
    return true;
}

bool isPalTwo(const string &s, int l, int r) {
    if (r - l <= 1) {
        return false;
    }
    for (int i = 1; i < r - l; i++) {
        if (isPalindrome(s, l, l + i) && isPalindrome(s, l + i, r)) {
            return true;
        }
    }
    return false;
}

vector<int> zAlgorithm(string s) {
    vector<int> z(s.size());
    z[0] = s.size();
    int i = 1;
    int j = 0;
    while (i < s.size()) {
        while (i + j < s.size() && s[i + j] == s[j]) {
            j++;
        }
        z[i++] = j;
        if (j == 0) {
            continue;
        }
        j--;
        int k = 1;
        while (i < s.size() && z[k] < j) {
            z[i++] = z[k++];
            j--;
        }
    }
    return z;
}

int main() {
    string s;
    cin >> s;
    string t = s;
    reverse(t.begin(), t.end());
    int n = s.size();

    vector<bool> bad(n);
    vector<int64_t> cnt(n + 2);
    vector<int> z = zAlgorithm(s);
    vector<int> zF = zAlgorithm(s + "$" + t);
    vector<int> zB = zAlgorithm(t + "$" + s);

    for (int i = 1; i < n; i++) {
        if (bad[i]) {
            continue;
        }
        if (zF[n * 2 + 1 - i] >= i) {
            for (int j = 2; i * j < n; j++) {
                if (z[i * (j - 1)] < i) {
                    break;
                }
                cnt[i * j + 1] += j - 1;
                bad[i * j] = true;
            }
        }
    }
    for (int i = 2; i <= n - 2; i++) {
        if (bad[i]) {
            continue;
        }
        // TODO: more faster
        if (isPalTwo(s, 0, i)) {
            for (int j = 1; i * j < n; j++) {
                if (z[i * (j - 1)] < i) {
                    break;
                }
                cnt[i * j + 1] += j;
                bad[i * j] = true;
            }
        }
    }

    for (int i = 0; i < n - 1; i++) {
        cnt[i + 1] += cnt[i];
    }

    int64_t ans = 0;
    for (int i = 1; i < n; i++) {
        if (zB[n * 2 + 1 - i] >= i) {
            ans += cnt[n - i];
        }
    }
    cout << ans << endl;
}
0