Submission #1252691

#TimeUsernameProblemLanguageResultExecution timeMemory
1252691CodeLakVNRima (COCI17_rima)C++20
0 / 140
157 ms143452 KiB
#include <bits/stdc++.h>
using namespace std;

#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

const int MAX_N = (int)5e5 + 5;
const int MAX_LEN = (int)3e6 + 6;

int nWord;
string word[MAX_N];

struct NODE {
    int child[27];
    int par, res = 0;
    int firstMax = -1, secondMax = -1;
};
vector<NODE> trie;
NODE newNode;

int nNode = 0;

void updateMax(int &firstMax, int &secondMax, int x) {
    if (x > firstMax) secondMax = firstMax, firstMax = x;
    else if (x > secondMax) secondMax = x;
}

void addWord(string &s) {
    // add new word
    int id = 0;
    for (char ch : s) {
        int c = ch - 'a';
        if (!trie[id].child[c]) {
            trie[id].child[c] = ++nNode;
            trie.push_back(newNode);
            trie[nNode].par = id;
        }
        id = trie[id].child[c];
    }

    // compute longest subsequence
    int parID = trie[id].par;
    int a = trie[parID].firstMax, b = trie[parID].secondMax, c = trie[id].res;
    int &ans = trie[id].res;
    maximize(ans, c + 1); // Case 1: its self, diff at 0 letter
    maximize(ans, (c == a ? b : a) + 1); // Case 2: words that have same parent with it, diff at 1 letter
    maximize(ans, trie[parID].res + 1); // Case 3: words that ending at its parent node, diff at 1 letter
    FOR(c, 0, 25) if (trie[id].child[c])
        maximize(ans, trie[trie[id].child[c]].res + 1); // Case 4: words that ending at its children nodes, diff at 1 letter

    // update for its parent
    updateMax(trie[parID].firstMax, trie[parID].secondMax, ans);
}

void solve() {
    cin >> nWord;
    FOR(i, 1, nWord) {
        cin >> word[i];
        reverse(word[i].begin(), word[i].end());
    }

    trie.push_back(newNode);
    FOR(i, 1, nWord) addWord(word[i]);

    int ans = 0;
    FOR(i, 1, nWord) maximize(ans, trie[i].res);

    cout << ans;
}

int32_t main() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    bool multitest = 0;
    int numTest = 1;
    if (multitest) cin >> numTest;

    while (numTest--) {
        solve();
    }

    return 0;
}

/* Lak lu theo dieu nhac!!!! */

Compilation message (stderr)

rima.cpp: In function 'int32_t main()':
rima.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rima.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...