Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable: 4996)
- #pragma comment(linker,"/STACK:64000000")
- #include <iostream>
- #include <stdio.h>
- #include <algorithm>
- #include <string>
- #include <vector>
- using namespace std;
- #define pb push_back
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- string s;
- char ss[100];
- void fullsearch()
- {
- freopen("result_fullsearch.txt", "w", stdout);
- int i;
- vector <string> data;
- while (sz(data) < 15)
- {
- i=0;
- s=gets(ss);
- while ((i < sz(data)) && (data[i] != s)) i++;
- if (i == sz(data)) data.pb(s);
- else i++;
- cout << "For string "+s+" " << i << " comparisons.\n";
- }
- }
- void binsearch()
- {
- freopen("result_binsearch.txt", "w", stdout);
- int i, l, r, x;
- vector <string> data;
- while (sz(data) < 15)
- {
- s=gets(ss);
- i=l=0;
- r=sz(data)-1;
- while (l < r)
- {
- i++;
- x=(l+r)/2;
- if (data[x] > s) r=x-1;
- else
- if (data[x] < s) l=x+1;
- else
- l=r=x;
- }
- if (sz(data) > 0) i++;
- if ((r < 0) || (l >= sz(data)) || (data[l] != s))
- {
- data.pb(s);
- sort(all(data));
- }
- cout << "For string "+s+" " << i << " comparisons.\n";
- }
- }
- void hashing()
- {
- freopen("result_hashing.txt", "w", stdout);
- int i, col=0, k, hash;
- bool f=false;
- vector <string> data(15, "");
- bool used[15];
- memset(used, 0, sizeof(used));
- while (col < 15)
- {
- s=gets(ss);
- i=hash=0;
- for(int j=0; j < sz(s); j++)
- hash=(hash + (1+(unsigned char)s[j]))%15;
- if (!used[hash])
- {
- used[hash]=true;
- data[hash]=s;
- col++;
- }
- else
- {
- i++;
- k=1;
- f=false;
- if (data[hash] != s)
- {
- while (!f)
- {
- if ((hash-k) >= 0)
- {
- if (!used[hash-k])
- {
- used[hash-k]=true;
- data[hash-k]=s;
- f=true;
- col++;
- }
- else
- {
- i++;
- if (data[hash-k] == s) f=true;
- }
- }
- if (!f)
- {
- if ((hash+k) < 15)
- {
- if (!used[hash+k])
- {
- used[hash+k]=true;
- data[hash+k]=s;
- f=true;
- col++;
- }
- else
- {
- i++;
- if (data[hash+k] == s) f=true;
- }
- }
- }
- k++;
- }
- }
- }
- cout << "For string "+s+" " << i << " comparisons.\n";
- }
- }
- int main()
- {
- int k;
- cerr << "Select the method of data processing:\n" << "1. Fullsearch\n" << "2. Binary search\n" << "3. Hashing\n";
- cin >> k;
- freopen("test.txt", "r", stdin);
- switch (k)
- {
- case 1: fullsearch(); break;
- case 2: binsearch(); break;
- case 3: hashing(); break;
- }
- cerr << "\nAll data were processed.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement