Submission #197451

# Submission time Handle Problem Language Result Execution time Memory
197451 2020-01-21T10:02:50 Z quocnguyen1012 Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
274 ms 20716 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const int MOD = 1000000007;
const int p = 41;
 
int hsh[1000005];
int powers[1000005];
 
int add(int a, int b){
    return (a+b)%MOD;
}
 
int mul(int a, int b){
    return ((ll)a*b)%MOD;    
}
 
int substring_hash(int l, int r){
    if(l == 0) return hsh[r];
    return add(hsh[r], mul(powers[r-l+1], MOD - hsh[l-1]));
}
 
void solve(){
    string s;
    cin >> s;
    int n = s.size();
    if(n == 1){
        cout << 1 << "\n";
        return;
    }
    powers[0] = 1;
    for(int i=1; i<=n; i++) powers[i] = mul(p, powers[i-1]);
    hsh[0] = 1 + (s[0] - 'a');
    for(int i=1; i<n; i++){
        hsh[i] = add(mul(p, hsh[i-1]), (s[i]-'a') + 1);
    }
    int l1=0, r1=0, l2=n-1, r2=n-1;
    int res = 0;
    while(1){
        //cout << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
        //cout << substring_hash(l1, r1) << " " << substring_hash(l2, r2) << endl;
        if(substring_hash(l1, r1) == substring_hash(l2, r2)){
            res += 2;
            r2 = l2-1;
            l1 = r1+1;
            r1++;
            l2--;
            if(r1 > l2){
                cout << res << "\n";
                return;
            }
            if(r1 == l2){
                cout << res+1 << "\n";
                return;
            }
        }
        else{
            r1++;
            l2--;
            if(r1 >= l2){
                cout << res+1 << "\n";
                return;
            }
        }
    }
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cout.precision(10);
    cout << fixed;
    
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 4 ms 508 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 4 ms 508 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 274 ms 20716 KB Output is correct
15 Correct 143 ms 15716 KB Output is correct
16 Correct 265 ms 19772 KB Output is correct
17 Correct 151 ms 11088 KB Output is correct