Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMAX 50005
- using namespace std;
- ifstream fin("subsecvente2.in");
- ofstream fout("subsecvente2.out");
- struct TRIE{
- int nd;
- TRIE *son[2];
- TRIE()
- {
- nd = 1;
- memset(son, 0, sizeof(son));
- }
- };
- void add_word(TRIE *nod, char cuv[], int poz, int dim)
- {
- if (cuv[poz] == '\0' || dim == 61)
- return;
- int nxt = cuv[poz] - 'a';
- if (nod -> son[nxt] == 0)
- nod -> son[nxt] = new TRIE();
- add_word(nod -> son[nxt], cuv, poz + 1, dim + 1);
- }
- int N, mx;
- void search_word(TRIE *nod, char cuv[], int poz, int dim, int act)
- {
- if (nod -> nd == act - 1) nod -> nd = act;
- if (nod -> nd == N) mx = max(mx, dim);
- if (cuv[poz] == '\0' || dim == 61)
- return;
- int nxt = cuv[poz] - 'a';
- if (nod -> son[nxt] != 0 && nod -> son[nxt] -> nd >= act-1 )
- search_word(nod -> son[nxt], cuv, poz + 1, dim + 1, act);
- }
- char cuv[NMAX];
- TRIE *ROOT = new TRIE();
- int main()
- {
- fin >> N;
- for (int i = 1; i <= N; i++)
- {
- fin >> cuv;
- int L = strlen(cuv);
- for (int j = 0; j < L; j++)
- {
- if (i == 1)
- add_word(ROOT, cuv, j, 0);
- else search_word(ROOT, cuv, j, 0, i);
- }
- }
- fout << mx << "\n";
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement