Submission #348842

# Submission time Handle Problem Language Result Execution time Memory
348842 2021-01-15T22:47:37 Z ahoraSoyPeor Palindromes (APIO14_palindrome) C++14
100 / 100
97 ms 46800 KB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int N = 500'005;
const int MOD = 1'000'000'007;
const int LOGN = 17;

//int mul ( int A, int B ) { return int((ll(A)*ll(B))%ll(MOD)); }
//int add ( int A, int B ) { return A+B >= MOD? A+B-MOD: A+B; }
//int sub ( int A, int B ) { return add ( A, MOD-B ); }

struct palindromic_tree {
  struct node{
    int len, link; ll cnt;
    map <char,int> next;
  };
  vector <node> pt;
  int last, it;
  string s;
  palindromic_tree (string str = "") {
    pt.reserve ( s.size()+2 );
    s = '#', it = 1, last = 1; //# isn't used in string
    add_node (), add_node();
    pt[1].len = -1, pt[0].link = 1;
    for ( char &c: str ) add_letter ( c );
  }
  int add_node () {
    pt.push_back({});
    return pt.size()-1;
  }
  int get_link ( int v ) {
    while ( s[it-pt[v].len-2] != s[it-1] ) v = pt[v].link;
    return v;
  }
  void add_letter ( char c ) {
    s += c, ++it;
    last = get_link ( last );
    if ( !pt[last].next.count (c) or !pt[last].next[c] ) {
      int curr = add_node ();
      pt[curr].len = pt[last].len+2;
      pt[curr].link = pt[get_link(pt[last].link)].next[c];
      pt[last].next[c] = curr;
    }
    last = pt[last].next[c];
    pt[last].cnt++;
  }
  node& operator[](int i) { return pt[i]; }
  int size() { return pt.size(); }
};


int main () {

  ios_base::sync_with_stdio(0); cin.tie(0);

  #ifdef LOCAL
    freopen ( "in.txt", "r", stdin );
  #endif // LOCAL

  string str;
  cin >> str;

  palindromic_tree root (str);
  vector <pii> toSort (root.size()-2);
  for ( int i = 2; i < root.size(); ++i )
    toSort[i-2] = {root[i].len, i};
  sort ( toSort.rbegin(), toSort.rend() );
  ll maxi = 0;
  for ( pii it: toSort ) {
    root[root[it.ss].link].cnt += root[it.ss].cnt;
    maxi = max ( maxi, root[it.ss].cnt*it.ff );
  }
  cout << maxi << "\n";

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1740 KB Output is correct
2 Correct 3 ms 1740 KB Output is correct
3 Correct 3 ms 1740 KB Output is correct
4 Correct 3 ms 1740 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 3 ms 1868 KB Output is correct
7 Correct 3 ms 1740 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12500 KB Output is correct
2 Correct 35 ms 12436 KB Output is correct
3 Correct 30 ms 12500 KB Output is correct
4 Correct 33 ms 12500 KB Output is correct
5 Correct 29 ms 12500 KB Output is correct
6 Correct 23 ms 11992 KB Output is correct
7 Correct 27 ms 11992 KB Output is correct
8 Correct 9 ms 1004 KB Output is correct
9 Correct 12 ms 3556 KB Output is correct
10 Correct 25 ms 11992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 46656 KB Output is correct
2 Correct 97 ms 46672 KB Output is correct
3 Correct 76 ms 46656 KB Output is correct
4 Correct 94 ms 46656 KB Output is correct
5 Correct 91 ms 46800 KB Output is correct
6 Correct 86 ms 46672 KB Output is correct
7 Correct 74 ms 30504 KB Output is correct
8 Correct 24 ms 2164 KB Output is correct
9 Correct 24 ms 2164 KB Output is correct
10 Correct 73 ms 30120 KB Output is correct
11 Correct 82 ms 46656 KB Output is correct
12 Correct 28 ms 4692 KB Output is correct