Advertisement
allia

поиск среди слов

Oct 20th, 2020 (edited)
2,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. int poisk (string *arr, string znach, int left, int right)
  8. {
  9.  int i = 0;
  10. while (left < right)
  11. {
  12.   i = (left + right) / 2;
  13.   if (arr[i] < znach)
  14.     left = i+1;
  15.   else right = i;
  16. }
  17.  
  18. if (arr[right] == znach)
  19. return right+1;
  20. return -1;
  21. }
  22.  
  23. int main()
  24. {
  25.   int n = 0, m = 0;
  26.   scanf("%d", &n);
  27.  
  28.   string buffer;
  29.  
  30.   string *arr = new string[n];
  31.  
  32.   for (int i = 0; i < n; i++)
  33.    cin >> arr[i];
  34.  
  35. scanf("%d", &m);
  36.  
  37. for (int i = 0; i < m; i++)
  38. {
  39.   cin >> buffer;
  40.   cout << poisk (arr, buffer, 0, n-1) << endl;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement