Submission #16192

# Submission time Handle Problem Language Result Execution time Memory
16192 2015-08-16T13:11:14 Z erdemkiraz Palindromes (APIO14_palindrome) C++14
100 / 100
573 ms 65864 KB
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)
#define hash ___hash

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 3e5 + 5;
const int LOG = 19;
const int ODD = 0;
const int EVEN = 1;
const int QU = 2;

int n;
char s[N];

class event{ public:
    int x, id, type;
    bool operator<(event oth) const {
        if(x != oth.x)
            return x < oth.x;
        return type < oth.type;
    }
};

int go_odd[N], go_even[N], tmp[N << 1], rad[N << 1];

void manacher() {
    int m = n * 2 + 1;
    for(int i = 0; i < m; i++)
        tmp[i] = '#';
    for(int i = 0; i < n; i++)
        tmp[i * 2 + 1] = s[i + 1];
    int i = 0, j = 0;
    while(i < m) {
        while(i - j >= 0 and i + j < m and tmp[i - j] == tmp[i + j])
            j++;
        rad[i] = j;
        int k = 1;
        while(rad[i - k] < rad[i] - k) {
            rad[i + k] = rad[i - k];
            k++;
        }
        i += k;
        j = max(0, j - k);
    }
    for(int i = 1; i <= n; i++)
        go_odd[i] = rad[(i - 1) * 2 + 1] / 2;
    for(int i = 1; i <= n; i++)
        go_even[i - 1] = rad[(i - 1) * 2] / 2;
}

int suf[N], order[N], lcp_table[N], sum[N], size[N];
pair < ii, int > p[N], temp_p[N];

void fast_sort() {
    for(int i = 1; i <= n; i++)
        size[p[i].first.second]++;
    sum[0] = 0;
    for(int i = 1; i < N; i++) {
        sum[i] = sum[i - 1] + size[i - 1];
        size[i - 1] = 0;
    }
    for(int i = 1; i <= n; i++)
        temp_p[++sum[p[i].first.second]] = p[i];
    for(int i = 1; i <= n; i++)
        size[temp_p[i].first.first]++;
    sum[0] = 0;
    for(int i = 1; i < N; i++) {
        sum[i] = sum[i - 1] + size[i - 1];
        size[i - 1] = 0;
    }
    for(int i = 1; i <= n; i++)
        p[++sum[temp_p[i].first.first]] = temp_p[i];
}

bool one_pass(int k) {
    for(int i = 1; i <= n; i++)
        p[i] = {{suf[i], i + (1 << k) <= n ? suf[i + (1 << k)] : 0}, i};
    fast_sort();
    //sort(p + 1, p + n + 1);
    int ptr = 1;
    for(int i = 1; i <= n; i++) {
        if(i - 1 >= 1 and p[i - 1].first != p[i].first)
            ptr++;
        suf[p[i].second] = ptr;
    }
    return ptr == n;
}

void build_sa() {
    for(int i = 1; i <= n; i++)
        suf[i] = s[i];
    for(int i = 0; i < LOG; i++)
        if(one_pass(i))
            break;
    for(int i = 1; i <= n; i++)
        order[suf[i]] = i;
    //for(int i = 1; i <= n; i++)
    //    printf("order[%d] = %d\n", i, order[i]);
}

void build_lcp() {
    int j = 0;
    for(int i = 1; i <= n; i++) {
        if(suf[i] == 1)
            continue;
        while(i + j <= n and order[suf[i] - 1] + j <= n and s[i + j] == s[order[suf[i] - 1] + j])
            j++;
        lcp_table[suf[i]] = j;
        if(j)
            j--;
    }
    //for(int i = 1; i <= n; i++)
    //    printf("lcp_table[%d] = %d\n", i, lcp_table[i]);
}

int a[N];
ll ans;

void build_table() {
    vector < event > v;
    for(int i = 1; i <= n; i++) {
        v.push_back({i - go_odd[i] + 1, i, ODD});
        if(go_even[i])
            v.push_back({i - go_even[i] + 1, i, EVEN});
        v.push_back({i, i, QU});
    }
    sort(v.begin(), v.end());
    set < int > s1, s2;
    foreach(it, v) {
        int x = it -> id;
        int type = it -> type;
        //printf("len = %d x = %d type = %d\n", it -> x, x, type);
        if(type == ODD)
            s1.insert(x);
        else if(type == EVEN)
            s2.insert(x);
        else {
            int val1 = (lcp_table[suf[x]] + 2 * x - 1) / 2;
            int val2 = (lcp_table[suf[x]] + 2 * x - 2) / 2;
            set < int > :: iterator it = s1.upper_bound(val1);
            if(it != s1.begin()) {
                it--;
                a[suf[x]] = max(a[suf[x]], (*it - x) * 2 + 1);
            }
            if(s2.size()) {
                it = s2.upper_bound(val2);
                if(it != s2.begin()) {
                    it--;
                    a[suf[x]] = max(a[suf[x]], (*it - x) * 2 + 2);
                }
            }
        }
    }
    //for(int i = 1; i <= n; i++)
    //    printf("a[%d] = %d\n", i, a[i]);
}

int sz;
ii vec[N];

void solve() {
    for(int i = 1; i <= n; i++)
        ans = max(ans, (ll) max(go_odd[i] * 2 - 1, go_even[i] * 2));
    build_table();
    for(int i = 2; i <= n + 1; i++) {
        int st = i;
        while(sz and vec[sz - 1].first >= a[i]) {
            ans = max(ans, (ll) (i - vec[sz - 1].second + 1) * vec[sz - 1].first);
            st = vec[sz - 1].second;
            sz--;
        }
        vec[sz++] = ii(a[i], st);
    }
}

int main () {
    
    scanf("%s", s + 1);
    n = strlen(s + 1);
    
    manacher();
    build_sa();
    build_lcp();
    solve();
    
    printf("%lld\n", ans);
    
    return 0;
    
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 25460 KB Output is correct - answer is '7'
2 Correct 4 ms 25460 KB Output is correct - answer is '4'
3 Correct 9 ms 25460 KB Output is correct - answer is '9'
4 Correct 0 ms 25460 KB Output is correct - answer is '1'
5 Correct 0 ms 25460 KB Output is correct - answer is '1'
6 Correct 3 ms 25460 KB Output is correct - answer is '2'
7 Correct 0 ms 25460 KB Output is correct - answer is '3'
8 Correct 0 ms 25460 KB Output is correct - answer is '3'
9 Correct 5 ms 25460 KB Output is correct - answer is '15'
10 Correct 14 ms 25460 KB Output is correct - answer is '24'
11 Correct 7 ms 25460 KB Output is correct - answer is '10'
12 Correct 8 ms 25460 KB Output is correct - answer is '24'
13 Correct 7 ms 25460 KB Output is correct - answer is '25'
14 Correct 13 ms 25460 KB Output is correct - answer is '28'
15 Correct 8 ms 25460 KB Output is correct - answer is '2'
16 Correct 0 ms 25460 KB Output is correct - answer is '1'
17 Correct 13 ms 25460 KB Output is correct - answer is '15'
18 Correct 7 ms 25460 KB Output is correct - answer is '18'
19 Correct 16 ms 25460 KB Output is correct - answer is '1176'
20 Correct 16 ms 25460 KB Output is correct - answer is '1225'
21 Correct 10 ms 25460 KB Output is correct - answer is '136'
22 Correct 9 ms 25460 KB Output is correct - answer is '45'
23 Correct 19 ms 25460 KB Output is correct - answer is '2500'
24 Correct 10 ms 25460 KB Output is correct - answer is '380'
25 Correct 12 ms 25460 KB Output is correct - answer is '2304'
26 Correct 12 ms 25460 KB Output is correct - answer is '110'
27 Correct 14 ms 25460 KB Output is correct - answer is '93'
28 Correct 13 ms 25460 KB Output is correct - answer is '729'
29 Correct 11 ms 25460 KB Output is correct - answer is '132'
30 Correct 3 ms 25460 KB Output is correct - answer is '7'
31 Correct 3 ms 25460 KB Output is correct - answer is '8'
32 Correct 13 ms 25460 KB Output is correct - answer is '64'
# Verdict Execution time Memory Grader output
1 Correct 22 ms 25460 KB Output is correct - answer is '124251'
2 Correct 24 ms 25460 KB Output is correct - answer is '38226'
3 Correct 27 ms 25592 KB Output is correct - answer is '249500'
4 Correct 19 ms 25460 KB Output is correct - answer is '5778'
5 Correct 20 ms 25592 KB Output is correct - answer is '247506'
6 Correct 19 ms 25592 KB Output is correct - answer is '248004'
7 Correct 24 ms 25460 KB Output is correct - answer is '973'
8 Correct 27 ms 25460 KB Output is correct - answer is '123753'
9 Correct 18 ms 25460 KB Output is correct - answer is '2346'
10 Correct 11 ms 25460 KB Output is correct - answer is '53'
11 Correct 12 ms 25460 KB Output is correct - answer is '52'
12 Correct 25 ms 25460 KB Output is correct - answer is '976'
# Verdict Execution time Memory Grader output
1 Correct 37 ms 26240 KB Output is correct - answer is '12497500'
2 Correct 34 ms 26240 KB Output is correct - answer is '6481800'
3 Correct 38 ms 26768 KB Output is correct - answer is '25000000'
4 Correct 41 ms 26768 KB Output is correct - answer is '17816841'
5 Correct 34 ms 26240 KB Output is correct - answer is '9945'
6 Correct 33 ms 26372 KB Output is correct - answer is '504100'
7 Correct 32 ms 26240 KB Output is correct - answer is '1512930'
8 Correct 12 ms 26240 KB Output is correct - answer is '413'
9 Correct 17 ms 26240 KB Output is correct - answer is '428'
10 Correct 33 ms 26372 KB Output is correct - answer is '7232'
# Verdict Execution time Memory Grader output
1 Correct 127 ms 33152 KB Output is correct - answer is '1249925001'
2 Correct 115 ms 33152 KB Output is correct - answer is '396337935'
3 Correct 194 ms 40976 KB Output is correct - answer is '2500050000'
4 Correct 170 ms 40976 KB Output is correct - answer is '1016525689'
5 Correct 123 ms 33152 KB Output is correct - answer is '99645'
6 Correct 110 ms 33152 KB Output is correct - answer is '78569380'
7 Correct 143 ms 35528 KB Output is correct - answer is '82810000'
8 Correct 63 ms 33284 KB Output is correct - answer is '3989'
9 Correct 162 ms 34340 KB Output is correct - answer is '125529616'
10 Correct 161 ms 34736 KB Output is correct - answer is '66664'
# Verdict Execution time Memory Grader output
1 Correct 344 ms 51740 KB Output is correct - answer is '11250075000'
2 Correct 321 ms 51740 KB Output is correct - answer is '894243195'
3 Correct 573 ms 65864 KB Output is correct - answer is '22499400004'
4 Correct 494 ms 65864 KB Output is correct - answer is '2958163321'
5 Correct 327 ms 51740 KB Output is correct - answer is '298935'
6 Correct 382 ms 55568 KB Output is correct - answer is '1210831209'
7 Correct 385 ms 58076 KB Output is correct - answer is '303195156'
8 Correct 197 ms 52268 KB Output is correct - answer is '11804'
9 Correct 189 ms 52268 KB Output is correct - answer is '11813'
10 Correct 455 ms 56492 KB Output is correct - answer is '262144'
11 Correct 332 ms 51740 KB Output is correct - answer is '3750025000'
12 Correct 427 ms 53456 KB Output is correct - answer is '184796836'