Advertisement
cosenza987

Untitled

Jun 22nd, 2023
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 1e4 + 7;
  8.  
  9. string v[N];
  10. int dp[N];
  11.  
  12. template <int ALPHA_SIZE = 62>
  13. struct Aho {
  14.     struct Node {
  15.         int p, char_p, link = -1, str_idx = -1, nxt[ALPHA_SIZE];
  16.         bool has_end = false;
  17.         Node(int _p = -1, int _char_p = -1) : p(_p), char_p(_char_p) {
  18.             fill(nxt, nxt + ALPHA_SIZE, -1);
  19.         }
  20.     };
  21.  
  22.     vector<Node> nodes = { Node() };
  23.     int ans, cnt = 0;
  24.     bool build_done = false;
  25.     //vector<int> ord;
  26.     vector<int> vis;
  27.  
  28.     // change this if different alphabet
  29.     int remap(char c) {
  30.         if (islower(c)) return c - 'a';
  31.         if (isalpha(c)) return c - 'A' + 26;
  32.         return c - '0' + 52;
  33.     }
  34.  
  35.     void add(string& p, int id = -1) {
  36.         int u = 0;
  37.         if (id == -1) id = cnt++;
  38.  
  39.         for (char ch : p) {
  40.             int c = remap(ch);
  41.             if (nodes[u].nxt[c] == -1) {
  42.                 nodes[u].nxt[c] = (int)nodes.size();
  43.                 nodes.push_back(Node(u, c));
  44.             }
  45.  
  46.             u = nodes[u].nxt[c];
  47.         }
  48.  
  49.         nodes[u].str_idx = id;
  50.         nodes[u].has_end = true;
  51.     }
  52.  
  53.     void build() {
  54.         build_done = true;
  55.         queue<int> q;
  56.  
  57.         for (int i = 0; i < ALPHA_SIZE; i++) {
  58.             if (nodes[0].nxt[i] != -1) q.push(nodes[0].nxt[i]);
  59.             else nodes[0].nxt[i] = 0;
  60.         }
  61.  
  62.         while (q.size()) {
  63.             int u = q.front();
  64.             //ord.push_back(u);
  65.             q.pop();
  66.  
  67.             int j = nodes[nodes[u].p].link;
  68.             if (j == -1) nodes[u].link = 0;
  69.             else nodes[u].link = nodes[j].nxt[nodes[u].char_p];
  70.  
  71.             nodes[u].has_end |= nodes[nodes[u].link].has_end;
  72.  
  73.             for (int i = 0; i < ALPHA_SIZE; i++) {
  74.                 if (nodes[u].nxt[i] != -1) q.push(nodes[u].nxt[i]);
  75.                 else nodes[u].nxt[i] = nodes[nodes[u].link].nxt[i];
  76.             }
  77.         }
  78.         vis.resize(nodes.size());
  79.     }
  80.  
  81.     void match(string& s, int k) {
  82.         int u = 0;
  83.         for (char ch : s) {
  84.             int c = remap(ch);
  85.             u = nodes[u].nxt[c];
  86.             for(int cur = u; cur != 0; cur = nodes[cur].link) {
  87.                 if(nodes[cur].str_idx != -1 and nodes[cur].str_idx != k) {
  88.                     dp[k] = max(dp[k], dp[nodes[cur].str_idx] + 1);
  89.                     break;
  90.                 }
  91.             }
  92.         }
  93.     }
  94. };
  95.  
  96. int main() {
  97.     ios_base::sync_with_stdio(false);
  98.     cin.tie(nullptr);
  99.     //freopen("in.txt", "r", stdin);
  100.     //freopen("output.txt", "w", stdout);
  101.     auto start = chrono::high_resolution_clock::now();
  102.     int n;
  103.     while(cin >> n) {
  104.         if(!n) break;
  105.         Aho<26> aho;
  106.         for(int i = 0; i < n; i++) {
  107.             dp[i] = 1;
  108.             cin >> v[i];
  109.         }
  110.         sort(v, v + n, [](string &a, string &b) {
  111.             return a.size() < b.size();
  112.         });
  113.         for(int i = 0; i < n; i++) {
  114.             aho.add(v[i]);
  115.         }
  116.         aho.build();
  117.         int ans = 0;
  118.         for(int i = 0; i < n; i++) {
  119.             aho.match(v[i], i);
  120.             ans = max(ans, dp[i]);
  121.         }
  122.         cout << ans << "\n";
  123.     }
  124.     auto stop = chrono::high_resolution_clock::now();
  125.     cerr << chrono::duration_cast<chrono::milliseconds>(stop - start).count() << "\n";
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement