제출 #1324451

#제출 시각아이디문제언어결과실행 시간메모리
1324451sh_qaxxorov_571Palindromic Partitions (CEOI17_palindromic)C++20
100 / 100
18 ms3248 KiB
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;

/**
 * CEOI 2017 - Palindromic Partitions
 * Rolling Hash yordamida O(N) yechim
 */

const ull BASE = 31; // Hash asosi

void solve() {
    string s;
    cin >> s;
    int n = s.length();
    
    int ans = 0;
    int left = 0, right = n - 1;
    
    ull left_hash = 0, right_hash = 0;
    ull p_pow = 1;
    int chunk_len = 0;

    while (left < right) {
        chunk_len++;
        // Chapdan hashni hisoblash: s[left] ni oxiriga qo'shish
        left_hash = left_hash * BASE + (s[left] - 'a' + 1);
        
        // O'ngdan hashni hisoblash: s[right] ni boshiga qo'shish
        right_hash = (s[right] - 'a' + 1) * p_pow + right_hash;
        p_pow *= BASE;

        if (left_hash == right_hash) {
            // Agar hashlar mos kelsa, satrlarni tekshiramiz (hash to'qnashuvi ehtimoli uchun)
            // Real musobaqada vaqtni tejash uchun faqat hash tekshirilishi ham mumkin
            ans += 2;
            left_hash = 0;
            right_hash = 0;
            p_pow = 1;
            chunk_len = 0;
        }
        left++;
        right--;
    }

    // Agar o'rtada belgi qolgan bo'lsa yoki oxirgi bo'lak ajratilmagan bo'lsa
    if (left == right || chunk_len > 0) {
        ans++;
    }

    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...