QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#283007#6567. Repetitive String Inventionwarner1129#TL 1ms3548kbC++203.4kb2023-12-13 17:41:312023-12-13 17:41:32

Judging History

你现在查看的是最新测评结果

  • [2023-12-13 17:41:32]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3548kb
  • [2023-12-13 17:41:31]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
template <class... T> void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); }
template <class T> void org(T l, T r) { while (l != r) cerr << ' ' << *l++; cerr << '\n'; }
#define debug(x...) dbg(#x, '=', x, '\n')
#define olist(x...) dbg(#x, '='), org(x)
#else
#define debug(...) ((void)0)
#define olist(...) ((void)0)
#endif
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using Pt = pair<int, int>;

template <class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
constexpr int mod = 998244353;

template<class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template<class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }
template<class... T> int add(T... x) { int t{}; return (((t += x) %= mod), ...), t; }
template<class... T> int mul(T... x) { i64 t{1}; return (((t *= x) %= mod), ...), t; }

constexpr u64 P = 1e9 + 321;

void solve() {
    string S;
    cin >> S;
    
    const int n = S.size();
    vector<u64> pw(n + 1, 1);
    for (int i = 1; i <= n; i++) {
        pw[i] = pw[i - 1] * P;
    }

    auto build = [&](string_view) -> vector<u64> {
        vector<u64> h(n);
        for (int i = 0; i < n; i++) {
            h[i] = S[i] * pw[i];
            if (i) h[i] += h[i - 1];
        }
        return h;
    };

    auto get = [&](const vector<u64> &h, int l, int r) -> u64 {
        assert(l <= r);
        return (h[r] - (l == 0 ? 0 : h[l - 1])) * pw[n - l];
    };

    auto cal = [&](string_view S, bool same) -> i64 {
        auto h = build(S);
        i64 ans = 0;
        
        for (int i = n - 1; i >= 1; i--) { // [0, i - 1] [i, n - 1]
            map<u64, i64> cnt;
            for (int j = i; j < n; j++) {
                cnt[get(h, i, j)]++;
            }

            for (int l = 0; l < i; l++) {
                string tmp;
                vector<int> fail;
                int p = -1;
                for (int r = l; r < i; r++) {
                    tmp += S[r];
                    while (p != -1 and tmp[p + 1] != tmp.back()) {
                        p = fail[p];
                    }
                    if (tmp[p + 1] == tmp.back()) p++;
                    if (r == l) p = -1;
                    fail.push_back(p);

                    debug(l, r, tmp);
                    olist(all(fail));
                    for (int g = p; g != -1; g = fail[g]) {
                        int len = g + 1;
                        if (l + g + 1 <= r - g - 1) {
                            debug(l + g + 1, r - g - 1, cnt[get(h, l + g + 1, r - g - 1)]);
                            ans += cnt[get(h, l + g + 1, r - g - 1)];
                        }
                    }

                    if (same) {
                        debug(same, cnt[get(h, l, r)]);
                        ans += cnt[get(h, l, r)];
                    }
                }
            }
        }
        
        return ans;
    };

    i64 ans = cal(S, 1);
    reverse(all(S));
    ans += cal(S, 0);

    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
 


详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3548kb

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3392kb

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: -100
Time Limit Exceeded

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:


result: