Submission #861958

# Submission time Handle Problem Language Result Execution time Memory
861958 2023-10-17T09:12:43 Z anhphant Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
118 ms 28756 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'

const ll mod = 1e9 + 9;
const ll base = 9973;
string S;
int N;

ll basepow[1000007], pre[1000007];

ll getHash(int i, int j) {
    ll r = pre[j];
    ll l = (pre[i - 1] * basepow[j - i + 1]) % mod;
    return (r - l + mod) % mod;
}

ll DP[1000007];

void solve() {
    cin >> S;
    int N = S.size();
    S = " " + S;

    for(int i = 1; i <= N; ++i) {
        pre[i] = (pre[i - 1] * base + S[i] - 'a' + 1) % mod;
    }

    ll L = 1, R = N;
    ll ans = 0;

    while(L <= R) {
        //cerr << L << " " << R << endl;

        if (L == R) {
            ans++;
            break;
        }

        for(int i = L; i <= N / 2; ++i) {
            //cerr << "i = " << i << endl;

            if (getHash(L, i) == getHash(N - i + 1, R)) {
                ans += 2;
                L = i + 1;
                R = N - i;
                goto GT;
            }
        }

        //cerr << "end : " << L << " " << R << endl;

        ans += (L < R);
        break;

        GT:;

        if (L == R) {
            ans++;
            break;
        }
    }

    cout << ans << endl;
}

int main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    if (fopen("FILE.INP", "r")) {
        freopen("FILE.INP", "r", stdin);
        freopen("FILE.OUT", "w", stdout);
    }

    basepow[0] = 1;
    for(int i = 1; i <= 1000000; ++i) basepow[i] = (basepow[i - 1] * base) % mod;

    int Tc;
    cin >> Tc;
    for(int _ = 1; _ <= Tc; ++_) {
        //cerr << " _ = " << _ << endl;
        solve();
    }
}

Compilation message

palindromic.cpp: In function 'int main()':
palindromic.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("FILE.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("FILE.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Correct 6 ms 9308 KB Output is correct
4 Correct 6 ms 9308 KB Output is correct
5 Correct 6 ms 9184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Correct 6 ms 9308 KB Output is correct
4 Correct 6 ms 9308 KB Output is correct
5 Correct 6 ms 9184 KB Output is correct
6 Correct 6 ms 9308 KB Output is correct
7 Correct 6 ms 9252 KB Output is correct
8 Correct 6 ms 9308 KB Output is correct
9 Correct 6 ms 9412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Correct 6 ms 9308 KB Output is correct
4 Correct 6 ms 9308 KB Output is correct
5 Correct 6 ms 9184 KB Output is correct
6 Correct 6 ms 9308 KB Output is correct
7 Correct 6 ms 9252 KB Output is correct
8 Correct 6 ms 9308 KB Output is correct
9 Correct 6 ms 9412 KB Output is correct
10 Correct 7 ms 9564 KB Output is correct
11 Correct 7 ms 9564 KB Output is correct
12 Correct 7 ms 9564 KB Output is correct
13 Correct 7 ms 9584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Correct 6 ms 9308 KB Output is correct
4 Correct 6 ms 9308 KB Output is correct
5 Correct 6 ms 9184 KB Output is correct
6 Correct 6 ms 9308 KB Output is correct
7 Correct 6 ms 9252 KB Output is correct
8 Correct 6 ms 9308 KB Output is correct
9 Correct 6 ms 9412 KB Output is correct
10 Correct 7 ms 9564 KB Output is correct
11 Correct 7 ms 9564 KB Output is correct
12 Correct 7 ms 9564 KB Output is correct
13 Correct 7 ms 9584 KB Output is correct
14 Correct 118 ms 27704 KB Output is correct
15 Correct 66 ms 22804 KB Output is correct
16 Correct 112 ms 28756 KB Output is correct
17 Correct 66 ms 20176 KB Output is correct