답안 #308046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
308046 2020-09-30T00:43:54 Z Elephant52 회문 (APIO14_palindrome) C++11
73 / 100
116 ms 120020 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vpi;

#define INF 1000000000
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)

void setIO(string name) {
    freopen((name+".in").c_str(), "r", stdin); 
    freopen((name+".out").c_str(), "w", stdout);
}

string s;

typedef struct node {
    int next[26], suflink, len;
    ll num;
    node() {};
} node;

node tree[300001];
int num, suf;
vi adj[300001];

bool add_letter(int pos) { //return 1 on new palindromes
    int cur = suf, letter = (s[pos]-'a'), curlen = 0;
    
    while(1) { //find longest palindromic suffix of s[0...pos-1] that can include s[pos]
        curlen = tree[cur].len;
        if (pos > curlen && s[pos-1-curlen] == s[pos]) break;
        cur = tree[cur].suflink;
    }
    
    if (tree[cur].next[letter]) { //if there exists a link from smaller palindrome to s[pos] + pal + s[pos] then do nothing
        suf = tree[cur].next[letter];
        tree[suf].num++;
        return 0;
    }
    
    //create a new palindrome with id num+1 in the tree
    
    suf = ++num;
    tree[num].num = 1;
    tree[num].len = tree[cur].len + 2; //add on s[pos]
    tree[cur].next[letter] = num;
    
    if (tree[num].len == 1) {
        tree[num].suflink = 2; //link to the empty palindrome
        adj[2].PB(num);
        return 1;
    } 
    
    while(1) {
        cur = tree[cur].suflink;
        curlen = tree[cur].len;
        if (pos > curlen && s[pos-1-curlen] == s[pos]) {
            tree[num].suflink = tree[cur].next[letter];
            adj[tree[cur].next[letter]].PB(num);
            break;
        }
    }
    
    return 1;
}

void dfs(int cur = 1) {
    for (auto edge : adj[cur]) {
        dfs(edge);
        tree[cur].num += tree[edge].num;
    }
}

void init() {
    num = suf = 2;
    tree[1].len = -1, tree[2].len = 0;
    tree[1].suflink = tree[2].suflink = 1;
    adj[1].PB(2);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> s;
    
    init();
    
    rep(i,0,s.length()) add_letter(i);
    
    ll ans = 0;
    
    dfs();
    
    rep(i,1,300001) ans = max(ans, (long long)tree[i].len * tree[i].num);
    
    cout << ans << endl;

    return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:15:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 | #define rep(i,a,b) for (int i = a; i < b; i++)
......
   96 |     rep(i,0,s.length()) add_letter(i);
      |         ~~~~~~~~~~~~~~                
palindrome.cpp:96:5: note: in expansion of macro 'rep'
   96 |     rep(i,0,s.length()) add_letter(i);
      |     ^~~
palindrome.cpp: In function 'void setIO(std::string)':
palindrome.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   18 |     freopen((name+".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   19 |     freopen((name+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 10 ms 7424 KB Output is correct
5 Correct 10 ms 7424 KB Output is correct
6 Correct 10 ms 7424 KB Output is correct
7 Correct 10 ms 7552 KB Output is correct
8 Correct 10 ms 7424 KB Output is correct
9 Correct 10 ms 7424 KB Output is correct
10 Correct 10 ms 7424 KB Output is correct
11 Correct 10 ms 7424 KB Output is correct
12 Correct 10 ms 7424 KB Output is correct
13 Correct 10 ms 7424 KB Output is correct
14 Correct 10 ms 7424 KB Output is correct
15 Correct 10 ms 7424 KB Output is correct
16 Correct 10 ms 7424 KB Output is correct
17 Correct 10 ms 7424 KB Output is correct
18 Correct 10 ms 7424 KB Output is correct
19 Correct 10 ms 7424 KB Output is correct
20 Correct 10 ms 7424 KB Output is correct
21 Correct 11 ms 7424 KB Output is correct
22 Correct 10 ms 7424 KB Output is correct
23 Correct 10 ms 7424 KB Output is correct
24 Correct 10 ms 7424 KB Output is correct
25 Correct 10 ms 7424 KB Output is correct
26 Correct 10 ms 7424 KB Output is correct
27 Correct 11 ms 7424 KB Output is correct
28 Correct 10 ms 7424 KB Output is correct
29 Correct 10 ms 7424 KB Output is correct
30 Correct 10 ms 7424 KB Output is correct
31 Correct 10 ms 7424 KB Output is correct
32 Correct 10 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7680 KB Output is correct
2 Correct 10 ms 7552 KB Output is correct
3 Correct 10 ms 7680 KB Output is correct
4 Correct 10 ms 7552 KB Output is correct
5 Correct 10 ms 7680 KB Output is correct
6 Correct 11 ms 7680 KB Output is correct
7 Correct 10 ms 7552 KB Output is correct
8 Correct 10 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 11 ms 7424 KB Output is correct
11 Correct 10 ms 7424 KB Output is correct
12 Correct 10 ms 7552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9216 KB Output is correct
2 Correct 12 ms 9088 KB Output is correct
3 Correct 12 ms 9472 KB Output is correct
4 Correct 11 ms 9344 KB Output is correct
5 Correct 11 ms 8832 KB Output is correct
6 Correct 12 ms 8832 KB Output is correct
7 Correct 12 ms 8960 KB Output is correct
8 Correct 10 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 11 ms 8320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 24704 KB Output is correct
2 Correct 26 ms 23672 KB Output is correct
3 Correct 28 ms 27136 KB Output is correct
4 Correct 27 ms 25344 KB Output is correct
5 Correct 29 ms 20480 KB Output is correct
6 Correct 22 ms 18048 KB Output is correct
7 Correct 24 ms 20480 KB Output is correct
8 Correct 12 ms 7808 KB Output is correct
9 Correct 15 ms 12032 KB Output is correct
10 Correct 24 ms 18432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 116 ms 120020 KB Execution killed with signal 7 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -