QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#596319#5065. Beautiful String333zhanTL 1ms3648kbC++203.4kb2024-09-28 15:31:402024-09-28 15:31:42

Judging History

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

  • [2024-09-28 15:31:42]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3648kb
  • [2024-09-28 15:31:40]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

constexpr int P = 1145141;
constexpr int mod2 = 1E9 + 33;
constexpr int mod = 998244353;
constexpr int N = 5010;
constexpr int Z = 1E9 + 50;

int p[N];
int p2[N];

void solve () {
    string s;
    cin >> s;

    const int n = s.size ();

    s = " " + s;

    vector <int> Hash (n + 1);
    for (int i = 1; i <= n; i ++) {
        Hash[i] = (Hash[i - 1] * P + s[i]) % mod;
    }
    vector <int> Hash2 (n + 1);
    for (int i = 1; i <= n; i ++) {
        Hash2[i] = (Hash2[i - 1] * P + s[i]) % mod2;
    }

    auto get = [&] (int l, int r) {
        int len = r - l + 1;
        int x = (Hash[r] - Hash[l - 1] * p[len] % mod + mod) % mod;
        int y = (Hash2[r] - Hash2[l - 1] * p2[len] % mod2 + mod2) % mod2;
        return x * Z + y;
    };

    vector <vector <int>> a (n + 1);
    for (int i = 2; i <= n; i ++) {
        for (int j = 1; j < i; j ++) {
            if (i - j > n - i + 1) {
                break;
            }
            int len = i - j;
            if (get (j, i - 1) == get (i, i + len - 1)) {
                a[i].push_back (j);
            }
        }
    }

    unordered_map <int, vector <pair <int, int>>> mp;
    for (int l = 1; l <= n; l ++) {
        for (int r = l; r <= n; r ++) {
            mp[get (l, r)].push_back ({l, r});
        }
    }

    vector <vector <vector <pair <int, int>>>> mp2 (n + 1);
    for (auto [_, v] : mp) {
        mp2[v.back ().second - v.back ().first + 1].push_back (v);
    }

    vector <int> d (n + 1);
    for (int i = 1; i <= n; i ++) {
        d[i] = max (0LL, (int)(a[i].size ()) - 1) ;
    }

    int ans = 0;
    for (int t = 2; t <= n; t ++) {
        for (auto &v : mp2[t]) {
            const int m = v.size ();
            for (int i = 0, y = 1; i < m - 1; i ++) {
                auto [l, r] = v[i];
                if (r - l + 1 == 1) {
                    break;
                }
                int len = r - l + 1;
                // int x = lower_bound (a[l].begin (), a[l].end (), l - len + 1) - a[l].begin ();
                while (d[l] - 1 >= 0 && a[l][d[l] - 1] >= l - len + 1) {
                    d[l] --;
                }           
                int x = d[l];     
                // int y = lower_bound (v.begin () + i, v.end (), pair <int, int> {r + 2, 0}) - v.begin ();
                while (y < m && v[y].first < r + 2) {
                    y ++;
                }
                int sum2 = m - y;
                if (v[i + 1].first == r + 1) {
                    sum2 --;
                }
                ans += sum2 * ((int)(a[l].size ()) - x);
                // if (sum2 * ((int)(a[l].size ()) - x) != 0) {
                //     cerr << l << " " << r << " " << sum2 << " " << ((int)(a[l].size ()) - x) << '\n';
                // }
                // if (l == 1 && r == 2) {
                //     cerr << a[l].size () << " " << x << '\n';     
                // }
            }
        }
    }

    cout << ans << '\n';
}
 
signed main () {
	ios::sync_with_stdio (false);
    cin.tie (nullptr);

    p[0] = 1;
    for (int i = 1; i < N; i ++) {
        p[i] = p[i - 1] * P % mod;
    }
    p2[0] = 1;
    for (int i = 1; i < N; i ++) {
        p2[i] = p2[i - 1] * P % mod2;
    }
    
    int T = 1;
    cin >> T;

    while (T --) {
        solve ();
    }

 	return 0;
}

详细

Test #1:

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

input:

2
114514
0000000

output:

1
3

result:

ok 2 number(s): "1 3"

Test #2:

score: -100
Time Limit Exceeded

input:

11
79380
2483905227
37902399703986576270
39991723655814713848046032694137883815354365954917
5541883522667841718787744915558298207485830622522715211018760095628594390563630840464643840093757297
56530485554219245862513973750218715585679416120445975390556326891488719311495909340757506478400624741858999...

output:


result: