Advertisement
Manioc

LCS2

Apr 23rd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100000
  3.  
  4. using namespace std;
  5.  
  6. string txt, palavra;
  7. queue<string> q;
  8. struct node{
  9.     int future[27];
  10.     int past, len;
  11. } sa[2*MAX];
  12.  
  13. int sz, last;
  14.  
  15. void init(){
  16.     sz = 1;
  17.     last = 0;
  18.     sa[0].past = -1;
  19.     sa[0].len = 0;
  20. }
  21.  
  22. void extends(int c){
  23.     int cur = sz++;
  24.     sa[cur].len = sa[last].len + 1;
  25.    
  26.     for(; last != -1 && !sa[last].future[c]; last = sa[last].past) sa[last].future[c] = cur;
  27.  
  28.     if(last == -1) sa[cur].past = 0;
  29.     else{
  30.         int q = sa[last].future[c];
  31.         if(sa[q].len == sa[last].len + 1) sa[cur].past = q;
  32.         else{
  33.             int clone = sz++;
  34.             sa[clone].len = sa[last].len + 1;
  35.             sa[clone].past = sa[q].past;
  36.             memcpy(sa[clone].future, sa[q].future, sizeof sa[q].future);
  37.             for(; last != -1 && !sa[last].future[c] ==  q; last = sa[last].past) sa[last].future[c] = clone;
  38.             sa[cur].past = sa[q].past = clone;
  39.         }
  40.     }
  41.     last = cur;
  42. }
  43.  
  44. void clear(int size){
  45.     for(int i = 0; i < palavra.size(); i++) {
  46.         memset(sa[i].future, 0, sizeof sa[i].future);
  47.         sa[i].past = 0;
  48.         sa[i].len = 0;
  49.     }
  50.     init();
  51. }
  52. void lcs(){
  53.     string nova = "";
  54.     while(q.size() > 1){
  55.         txt = q.front();
  56.         q.pop();
  57.         palavra = q.front();
  58.         q.pop();
  59.         clear(palavra.size());
  60.  
  61.         for(int i = 0; i < txt.size(); i++) extends(txt[i]-'a');
  62.         int l = 0, best = 0, v = 0;
  63.         nova = "";
  64.         for(int i = 0; i < palavra.size(); i++){
  65.             bool flag = false;
  66.             while(v && !sa[v].future[palavra[i]-'a']){
  67.                 v = sa[v].past;
  68.                 l = sa[v].len;
  69.                 if(!flag){
  70.                     nova += "{";
  71.                     flag = true;
  72.                 }
  73.             }
  74.  
  75.  
  76.             if(sa[v].future[palavra[i]-'a']){
  77.                 nova += palavra[i];
  78.                 //cout << palavra[i] << endl;
  79.                 v = sa[v].future[palavra[i]-'a'];
  80.                 l++;
  81.             }else {
  82.                 if(nova[nova.size()-1] != '{') {
  83.                     nova += "{";
  84.                     //cout <<'{' << endl;
  85.                 }
  86.             }
  87.  
  88.         }
  89.         q.push(nova);
  90.         cout << nova << endl;
  91.     }
  92.     cout << endl;
  93.     cout << nova << endl;
  94. }
  95. int main(){
  96.     int num; cin >> num;
  97.     while(num--){
  98.         cin >> txt;
  99.         q.push(txt);
  100.     }
  101.     cout << endl;
  102.     lcs();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement