답안 #44656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
44656 2018-04-04T12:16:26 Z yusufake 회문 (APIO14_palindrome) C++
100 / 100
40 ms 40308 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 (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 (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:10: 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 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 552 KB Output is correct
4 Correct 2 ms 556 KB Output is correct
5 Correct 2 ms 704 KB Output is correct
6 Correct 2 ms 712 KB Output is correct
7 Correct 2 ms 712 KB Output is correct
8 Correct 2 ms 764 KB Output is correct
9 Correct 2 ms 772 KB Output is correct
10 Correct 2 ms 852 KB Output is correct
11 Correct 2 ms 872 KB Output is correct
12 Correct 2 ms 880 KB Output is correct
13 Correct 2 ms 880 KB Output is correct
14 Correct 2 ms 916 KB Output is correct
15 Correct 2 ms 920 KB Output is correct
16 Correct 2 ms 1036 KB Output is correct
17 Correct 2 ms 1036 KB Output is correct
18 Correct 2 ms 1036 KB Output is correct
19 Correct 2 ms 1036 KB Output is correct
20 Correct 2 ms 1036 KB Output is correct
21 Correct 2 ms 1036 KB Output is correct
22 Correct 2 ms 1036 KB Output is correct
23 Correct 2 ms 1036 KB Output is correct
24 Correct 2 ms 1036 KB Output is correct
25 Correct 2 ms 1036 KB Output is correct
26 Correct 2 ms 1036 KB Output is correct
27 Correct 2 ms 1036 KB Output is correct
28 Correct 2 ms 1036 KB Output is correct
29 Correct 2 ms 1036 KB Output is correct
30 Correct 2 ms 1036 KB Output is correct
31 Correct 2 ms 1036 KB Output is correct
32 Correct 2 ms 1036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1252 KB Output is correct
2 Correct 2 ms 1252 KB Output is correct
3 Correct 2 ms 1252 KB Output is correct
4 Correct 2 ms 1252 KB Output is correct
5 Correct 2 ms 1252 KB Output is correct
6 Correct 2 ms 1252 KB Output is correct
7 Correct 2 ms 1296 KB Output is correct
8 Correct 2 ms 1296 KB Output is correct
9 Correct 2 ms 1296 KB Output is correct
10 Correct 2 ms 1296 KB Output is correct
11 Correct 2 ms 1296 KB Output is correct
12 Correct 2 ms 1296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2472 KB Output is correct
2 Correct 3 ms 2504 KB Output is correct
3 Correct 3 ms 2516 KB Output is correct
4 Correct 3 ms 2528 KB Output is correct
5 Correct 3 ms 2556 KB Output is correct
6 Correct 3 ms 2568 KB Output is correct
7 Correct 3 ms 2596 KB Output is correct
8 Correct 2 ms 2596 KB Output is correct
9 Correct 2 ms 2596 KB Output is correct
10 Correct 3 ms 2596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 13268 KB Output is correct
2 Correct 13 ms 13268 KB Output is correct
3 Correct 13 ms 13344 KB Output is correct
4 Correct 14 ms 13444 KB Output is correct
5 Correct 13 ms 13544 KB Output is correct
6 Correct 10 ms 13544 KB Output is correct
7 Correct 12 ms 13544 KB Output is correct
8 Correct 5 ms 13544 KB Output is correct
9 Correct 7 ms 13544 KB Output is correct
10 Correct 10 ms 13544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 37344 KB Output is correct
2 Correct 36 ms 37608 KB Output is correct
3 Correct 37 ms 37988 KB Output is correct
4 Correct 35 ms 38396 KB Output is correct
5 Correct 40 ms 38500 KB Output is correct
6 Correct 31 ms 38500 KB Output is correct
7 Correct 32 ms 38500 KB Output is correct
8 Correct 9 ms 38500 KB Output is correct
9 Correct 10 ms 38500 KB Output is correct
10 Correct 30 ms 38500 KB Output is correct
11 Correct 30 ms 40308 KB Output is correct
12 Correct 10 ms 40308 KB Output is correct