Submission #380105

# Submission time Handle Problem Language Result Execution time Memory
380105 2021-03-20T09:57:05 Z alrad Savez (COCI15_savez) C++17
108 / 120
115 ms 17516 KB
#include <bits/stdc++.h>

using namespace std;

using ull = unsigned long long;
using ll = long long;

/*
#pragma comment(linker, "/STACK:256000000")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

template <class T> inline T gcd(T a , T b) { return !a ? b : gcd(b % a , a); }
template <class T> inline T lcm(T a , T b) { return (a * b) / gcd(a , b) ; }

mt19937 rnd(time(0));

#define all(x) x.begin(), x.end()
#define debug(x) { cerr << #x << " = " << x << endl; }

const int N = 2e6 + 1;
const int ALPHA = 26;

int dp[N];

struct node {
  node *next[ALPHA];
  int id;
  node() {
    for (int i = 0; i < ALPHA; i++) {
      next[i] = nullptr;
    }
    id = -1;
  }
};

node *root = new node();

void add(const string s, int cur) {
  node *v = root;
  for (char c : s) {
    int to = int(c - 'A');
    if (v->next[to] == nullptr) {
      v->next[to] = new node();
    }
    v = v->next[to];
  }
  if (v->id == -1 || (dp[cur] > dp[v->id])) {
    v->id = cur;
  }
  return;
}

int ask(const string s) {
  int ans = 0;
  string rev = s;
  reverse(all(rev));
  node *v = root;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] != rev[i]) {
      return ans;
    }
    int to = int(s[i] - 'A');
    if (v->next[to] == nullptr) {
      return ans;
    }
    v = v->next[to];
    if (v->id != -1) {
      ans = max(ans, dp[v->id]);
    }
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  int res = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    dp[i] = ask(s) + 1;
    res = max(res, dp[i]);
    add(s, i);
  }
  cout << res << '\n';
  return;
}

signed main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0) , cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Incorrect 1 ms 620 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 13 ms 17516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1772 KB Output is correct
2 Correct 12 ms 3180 KB Output is correct
3 Correct 12 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 16 ms 3948 KB Output is correct
3 Correct 15 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2412 KB Output is correct
2 Correct 14 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2284 KB Output is correct
2 Correct 14 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2412 KB Output is correct
2 Correct 17 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2540 KB Output is correct
2 Correct 22 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 2540 KB Output is correct
2 Correct 39 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 3436 KB Output is correct
2 Correct 64 ms 3308 KB Output is correct
3 Correct 115 ms 6292 KB Output is correct