Advertisement
shek_shek

Сортировка подсчетом

Sep 15th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stack>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string.h>
  7. #include <string>
  8. #include <set>
  9. #include <memory.h>
  10. #include <vector>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #include <ctime>
  15. #include <cassert>
  16.  
  17. #define forn(i, n) for (int i = 0; i < (n); i++)
  18.  
  19. using namespace std;
  20. const int N = 1000 * 100;
  21. const int p = 31;
  22.  
  23. long long myHash(const string &s) {
  24.     long long res = 0, p_pow = 1;
  25.     for (int i = s.size() - 1; i >= 0; i--) {
  26.         res += (s[i] - 'a' + 1) * p_pow;
  27.         p_pow *= p;
  28.     }
  29.     assert(res < N);
  30.     return res;
  31. }
  32.  
  33. int main() {
  34.     freopen("output.txt", "w", stdout);
  35.     srand(time(NULL));
  36.     int cnt[N];
  37.     memset(cnt, 0, sizeof(cnt));
  38.     map <long long, string> myHashs;
  39.     forn (i, N) {
  40.         string s = "";
  41.         forn (j, 3)
  42.             s += 'a' + (rand() % 26);
  43.         myHashs[myHash(s)] = s;
  44.         cnt[myHash(s)]++;
  45.     }
  46.     forn (i, N)
  47.         if (cnt[i] != 0)
  48.             while (cnt[i]--)
  49.                 cout << myHashs[i] << endl;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement