Advertisement
tepyotin2

Untitled

Oct 4th, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Word{
  6.     int ogord;
  7.     string va;
  8.     Word(){};
  9.     Word(int ogord1, string va1){
  10.         ogord = ogord1;
  11.         va = va1;
  12.     };
  13. };
  14.  
  15. int W, N;
  16. Word pos[1000000];
  17.  
  18. int cal_search(int k, string q){
  19.     int l = 0;
  20.     int r = W-1;
  21.     int omid = 0;
  22.     int okay = -1; //Okay declared outside of loop.
  23.     // If the value is okay, the okay will equal the
  24.     // mid or the position of the string.
  25.     while(l<=r){
  26.         int mid = (l+r)/2; //mid has to be inside of the loop,
  27.         // because the answer should be the value of okay, not the mid.
  28.         if(omid == mid){
  29.             break;
  30.         }
  31.         string posval = pos[mid].va;
  32.         if(q == posval.substr(0, q.size())){
  33.             okay = mid;
  34.         }
  35.         if(q == posval){ //If q (the value we are testing) is the same as the pos value,
  36.         // then we do not have to loop any further.
  37.             break;
  38.         }else if(q < posval){ //If q(the value we are testing) is less than the pos value,
  39.         // it means that the current mid value is more than the q, which means that we have
  40.         // to move the r (the largest value) to the mid to make the range include smaller numbers.
  41.             r = mid;
  42.         }else{ //If q(the value we are testing) is more than the pos value,
  43.         // it means that the current mid value is less than the q, which means that we have
  44.         // to move the l (the smalles value) to the mid to make the range include bigger numbers.
  45.             l = mid;
  46.         }
  47.         omid = mid; //omid should be declared at the end of the loop.
  48.         // The old-mid will be the old value of the mid when being compared
  49.         // to the current mid in the next loop.
  50.     }
  51.     int ans = -1;
  52.     if(okay>=0){
  53.         Word b = pos[okay+k-1];
  54.         if(q == b.va.substr(0, q.size())){
  55.             ans = b.ogord;
  56.         }
  57.     }
  58.     // if(q == pos[ans].va.substr(0, q.size())) ans = okay + k-1;
  59.     return ans;
  60. }
  61.  
  62. int main(){
  63.     //ios_base::sync_with_stdio(0), cin.tie(0);
  64.     //freopen(  "auto/1.in", "r", stdin);
  65.     freopen("auto.in", "r", stdin);
  66.     freopen("auto.out", "w", stdout);
  67.     cin >> W >> N;
  68.     string va;
  69.     for(int i=0; i<W; i++){
  70.         cin >> va;
  71.         pos[i] = Word(i+1, va);
  72.     }
  73.     sort(pos, pos+W
  74.     , [](const Word& a, const Word& b){
  75.         return a.va < b.va;
  76.         }
  77.     );
  78.     // for(int i=0; i<W; i++){
  79.     //     Word a = pos[i];
  80.     //     cout << a.va << ", pos: " << a.ogord << '\n';
  81.     // }
  82.     for(int i=0; i<N; i++){
  83.         int vaa;
  84.         string vab;
  85.         cin >> vaa >> vab;
  86.         int ans = cal_search(vaa, vab);
  87.         cout << ans << '\n';
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement