Advertisement
palmerstone

Babelfish

Oct 27th, 2011
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int i = 0, flag = 0;
  11. struct dictionary
  12. {
  13. char entry[12];
  14. char foreign[12];
  15. };
  16. struct dictionary ar[100010];
  17.  
  18. int compare(const void *a, const void *b)
  19. {
  20. char str[30], str2[30];
  21. strcpy(str, ((dictionary *)a)->foreign);
  22. strcpy(str2, ((dictionary *)b)->foreign);
  23.  
  24. if (strcmp(str, str2) > 0) return 1;
  25. return -1;
  26. }
  27.  
  28. int binsearch(char str[15])
  29. {
  30. int j, l, a, b, x = 0, y, z;
  31. y = -1, a = 0, b = i;
  32. for (; ;)
  33. {
  34. y = (a + b) / 2;
  35. if (y == x) return x;
  36. x = y;
  37. z = strcmp(ar[x].foreign, str);
  38. if (z == 0) {flag = 1; return x;}
  39. else if (z < 0) a = x;
  40. else b = x;
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. int t, j, k, l, a, b, c, d, x, y, z;
  47. char str[100];
  48.  
  49. for (; ;)
  50. {
  51. gets(str);
  52. if (str[0] == 0) break;
  53. for (j = 0; str[j] != 32; j++) ar[i].entry[j] = str[j];
  54. ar[i].entry[j] = 0, j++, l = 0;
  55. for (; str[j] != 0; j++) ar[i].foreign[l++] = str[j];
  56. ar[i++].foreign[l] = 0;
  57. }
  58. qsort(ar, i, sizeof(dictionary), compare);
  59.  
  60. while (gets(str))
  61. {
  62. flag = 0;
  63. z = binsearch(str);
  64. if (flag == 1) puts(ar[z].entry);
  65. else puts("eh");
  66. }
  67. return 0;
  68. }
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement