ash_vs_ash

Untitled

Jul 2nd, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string s;
  4. void count_sort(vector<int> &p, vector<int> &c){
  5.     int n = p.size();
  6.     vector<int> cnt(n);
  7.     for (auto x: c){
  8.         cnt[x]++;
  9.     }
  10.     vector<int> p_new(n), pos(n);
  11.     pos[0] = 0;
  12.     for (int i=1; i<n; i++){
  13.         pos[i] = pos[i-1] + cnt[i-1];
  14.     }
  15.     for (auto x: p){
  16.         int i = c[x];
  17.         p_new[pos[i]] = x;
  18.         pos[i]++;
  19.     }
  20.     p = p_new;
  21. }
  22. vector<int> precalc(){
  23.     int n = s.length();
  24.     vector<int> p(n), c(n);
  25.     // p stores the at present starting index of the smallest processed string
  26.     // c stores the equivalence classes of the corresponding strings
  27.     {
  28.         // k = 0
  29.         vector<pair<char, int> > a(n);
  30.         for (int i=0; i<n; i++){
  31.             a[i] = {s[i], i};
  32.         }
  33.         sort(a.begin(), a.end());
  34.         for (int i=0; i<n; i++){
  35.             p[i] = a[i].second;
  36.         }
  37.         c[p[0]] = 0;
  38.         for (int i=1; i<n; i++){
  39.             if (a[i].first == a[i-1].first){
  40.                 c[p[i]] = c[p[i-1]];
  41.             }
  42.             else c[p[i]] = c[p[i-1]] + 1;
  43.         }
  44.     }
  45.     int k = 0;
  46.     while((1<<k) < n){
  47.        
  48.         for (int i=0; i<n; i++){
  49.             p[i] = (p[i] - (1<<k) + n)%n;
  50.         }
  51.         count_sort(p, c);
  52.         vector<int> c_new(n);
  53.         c_new[p[0]] = 0;
  54.         for (int i=1; i<n; i++){
  55.             pair<int, int> prev = {c[p[i-1]], c[(p[i-1] + (1<<k))%n]};
  56.             pair<int, int> now = {c[p[i]], c[(p[i] + (1<<k))%n]};
  57.             if (now == prev){
  58.                 c_new[p[i]] = c_new[p[i-1]];
  59.             }
  60.             else c_new[p[i]] = c_new[p[i-1]] + 1;
  61.         }
  62.         c = c_new;
  63.         k++;
  64.     }
  65.     return p;
  66. }
  67. // int compare(string s, string &t){
  68. //     // Check if t is a prefix of s
  69. //     int m = t.size();
  70. //     int n = s.size(); n--; //to remove the dollar from comparision
  71. //     int i = 0;
  72. //     while(i<m){
  73. //         if (i>=n)return 1;
  74. //         if (t[i] > s[i])return 1;
  75. //         if (t[i]<s[i])return -1;
  76. //         i++;
  77. //     }
  78. //     return 0;
  79. // }
  80. int search(vector<int> &p, string &t){
  81.     int n = s.size(), m = t.size();
  82.     int f = 0, l = n-1;
  83.     while(f<=l){
  84.         int mid = (f+l)/2;
  85.         // int x = compare(s.substr(p[mid], n-p[mid]), t);
  86.         string st = s.substr(p[mid], n-p[mid]-1);
  87.         if (st.length() > t.length()){
  88.             st = st.substr(0, t.length());
  89.         }
  90.         int x = t.compare(st);
  91.         if(x == 0){
  92.             return 1;
  93.         }
  94.         else if (x > 0){
  95.             f = mid + 1;
  96.         }
  97.         else l = mid-1;
  98.     }
  99.     return 0;
  100. }
  101. int main()
  102. {
  103.     // string s;
  104.     cin>>s;
  105.     s += '$';
  106.     int n = s.size();
  107.     vector<int> p = precalc();
  108.     // for (int i=0; i<n; i++){
  109.     //     cout<<p[i]<<" ";
  110.     // }
  111.     int q; cin>>q;
  112.     while(q--){
  113.         string t;
  114.         cin>>t;
  115.         int x = search(p, t);
  116.         if (x == 1)cout<<"Yes"<<endl;
  117.         else cout<<"No"<<endl;
  118.     }
  119.     // vector<pair<pair<int, int>, int>> a;
  120.     // for (int i=0; i<8; i++){
  121.     //     int l, r, k; cin>>l>>r>>k;
  122.     //     a.push_back({{l, r}, k});
  123.     // }
  124.     // radix_sort(a);
  125.     // for (int i=0; i<8; i++){
  126.     //     cout<<a[i].first.first<<" "<<a[i].first.second<<" "<<a[i].second<<endl;
  127.     // }
  128. }
Add Comment
Please, Sign In to add comment