답안 #861953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861953 2023-10-17T09:02:28 Z anhphant Palindromic Partitions (CEOI17_palindromic) C++14
0 / 100
6 ms 9308 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;

        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++;
    }

    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:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen("FILE.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen("FILE.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Incorrect 6 ms 9308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Incorrect 6 ms 9308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Incorrect 6 ms 9308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9304 KB Output is correct
2 Correct 6 ms 9308 KB Output is correct
3 Incorrect 6 ms 9308 KB Output isn't correct
4 Halted 0 ms 0 KB -