Submission #1216039

#TimeUsernameProblemLanguageResultExecution timeMemory
1216039Ahmed_Salah7Rima (COCI17_rima)C++20
14 / 140
267 ms128588 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e5 + 5, M = 1e6 + 6, mod = 998244353; int dp[N]; struct Node { int next[26]; int output = -1; Node() { memset(next, -1, sizeof(next)); } }; struct Trie { vector<Node> trie; Trie() { trie.emplace_back(Node()); } void add(string &s, int idx) { int u = 0; for (char ch: s) { int c = ch - 'a'; if (trie[u].next[c] == -1) { trie[u].next[c] = trie.size(); trie.emplace_back(Node()); } u = trie[u].next[c]; } trie[u].output = idx; } int search(string s) { int u = 0; for (char ch: s) { int c = ch - 'a'; if (trie[u].next[c] == -1) { return -1; } u = trie[u].next[c]; } return u; } } trie = Trie(); void update(int i, int idx) { for (auto v: trie.trie[idx].next) { if (v != -1) { int vIdx = trie.trie[v].output; if (vIdx != -1) { dp[i] = max(dp[i], dp[vIdx] + 1); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // int n; cin >> n; vector<string> words(n); for (int i = 0; i < n; i++) { cin >> words[i]; } int ans = 0; // sz // sz + 1 // sz - 1 for (int i = 0; i < n; i++) { dp[i] = 1; reverse(words[i].begin(), words[i].end()); for (int j = 0; j <= 1; ++j) { int idx = trie.search(words[i].substr(0, words[i].size() - j)); if (idx != -1) { update(i, idx); } if (j == 1 && idx != -1) { int vIdx = trie.trie[idx].output; dp[i] = max(dp[i], dp[vIdx] + 1); } } ans = max(ans, dp[i]); trie.add(words[i], i); } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...