Advertisement
cosenza987

Untitled

Jun 23rd, 2023
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #pragma GCC optimize("O3")
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. const int N = 1e4 + 1, ALPHA_SIZE = 26;
  10.  
  11. string v[N];
  12. int dp[N];
  13.  
  14. namespace Aho {
  15.     struct Node {
  16.         int p, char_p, link = -1, str_idx = -1, nxt[ALPHA_SIZE];
  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;
  23.     int cnt = 0;
  24.  
  25.     // change this if different alphabet
  26.     inline void add(string& p, int id = -1) {
  27.         int u = 0;
  28.         if (id == -1) id = cnt++;
  29.  
  30.         for (char &ch : p) {
  31.             int c = ch - 'a';
  32.             if (nodes[u].nxt[c] == -1) {
  33.                 nodes[u].nxt[c] = (int)nodes.size();
  34.                 nodes.push_back(Node(u, c));
  35.             }
  36.  
  37.             u = nodes[u].nxt[c];
  38.         }
  39.  
  40.         nodes[u].str_idx = id;
  41.     }
  42.  
  43.     inline void build() {
  44.         queue<int> q;
  45.  
  46.         for (int i = 0; i < ALPHA_SIZE; i++) {
  47.             if (nodes[0].nxt[i] != -1) q.push(nodes[0].nxt[i]);
  48.             else nodes[0].nxt[i] = 0;
  49.         }
  50.  
  51.         while (q.size()) {
  52.             int u = q.front();
  53.             q.pop();
  54.  
  55.             int j = nodes[nodes[u].p].link;
  56.             if (j == -1) nodes[u].link = 0;
  57.             else nodes[u].link = nodes[j].nxt[nodes[u].char_p];
  58.             if(nodes[u].str_idx == -1) {
  59.                 nodes[u].str_idx = nodes[nodes[u].link].str_idx;
  60.             }
  61.  
  62.             for (int i = 0; i < ALPHA_SIZE; i++) {
  63.                 if (nodes[u].nxt[i] != -1) {
  64.                     q.push(nodes[u].nxt[i]);
  65.                 } else {
  66.                     nodes[u].nxt[i] = nodes[nodes[u].link].nxt[i];
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     inline void match(string& s, int k) {
  73.         int u = 0;
  74.         for (char &ch : s) {
  75.             int c = ch - 'a';
  76.             u = nodes[u].nxt[c];
  77.             int x = nodes[u].str_idx;
  78.             if(x != k and x != -1) {
  79.                 dp[k] = max(dp[k], dp[x] + 1);
  80.             }
  81.             x = nodes[nodes[u].link].str_idx;
  82.             if(x != k and x != -1) {
  83.                 dp[k] = max(dp[k], dp[x] + 1);
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. int main() {
  90.     ios_base::sync_with_stdio(false);
  91.     cin.tie(nullptr);
  92.     Aho::nodes.reserve((int)1e6);
  93.     int n;
  94.     while(cin >> n) {
  95.         if(!n) break;
  96.         Aho::nodes.clear();
  97.         Aho::cnt = 0;
  98.         Aho::nodes.push_back(Aho::Node());
  99.         for(int i = 0; i < n; i++) {
  100.             dp[i] = 1;
  101.             cin >> v[i];
  102.         }
  103.         sort(v, v + n, [](string &a, string &b) {
  104.             return a.size() < b.size();
  105.         });
  106.         for(int i = 0; i < n; i++) {
  107.             Aho::add(v[i]);
  108.         }
  109.         Aho::build();
  110.         int ans = 1;
  111.         for(int i = 1; i < n; i++) {
  112.             Aho::match(v[i], i);
  113.             ans = max(ans, dp[i]);
  114.         }
  115.         cout << ans << "\n";
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement