Submission #912517

# Submission time Handle Problem Language Result Execution time Memory
912517 2024-01-19T15:00:29 Z MisterReaper Palindromic Partitions (CEOI17_palindromic) C++17
100 / 100
230 ms 17048 KB
#include <bits/stdc++.h>
using i64 = long long;

/**
 * Description: Modular arithmetic operations basic
 * Source: MisterReaper
 * KACTL:
 * Not. 
 */

constexpr int MOD = 1E9 + 7;
constexpr int PRIME = 37;

int mul(int a, int b) {
    return (1LL * a * b) % MOD;
}

int add(int a, int b) {
    return (a + b) % MOD;
}

int sub(int a, int b) {
    return add(a, MOD - b);
}

int expo(int a, int b) {
    int res = 1;
    while(b) {
        if(b & 1) {
            res = mul(res, a);
        }

        a = mul(a, a);
        b >>= 1;
    }

    return res;
}

int inv(int a) {
    return expo(a, MOD - 2);
}

int divide(int a, int b) {
    return mul(a, inv(b));
}

#define ONLINE_JUDGE
void solve() {
    std::string s;
    std::cin >> s;

    int n = s.size();
    s = '$' + s;

    std::vector<int> hash(n + 1);
    int ans = 0, l = 1, r = n;
    while(l <= r) {
        if(l == r) {
            ans++;
            break;
        }

        //std::cerr << l << " " << r << "\n";
        int h1 = 0, h2 = 0, len = 0;
        while(l < r) {
            h1 = add(mul(h1, PRIME), s[l] - 'a' + 1);
            h2 = add(h2, mul(s[r] - 'a' + 1, expo(PRIME, len)));

            if(h1 == h2) {
                break;
            }

            l++; 
            r--;
            len++;
        }

        if(h1 == h2) {
            ans += 2;
        } else {
            ans++;
            break;
        }

        l++;
        r--;
    }

    std::cout << ans << "\n";
    
    return;
}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL); std::cout.tie(NULL);

    int t = 1; std::cin >> t;
    for(int i = 1; i <= t; i++) {
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 212 ms 17048 KB Output is correct
15 Correct 220 ms 10704 KB Output is correct
16 Correct 230 ms 14344 KB Output is correct
17 Correct 26 ms 8140 KB Output is correct