nickel2halide

letter

Dec 14th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #include <vector>
  5. #include <string>
  6.  
  7. #include <algorithm>
  8.  
  9. #include <cctype>
  10. #include <cassert>
  11.  
  12. using namespace std;
  13.  
  14. struct sort_fst {
  15.     const string& s;
  16.     sort_fst(const string& s) : s(s) {}
  17.     bool operator()(int i, int j) { return s[i] < s[j]; }
  18. };
  19.  
  20. int main() {
  21.     ifstream in("letter.in");
  22.     ofstream out("letter.out");
  23.  
  24.     int N, M;
  25.     in >> N >> M;
  26.  
  27.     string a, b;
  28.  
  29.     for (int i = 0; i < N; ++i) {
  30.         char ch;
  31.         do { in.get(ch); } while (!isalpha(ch));
  32.         a += ch;
  33.     }
  34.  
  35.     for (int i = 0; i < M; ++i) {
  36.         char ch;
  37.         do { in.get(ch); } while (!isalpha(ch));
  38.         b += ch;
  39.     }
  40.  
  41.     vector<int> sa(N), sa2(N), rs(N), rk(N), rk2(N);
  42.     for (int i = 0; i < N; ++i) sa[i] = i;
  43.     sort(sa.begin(), sa.end(), sort_fst(a));
  44.     int r = 0;
  45.     for (int i = 1; i < N; ++i) {
  46.         if (a[sa[i]] != a[sa[i-1]])
  47.             rs[++r] = i;
  48.         rk[sa[i]] = r;
  49.     }
  50.  
  51.     for (int l = 1; l < N; l <<= 1) {
  52.         for (int i = N - l; i < N; ++i)
  53.             sa2[rs[rk[i]]++] = i;
  54.         for (int i = 0; i < N; ++i)
  55.             if (sa[i] >= l)
  56.                 sa2[rs[rk[sa[i] - l]]++] = sa[i] - l;
  57.         r = 0;
  58.         rs[sa[0] = sa2[0]] = 0;
  59.         rs[0] = 0;
  60.         for (int i = 1; i < N; ++i) {
  61.             if (rk[sa2[i]] != rk[sa2[i-1]] ||
  62.                 sa2[i]+l>=N || sa2[i-1]+l>=N ||
  63.                 rk[sa2[i]+l] != rk[sa2[i-1]+l])
  64.  
  65.                 rs[++r] = i;
  66.             rk2[sa[i] = sa2[i]] = r;
  67.         }
  68.  
  69.         for (int i = 0; i < N; ++i) rk[i] = rk2[i];
  70.     }
  71.  
  72.     int ans = 1;
  73.  
  74.     int lo = 0, hi = N, k = 0;
  75.     for (int i = 0; i < M; ++i) {
  76.         int l = lo, h = hi;
  77.         while (l < h) {
  78.             int m = (l + h) / 2;
  79.             if (a[sa[m] + k] < b[i])
  80.                 l = m + 1;
  81.             else
  82.                 h = m;
  83.         }
  84.  
  85.         if (l >= hi || a[sa[l] + k] > b[i]) {
  86.             ++ans;
  87.             lo = 0, hi = N, k = 0;
  88.             --i;
  89.             continue;
  90.         }
  91.  
  92.         lo = l;
  93.  
  94.         l = lo, h = hi;
  95.         while (l < h) {
  96.             int m = (l + h) / 2;
  97.             if (a[sa[m] + k] <= b[i])
  98.                 l = m + 1;
  99.             else
  100.                 h = m;
  101.         }
  102.         hi = l;
  103.  
  104.         ++k;
  105.     }
  106.  
  107.     out << ans << "\n";
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment