Advertisement
Ritam_C

Substring_Search

Sep 3rd, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void radix_sort(vector<int>& p, vector<int>& c) {
  5.     int n = p.size();
  6.     vector<int> pos(n), cnt(n, 0), f(n);
  7.     for(int i : c) {
  8.         cnt[i]++;
  9.     }
  10.     pos[0] = 0;
  11.     for(int i = 0; i < n; i++) {
  12.         pos[i] = pos[i-1]+cnt[i-1];
  13.     }
  14.     for(int x : p) {
  15.         f[pos[c[x]]] = x;
  16.         pos[c[x]]++;
  17.     }
  18.     p = f;
  19. }
  20.  
  21. vector<int> generate(string& t) {
  22.     int n = t.length();
  23.     vector<int> p(n), c(n);
  24.     vector<pair<char, int>> a(n);
  25.     for(int i = 0; i < n; i++) {
  26.         a[i] = {t[i], i};
  27.     }
  28.     sort(a.begin(), a.end());
  29.     for(int i = 0; i < n; i++) {
  30.         p[i] = a[i].second;
  31.     }
  32.     c[p[0]] = 0;
  33.     for(int i = 1; i < n; i++) {
  34.         c[p[i]] = (a[i].first == a[i-1].first) ? c[p[i-1]] : c[p[i-1]]+1;
  35.     }
  36.     int k = 0;
  37.     while((1 << k) < n) {
  38.         for(int i = 0; i < n; i++) {
  39.             p[i] = (p[i]-(1 << k)+n)%n;
  40.         }
  41.         radix_sort(p, c);
  42.         vector<int> c_new(n);
  43.         c_new[p[0]] = 0;
  44.         for(int i = 1; i < n; i++) {
  45.             pair<int, int> prev = {c[p[i-1]], c[(p[i-1]+(1 << k))%n]};
  46.             pair<int, int> curr = {c[p[i]], c[(p[i]+(1 << k))%n]};
  47.             c_new[p[i]] = (prev == curr) ? c_new[p[i-1]] : c_new[p[i-1]]+1;
  48.         }
  49.         c = c_new;
  50.         k++;
  51.     }
  52.     return p;
  53. }
  54.  
  55. string search(string& st, string& t, vector<int>& p) {
  56.     int s = 0, e = p.size()-1;
  57.     string t1, t2, t3;
  58.     while(e >= s) {
  59.         int m = s+(e-s)/2;
  60.         t1 = (t.length()-p[m] >= st.length()) ? t.substr(p[m], st.length()) : t.substr(p[m]);
  61.         t2 = (t.length()-p[m] >= st.length()) ? t.substr(p[s], st.length()) : t.substr(p[s]);
  62.         t3 = (t.length()-p[m] >= st.length()) ? t.substr(p[e], st.length()) : t.substr(p[e]);
  63.         if(t1 == st) {
  64.             return "Yes\n";
  65.         } else if(t1 < st && st <= t3) {
  66.             s = m+1;
  67.         } else if(t2 <= st && st < t1) {
  68.             e = m-1;
  69.         } else {
  70.             return "No\n";
  71.         }
  72.     }
  73.     return "No\n";
  74. }
  75.  
  76. int main() {
  77.     ios_base::sync_with_stdio(false);
  78.     cin.tie(NULL); cout.tie(NULL);
  79.     string t; int q; cin >> t >> q;
  80.     t += '$';
  81.     vector<int> p = generate(t);
  82.     while(q--) {
  83.         string s; cin >> s;
  84.         cout << search(s, t, p);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement