Submission #1216034

#TimeUsernameProblemLanguageResultExecution timeMemory
1216034Ahmed_Salah7Rima (COCI17_rima)C++20
42 / 140
326 ms128272 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 = {Node()};

    Trie() {}

    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;

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];
    }
    sort(words.begin(), words.end(), [](const string &a, const string &b) {
        return a.size() > b.size();
    });
    int ans = 0;
    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);
            }
        }
        ans = max(ans, dp[i]);
        trie.add(words[i], i);
    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...