Advertisement
LLIAMA3OB

LCS

Oct 4th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9.     int size;
  10.     Node * link;
  11.     map<char, Node*> next;
  12.  
  13.     Node() : size(0), link(nullptr) {}
  14. };
  15.  
  16. class SA{
  17.     vector<Node*> first;
  18.  
  19. public:
  20.     SA(string s) : first(1, new Node()) {
  21.         Node * last = first[0];
  22.         for (const char& c : s) {
  23.             Node * _new = new Node();
  24.             first.emplace_back(_new);
  25.             _new->size = last->size + 1;
  26.             Node * i = last;
  27.             while (i != nullptr && !i->next.count(c)) {
  28.                 i->next[c] = _new;
  29.                 i = i->link;
  30.             }
  31.             if (i == nullptr) {
  32.                 _new->link = first[0];
  33.             } else {
  34.                 Node * other = i->next[c];
  35.                 if (i->size + 1 == other->size) {
  36.                     _new->link = other;
  37.                 } else {
  38.                     Node * __new = new Node();
  39.                     first.emplace_back(__new);
  40.                     __new->size = i->size + 1;
  41.                     __new->next = other->next;
  42.                     __new->link = other->link;
  43.                     while (i != nullptr && i->next[c] == other) {
  44.                         i->next[c] = __new;
  45.                         i = i->link;
  46.                     }
  47.                     other->link = _new->link = __new;
  48.                 }
  49.             }
  50.             last = _new;
  51.         }
  52.     }
  53.  
  54.     string lcs(string other) {
  55.         Node * v = first[0];
  56.         unsigned long l = 0, best = 0, bestpos = 0;
  57.         for (unsigned long i = 0; i < other.length(); ++i) {
  58.             char c = other[i];
  59.             while (v != first[0] && !v->next.count(c)) {
  60.                 v = v->link;
  61.                 l = v->size;
  62.             }
  63.             if (v->next.count(c)) {
  64.                 v = v->next[c];
  65.                 ++l;
  66.             }
  67.             if (l > best) {
  68.                 best = l;
  69.                 bestpos = i;
  70.             }
  71.         }
  72.         return other.substr(bestpos - best + 1, best);
  73.     }
  74.  
  75.     ~SA() {
  76.         for (auto ptr : first) {
  77.             delete ptr;
  78.         }
  79.     }
  80. };
  81.  
  82. void solution() {
  83.     string s1, s2;
  84.     cin >> s1 >> s2;
  85.     SA sa(s1);
  86.     cout << sa.lcs(s2) << '\n';
  87. }
  88.  
  89. int main()
  90. {
  91.     iostream::sync_with_stdio(false);
  92.     solution();
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement