Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. //經過ㄌㄅㄑ的指導發現似乎是hash寫爛後決定再試最後一次OuO
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6. #define endl '\n'
  7. #define int long long
  8. int _hash(string str) {
  9.     int result=0, num;
  10.     result += (str[0] - 'A');
  11.    
  12.     for(int i=1; i<str.length(); i++) {
  13.         result *= 10;
  14.         result += (str[i] - '0');
  15.     }
  16.  
  17.     return result;
  18. }
  19. signed main() {
  20.     ios_base::sync_with_stdio(0);
  21.     cin.tie(0);
  22.     cout.tie(0);
  23.     int n, m, target;
  24.     string input;
  25.     cin >> n >> m;
  26.     int data[n];
  27.     for(int i=0; i<n; i++) {
  28.         cin >> input;
  29.         data[i] = _hash(input);
  30.     }
  31.     sort(data, data+n);
  32.     for(int i = 0; i < m; i++) {
  33.         bool ans = 0;
  34.         cin >> input;
  35.         target = _hash(input);
  36.         int L = 0, R = n - 1;
  37.         int M = (L+R)/2;
  38.         while (L <= R) {
  39.             M = (L + R) >> 1;
  40.             if(data[M] == target) {
  41.                 ans = 1;
  42.                 break;
  43.             }
  44.             else if(data[M] > target) {
  45.                 R = M - 1;
  46.             }
  47.             else {
  48.                 L = M + 1;
  49.             }
  50.         }
  51.         if(ans) cout << "Yes" << endl;
  52.         else cout << "No" << endl;
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement