답안 #712958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
712958 2023-03-20T16:15:50 Z Victor 회문 (APIO14_palindrome) C++17
100 / 100
27 ms 36244 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
 
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
 
#define umap unordered_map
#define uset unordered_set
 
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
 
const int INF = 1000000007;

const int MAXN = 305000;

struct node {
    int next[26];
    int len;
    int sufflink;
    int num;
};

int len;
string s;
node tree[MAXN]; 
int num;            // node 1 - root with len -1, node 2 - root with len 0
int suff;           // max suffix palindrome
long long ans;

bool addLetter(int pos) {
    int cur = suff, curlen = 0;
    int let = s[pos] - 'a';

    while (true) {
        curlen = tree[cur].len;
        if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])     
            break;  
        cur = tree[cur].sufflink;
    }       
    if (tree[cur].next[let]) {  
        suff = tree[cur].next[let];
        return false;
    }

    num++;
    suff = num;
    tree[num].len = tree[cur].len + 2;
    tree[cur].next[let] = num;

    if (tree[num].len == 1) {
        tree[num].sufflink = 2;
        tree[num].num = 1;
        return true;
    }

    while (true) {
        cur = tree[cur].sufflink;
        curlen = tree[cur].len;
        if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
            tree[num].sufflink = tree[cur].next[let];
            break;
        }       
    }           

    tree[num].num = 1 + tree[tree[num].sufflink].num;

    return true;
}

void initTree() {
    num = 2; suff = 2;
    tree[1].len = -1; tree[1].sufflink = 1;
    tree[2].len = 0; tree[2].sufflink = 1;
}

int main() {
    //assert(freopen("input.txt", "r", stdin));
    //assert(freopen("output.txt", "w", stdout));

  	int cnt[MAXN];
  	memset(cnt, 0, sizeof(cnt));
    cin>>s;
    len = sz(s);

    initTree();
	ll ans2 = 0;
    for (int i = 0; i < len; i++) {
        addLetter(i);
        ++cnt[suff];
    }

  	per(i, 2, num+1) {
        ans2 = max(ans2, tree[i].len * ll(cnt[i]));
        cnt[tree[i].sufflink] += cnt[i];
    }
  
    cout << ans2 << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 2 ms 1496 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 2 ms 1500 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1496 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1500 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 1 ms 1492 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
21 Correct 2 ms 1492 KB Output is correct
22 Correct 1 ms 1496 KB Output is correct
23 Correct 1 ms 1500 KB Output is correct
24 Correct 1 ms 1492 KB Output is correct
25 Correct 1 ms 1492 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 1 ms 1492 KB Output is correct
29 Correct 1 ms 1492 KB Output is correct
30 Correct 1 ms 1492 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1620 KB Output is correct
2 Correct 1 ms 1620 KB Output is correct
3 Correct 1 ms 1620 KB Output is correct
4 Correct 1 ms 1508 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1496 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1500 KB Output is correct
12 Correct 1 ms 1496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2660 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2604 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 2 ms 2260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 13144 KB Output is correct
2 Correct 11 ms 13156 KB Output is correct
3 Correct 10 ms 13144 KB Output is correct
4 Correct 11 ms 13052 KB Output is correct
5 Correct 10 ms 13140 KB Output is correct
6 Correct 8 ms 10068 KB Output is correct
7 Correct 10 ms 11380 KB Output is correct
8 Correct 6 ms 1876 KB Output is correct
9 Correct 7 ms 4436 KB Output is correct
10 Correct 9 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 36196 KB Output is correct
2 Correct 27 ms 36244 KB Output is correct
3 Correct 27 ms 36152 KB Output is correct
4 Correct 27 ms 36228 KB Output is correct
5 Correct 26 ms 36240 KB Output is correct
6 Correct 25 ms 32380 KB Output is correct
7 Correct 25 ms 30424 KB Output is correct
8 Correct 12 ms 2416 KB Output is correct
9 Correct 12 ms 2404 KB Output is correct
10 Correct 24 ms 30024 KB Output is correct
11 Correct 27 ms 36200 KB Output is correct
12 Correct 14 ms 5360 KB Output is correct