답안 #209820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
209820 2020-03-15T15:45:05 Z jhnah917 회문 (APIO14_palindrome) C++14
73 / 100
1000 ms 60972 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll p1 = 179, mod1 = 1e9-63;
const ll p2 = 917, mod2 = 1e9+7;

int n;
char s[606060];
int dp[606060];
ll ha1[606060], pw1[606060];
ll ha2[606060], pw2[606060];

int pv = 0;
map<pair<ll, ll>, int> mp;
int cnt[303030];
int len[303030];
int lnk[303030];
vector<int> g[303030];

void manacher(){
    for(int i=n-1; i>=0; i--){
        s[i << 1 | 1] = s[i];
        s[i << 1] = '#';
    }
    n <<= 1; s[n++] = '#';
    int r = -1, p = -1;
    for(int i=0; i<n; i++){
        dp[i] = (i <= r ? min(dp[p*2-i], r-i) : 0);
        while(i-dp[i]-1 >= 0 && i+dp[i]+1 < n && s[i-dp[i]-1] == s[i+dp[i]+1]) dp[i]++;
        if(i+dp[i] > r) r = i+dp[i], p = i;
    }
}
pair<ll, ll> substr(int l, int r){
    ll r1 = ha1[l] - ha1[r+1] * pw1[r-l+1]; r1 %= mod1; if(r1 < 0) r1 += mod1;
    ll r2 = ha2[l] - ha2[r+1] * pw2[r-l+1]; r2 %= mod2; if(r2 < 0) r2 += mod2;
    return {r1, r2};
}

void go(int s, int e, pair<ll, ll> now){
    if(e-s+1 <= 2) return;
    auto par = substr(s+1, e-1);
    int v = mp[now], u;
    if(!mp.count(par)){
        u = mp[par] = ++pv; len[u] = e-s-1;
    }else u = mp[par];
    if(lnk[v]) return; lnk[v] = 1;
    g[u].push_back(v);
    go(s+1, e-1, par);
}

int sz[303030];
int chk[303030];
void dfs(int v){
    sz[v] = cnt[v];
    chk[v] = 1;
    for(auto i : g[v]){
        if(chk[i]) continue;
        dfs(i); sz[v] += sz[i];
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> s; n = strlen(s);
    pw1[0] = 1, pw1[1] = p1;
    pw2[0] = 1, pw2[1] = p2;
    for(int i=n-1; i>=0; i--) ha1[i] = (ha1[i+1] * p1 + s[i]) % mod1;
    for(int i=2; i<=n; i++) pw1[i] = pw1[i-1] * p1 % mod1;
    for(int i=n-1; i>=0; i--) ha2[i] = (ha2[i+1] * p2 + s[i]) % mod2;
    for(int i=2; i<=n; i++) pw2[i] = pw2[i-1] * p2 % mod2;
    manacher();

    for(int i=0; i<n; i++){
        if(!dp[i]) continue;
        int s, e;
        if(i & 1) s = i/2-dp[i]/2, e = i/2+dp[i]/2;
        else{
            s = i-1, e = i+1; s /= 2, e /= 2;
            s -= dp[i]/2-1;
            e += dp[i]/2-1;
        }
        auto now = substr(s, e);
        if(!mp.count(now)){
            mp[now] = ++pv; len[mp[now]] = e-s+1;
        }
        cnt[mp[now]]++;
        go(s, e, now);
    }

    ll ans = 0;
    for(int i=1; i<=pv; i++) if(!sz[i]) dfs(i);
    for(int i=1; i<=pv; i++){
        ans = max(ans, (ll)len[i] * sz[i]);
    }
    cout << ans;
};

// banana

Compilation message

palindrome.cpp: In function 'void go(int, int, std::pair<long long int, long long int>)':
palindrome.cpp:47:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if(lnk[v]) return; lnk[v] = 1;
     ^~
palindrome.cpp:47:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
     if(lnk[v]) return; lnk[v] = 1;
                        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 9 ms 7548 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7544 KB Output is correct
5 Correct 9 ms 7544 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 9 ms 7544 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7548 KB Output is correct
12 Correct 9 ms 7544 KB Output is correct
13 Correct 9 ms 7544 KB Output is correct
14 Correct 9 ms 7544 KB Output is correct
15 Correct 9 ms 7544 KB Output is correct
16 Correct 9 ms 7548 KB Output is correct
17 Correct 9 ms 7544 KB Output is correct
18 Correct 9 ms 7544 KB Output is correct
19 Correct 9 ms 7544 KB Output is correct
20 Correct 9 ms 7548 KB Output is correct
21 Correct 9 ms 7544 KB Output is correct
22 Correct 9 ms 7544 KB Output is correct
23 Correct 9 ms 7544 KB Output is correct
24 Correct 11 ms 7544 KB Output is correct
25 Correct 9 ms 7544 KB Output is correct
26 Correct 9 ms 7544 KB Output is correct
27 Correct 9 ms 7544 KB Output is correct
28 Correct 9 ms 7544 KB Output is correct
29 Correct 9 ms 7544 KB Output is correct
30 Correct 9 ms 7544 KB Output is correct
31 Correct 9 ms 7544 KB Output is correct
32 Correct 9 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7676 KB Output is correct
2 Correct 10 ms 7672 KB Output is correct
3 Correct 11 ms 7672 KB Output is correct
4 Correct 10 ms 7672 KB Output is correct
5 Correct 11 ms 7672 KB Output is correct
6 Correct 11 ms 7672 KB Output is correct
7 Correct 10 ms 7672 KB Output is correct
8 Correct 10 ms 7672 KB Output is correct
9 Correct 9 ms 7672 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7544 KB Output is correct
12 Correct 10 ms 7672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 9340 KB Output is correct
2 Correct 22 ms 9208 KB Output is correct
3 Correct 29 ms 9336 KB Output is correct
4 Correct 28 ms 9336 KB Output is correct
5 Correct 17 ms 9464 KB Output is correct
6 Correct 19 ms 9336 KB Output is correct
7 Correct 23 ms 9080 KB Output is correct
8 Correct 11 ms 7928 KB Output is correct
9 Correct 10 ms 7928 KB Output is correct
10 Correct 18 ms 8824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 229 ms 25312 KB Output is correct
2 Correct 224 ms 24312 KB Output is correct
3 Correct 298 ms 25312 KB Output is correct
4 Correct 285 ms 24440 KB Output is correct
5 Correct 122 ms 25848 KB Output is correct
6 Correct 112 ms 20984 KB Output is correct
7 Correct 184 ms 21892 KB Output is correct
8 Correct 25 ms 11768 KB Output is correct
9 Correct 69 ms 14712 KB Output is correct
10 Correct 109 ms 23288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 899 ms 60972 KB Output is correct
2 Correct 822 ms 55928 KB Output is correct
3 Execution timed out 1104 ms 60920 KB Time limit exceeded
4 Halted 0 ms 0 KB -