답안 #42239

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42239 2018-02-23T21:50:00 Z yusufake 회문 (APIO14_palindrome) C++
100 / 100
39 ms 35000 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <utility>
#include <cstring>
#include <cassert>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

const int MAXN = 505000;

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

int len;
char s[MAXN];
node tree[MAXN]; 
int num;            // node 1 - root with len -1, node 2 - root with len 0
int suff;           // max suffix palindrome
long long x,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];
        tree[suff].num++;
        return false;
    }

    num++;
    suff = num;
    tree[num].len = tree[cur].len + 2;
    tree[cur].next[let] = num;
    tree[num].num = 1;
        
    if (tree[num].len == 1) {
        tree[num].sufflink = 2;
        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;
        }       
    }           

    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));

    scanf(" %s", s);
    len = strlen(s);

    initTree();

    for (int i = 0; i < len; i++) {
        addLetter(i);
    }

    for(int i=num; i ;i--){
        tree[ tree[i].sufflink ].num += x = tree[i].num;
        ans = max(ans , x*tree[i].len);
    }
    
    cout << ans << endl;

    return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:83:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %s", s);
                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 352 KB Output is correct
3 Correct 2 ms 424 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 476 KB Output is correct
6 Correct 2 ms 480 KB Output is correct
7 Correct 2 ms 480 KB Output is correct
8 Correct 2 ms 480 KB Output is correct
9 Correct 2 ms 528 KB Output is correct
10 Correct 2 ms 532 KB Output is correct
11 Correct 2 ms 580 KB Output is correct
12 Correct 2 ms 580 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 1 ms 748 KB Output is correct
22 Correct 1 ms 748 KB Output is correct
23 Correct 1 ms 748 KB Output is correct
24 Correct 1 ms 748 KB Output is correct
25 Correct 1 ms 748 KB Output is correct
26 Correct 1 ms 748 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 1 ms 748 KB Output is correct
29 Correct 1 ms 748 KB Output is correct
30 Correct 2 ms 748 KB Output is correct
31 Correct 1 ms 748 KB Output is correct
32 Correct 1 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Correct 2 ms 748 KB Output is correct
4 Correct 2 ms 748 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 2 ms 748 KB Output is correct
7 Correct 1 ms 748 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 2 ms 748 KB Output is correct
10 Correct 2 ms 748 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
12 Correct 2 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1772 KB Output is correct
2 Correct 2 ms 1772 KB Output is correct
3 Correct 3 ms 1772 KB Output is correct
4 Correct 2 ms 1772 KB Output is correct
5 Correct 3 ms 1772 KB Output is correct
6 Correct 3 ms 1772 KB Output is correct
7 Correct 3 ms 1772 KB Output is correct
8 Correct 2 ms 1772 KB Output is correct
9 Correct 2 ms 1772 KB Output is correct
10 Correct 2 ms 1772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12028 KB Output is correct
2 Correct 14 ms 12028 KB Output is correct
3 Correct 14 ms 12028 KB Output is correct
4 Correct 13 ms 12028 KB Output is correct
5 Correct 13 ms 12028 KB Output is correct
6 Correct 12 ms 12028 KB Output is correct
7 Correct 11 ms 12028 KB Output is correct
8 Correct 3 ms 12028 KB Output is correct
9 Correct 5 ms 12028 KB Output is correct
10 Correct 11 ms 12028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 34924 KB Output is correct
2 Correct 36 ms 34924 KB Output is correct
3 Correct 34 ms 35000 KB Output is correct
4 Correct 35 ms 35000 KB Output is correct
5 Correct 37 ms 35000 KB Output is correct
6 Correct 34 ms 35000 KB Output is correct
7 Correct 31 ms 35000 KB Output is correct
8 Correct 7 ms 35000 KB Output is correct
9 Correct 7 ms 35000 KB Output is correct
10 Correct 29 ms 35000 KB Output is correct
11 Correct 35 ms 35000 KB Output is correct
12 Correct 9 ms 35000 KB Output is correct