Advertisement
ipilot

Lab C++

Oct 29th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #pragma warning(disable: 4996)
  2. #pragma comment(linker,"/STACK:64000000")
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. #define pb push_back
  12. #define sz(a) (int)a.size()
  13. #define all(a) a.begin(), a.end()
  14.  
  15. string s;
  16. char ss[100];
  17.  
  18. void fullsearch()
  19. {
  20.     freopen("result_fullsearch.txt", "w", stdout);
  21.     int i;
  22.     vector <string> data;
  23.     while (sz(data) < 15)
  24.     {
  25.         i=0;
  26.         s=gets(ss);
  27.         while ((i < sz(data)) && (data[i] != s)) i++;
  28.         if (i == sz(data)) data.pb(s);
  29.         else i++;
  30.         cout << "For string "+s+" " << i << " comparisons.\n";
  31.     }
  32. }
  33.  
  34. void binsearch()
  35. {
  36.     freopen("result_binsearch.txt", "w", stdout);
  37.     int i, l, r, x;
  38.     vector <string> data;
  39.     while (sz(data) < 15)
  40.     {
  41.         s=gets(ss);
  42.         i=l=0;
  43.         r=sz(data)-1;
  44.         while (l < r)
  45.         {
  46.             i++;
  47.             x=(l+r)/2;
  48.             if (data[x] > s) r=x-1;
  49.             else
  50.                 if (data[x] < s) l=x+1;
  51.                 else
  52.                     l=r=x;
  53.         }
  54.         if (sz(data) > 0) i++;
  55.         if ((r < 0) || (l >= sz(data)) || (data[l] != s))
  56.         {
  57.             data.pb(s);
  58.             sort(all(data));
  59.         }
  60.         cout << "For string "+s+" " << i << " comparisons.\n";
  61.     }
  62. }
  63.  
  64. void hashing()
  65. {
  66.     freopen("result_hashing.txt", "w", stdout);
  67.     int i, col=0, k, hash;
  68.     bool f=false;
  69.     vector <string> data(15, "");
  70.     bool used[15];
  71.     memset(used, 0, sizeof(used));
  72.     while (col < 15)
  73.     {
  74.         s=gets(ss);
  75.         i=hash=0;
  76.         for(int j=0; j < sz(s); j++)
  77.             hash=(hash + (1+(unsigned char)s[j]))%15;
  78.         if (!used[hash])
  79.         {
  80.             used[hash]=true;
  81.             data[hash]=s;
  82.             col++;
  83.         }
  84.         else
  85.         {
  86.             i++;
  87.             k=1;
  88.             f=false;
  89.             if (data[hash] != s)
  90.             {
  91.                 while (!f)
  92.                 {
  93.                     if ((hash-k) >= 0)
  94.                     {
  95.                         if (!used[hash-k])
  96.                         {
  97.                             used[hash-k]=true;
  98.                             data[hash-k]=s;
  99.                             f=true;
  100.                             col++;
  101.                         }
  102.                         else
  103.                         {
  104.                             i++;
  105.                             if (data[hash-k] == s) f=true;
  106.                         }
  107.                     }
  108.                     if (!f)
  109.                     {
  110.                         if ((hash+k) < 15)
  111.                         {
  112.                             if (!used[hash+k])
  113.                             {
  114.                                 used[hash+k]=true;
  115.                                 data[hash+k]=s;
  116.                                 f=true;
  117.                                 col++;
  118.                             }
  119.                             else
  120.                             {
  121.                                 i++;
  122.                                 if (data[hash+k] == s) f=true;
  123.                             }
  124.                         }
  125.                     }
  126.                     k++;
  127.                 }
  128.             }
  129.         }
  130.         cout << "For string "+s+" " << i << " comparisons.\n";
  131.     }  
  132. }
  133.  
  134. int main()
  135. {
  136.     int k;
  137.     cerr << "Select the method of data processing:\n" << "1. Fullsearch\n" << "2. Binary search\n" << "3. Hashing\n";
  138.     cin >> k;
  139.     freopen("test.txt", "r", stdin);
  140.     switch (k)
  141.     {
  142.         case 1: fullsearch(); break;
  143.         case 2: binsearch(); break;
  144.         case 3: hashing(); break;
  145.     }
  146.     cerr << "\nAll data were processed.\n";
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement