Advertisement
Guest User

asd

a guest
Nov 6th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>
  2. #include <climits>
  3. #include <vector>
  4. #include <queue>
  5. #include <map>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11. typedef pair<int,int> pii;
  12. typedef vector<int> vi;
  13. typedef vector<vi> vvi;
  14.  
  15. #ifdef EVAL
  16. #include <fstream>
  17. #define cin fin
  18. #define cout fout
  19. ifstream fin("input.txt");
  20. ofstream fout("output.txt");
  21. #endif
  22.  
  23. void print_spaces(int n){
  24.     while(n--) cout << "   ";
  25. }
  26.  
  27. struct nodo : map<char, nodo*>{
  28.     char *s = 0;
  29.     int len = 0;
  30.     bool is_ending(){
  31.         return size() == 0;
  32.     }
  33.     void debug(string msg){
  34.         cout << msg << " : ";
  35.         for(int i = 0; i < len; i++) cout << s[i];
  36.         cout << '\n';
  37.     }
  38.     void print(int level){
  39.         print_spaces(level);
  40.         for(int i = 0; i < len; i++) cout << s[i];
  41.         cout << '\n';
  42.         for(auto &n : *this) n.second->print(level+1);
  43.     }
  44. };
  45.  
  46.  
  47. struct compact_trie{
  48.     nodo root;
  49.     void insert(char *s, int len){
  50.         int l = 0;
  51.         nodo *prev;
  52.         nodo *tmp = &root;
  53.         while(l < len){
  54.             //tmp->debug("sono nel nodo");
  55.             int i = 0;
  56.  
  57.             for(; i < tmp->len && l < len; i++, l++)
  58.                 if(s[l] != tmp->s[i])
  59.                     break;
  60.  
  61.             //cout << "match di " << i << '\n';
  62.  
  63.             if(l == len && i == tmp->len) return;
  64.             if(i < tmp->len){
  65.                 nodo *red = new nodo();
  66.                 red->len = i;
  67.                 red->s = tmp->s;
  68.                 //prev->debug("prev si presenta");
  69.                 (*prev)[tmp->s[0]] = red;
  70.                 (*red)[tmp->s[i]] = tmp;
  71.                 //red->debug("creo red");
  72.                 tmp->len -= i;
  73.                 tmp->s += i;
  74.                 //tmp->debug("ora tmp diventa");
  75.                 //cout << "splittiamo\n";
  76.                 tmp = red;
  77.             }
  78.  
  79.             if(!tmp->count(s[l])){
  80.                 // cout << "creo un nuovo figlio di troia\n";
  81.                 // cout << "link in " << s[l] << '\n';
  82.                 (*tmp)[s[l]] = new nodo();
  83.                 (*tmp)[s[l]]->s = &s[l];
  84.                 (*tmp)[s[l]]->len = len - l;               
  85.             }
  86.             prev = tmp;
  87.             tmp = (*tmp)[s[l]];
  88.         }
  89.     }
  90.     void print(){
  91.         root.print(0);
  92.     }
  93. };
  94.  
  95. //dumb edition
  96. struct sufftree{
  97.     compact_trie dumb;
  98.     sufftree (string &s){
  99.         s += '\0';
  100.         for(int i = 0; i < s.size()-1; i++)
  101.             dumb.insert(&s[i], s.size()-i);
  102.     }
  103.     void print(){
  104.         dumb.print();
  105.     }
  106.     int lcp(string &s, int l){
  107.         int r = l;
  108.         nodo *tmp = &dumb.root;
  109.  
  110.         while(1){
  111.             int i;
  112.             for(i = 0; i < tmp->len && r < s.size() && tmp->s[i] == s[r]; i++, r++);
  113.            
  114.             if(r == s.size() || i != tmp->len || !tmp->count(s[r]))
  115.                 break;
  116.  
  117.             tmp = (*tmp)[s[r]];
  118.         }
  119.  
  120.         return r - l;
  121.     }
  122. };
  123.  
  124.  
  125.  
  126. int main(){
  127.     cin.sync_with_stdio(false);
  128.     cin.tie(NULL);
  129.     string s, t;
  130.     getline(cin, s), getline(cin, t);
  131.     sufftree st(t);
  132.     //st.print();
  133.     int done = 0;
  134.     int ans = 0;
  135.     while(done != s.size())
  136.         done += st.lcp(s, done), ans++;
  137.     cout << ans;
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement