Submission #1004918

#TimeUsernameProblemLanguageResultExecution timeMemory
1004918vjudge1Rima (COCI17_rima)C++17
140 / 140
224 ms162668 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (b); i >= (a); i --) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPD(i, n) for (int i = (n) - 1; i >= 0; --i) #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) constexpr ll LINF = (1ll << 60); constexpr int INF = (1ll << 30); constexpr int Mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* Phu Trong from Nguyen Tat Thanh High School for gifted student */ template <class X, class Y> bool minimize(X &x, const Y &y){ X eps = 1e-9; if (x > y + eps) {x = y; return 1;} return 0; } template <class X, class Y> bool maximize(X &x, const Y &y){ X eps = 1e-9; if (x + eps < y) {x = y; return 1;} return 0; } int n; #define All(x...) (x).begin(), (x).end() struct Node{ Node* child[26]; int last, dp; Node(){ REP(i, 26) child[i] = nullptr; last = 0, dp = 0; } }; int numNode = 0; Node *root = new Node(); void Add(string &s){ Node *p = root; for (char c : s){ int nxt = (c - 'a'); if (p -> child[nxt] == nullptr){ p -> child[nxt] = new Node(); } p = p -> child[nxt]; } p -> last = 1; } int ans = 0; void dfs(Node *p){ REP(c, 26) if(p -> child[c] != nullptr){ dfs(p -> child[c]); } int cnt = 0; int mx_first = 0, mx_second = 0; REP(c, 26){ if (p -> child[c] != nullptr){ if (p -> child[c] -> last){ ++cnt; if (p -> child[c] -> dp > mx_first) swap(mx_first, mx_second), mx_first = p -> child[c] -> dp; else maximize(mx_second, p -> child[c] -> dp); } } } p -> dp = mx_first + cnt; maximize(ans, p -> dp + mx_second + p -> last); } void process(void){ cin >> n; vector<string> s(n); for (string&i : s) cin >> i; for (string&i : s) reverse(All(i)), Add(i); dfs(root); cout << ans; } signed main(){ #define name "Whisper" cin.tie(nullptr) -> sync_with_stdio(false); //freopen(name".inp", "r", stdin); //freopen(name".out", "w", stdout); process(); }
#Verdict Execution timeMemoryGrader output
Fetching results...