Submission #1213517

#TimeUsernameProblemLanguageResultExecution timeMemory
1213517faresmagdRima (COCI17_rima)C++20
0 / 140
913 ms327680 KiB
#include <bits/stdc++.h>
using namespace std;
#define ln '\n'
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ll long long
#define ull unsigned long long
#define F first
#define S second

struct Node {
    int next[26];
    int length = 0, fail = 0, id = -1;

    Node() {
        memset(next, -1, sizeof(next));
    }
};

struct AhoCorasick {
    vector<Node> trie = {Node()};
    set<pair<int, int>, greater<>>endings;
    vector<string>strs;
    map<string, int>mp;

    void add(string& s) {
        int u = 0;
        for (int i = 0; i < int(s.size()); ++i) {
            int c = s[i] - 'a';
            if (trie[u].next[c] == -1) {
                trie[u].next[c] = trie.size();
                trie.emplace_back(Node());
                trie.back().length = trie[u].length + 1;
            }
            u = trie[u].next[c];
        }
        endings.insert({trie[u].length, u});
        reverse(all(s));
        s.pop_back();
        if(!s.empty()){
            mp[s]++;
            trie[u].id = strs.size();
            strs.emplace_back(s);
        }
    }

    void build() {
        queue<int> q;

        for (int c = 0; c < 26; ++c) {
            int u = trie[0].next[c];
            if (u != -1) {
                trie[u].fail = 0;
                q.emplace(u);
            } else {
                trie[0].next[c] = 0;
            }
        }

        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int c = 0; c < 26; ++c) {
                int v = trie[u].next[c];
                if (v == -1) continue;

                int f = trie[u].fail;
                while (trie[f].next[c] == -1)
                    f = trie[f].fail;
                trie[v].fail = trie[f].next[c];

                q.emplace(v);
            }
        }
    }

    int find_longest_sequence(){
        int ans = 0;
        vector<bool>vis(trie.size());
        for(auto [_, u] : endings){
            int cur = mp[strs[trie[u].id]] - 1;
            while (u != 0 && !vis[u]){
                vis[u] = 1;
                int v = trie[u].fail;
                if(v == 0 || trie[v].length + 1 != trie[u].length){
                    break;
                }
                u = v;
                cur++;
            }
            ans = max(ans, cur + 1);
        }
        return ans;
    }
};

void solve(){
    int n; cin >> n;
    string s;
    AhoCorasick ac = AhoCorasick();
    map<string, int>mp;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        ac.add(s);
    }
    ac.build();
    cout << ac.find_longest_sequence() << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("output.txt", "w", stdout);
#endif
    int t_t = 1; //cin >> t_t;
    while (t_t--){
        solve();
    }
    return 0;
}

Compilation message (stderr)

rima.cpp: In function 'int main()':
rima.cpp:114:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...