Advertisement
mickypinata

USACO-T043: Contact

Dec 21st, 2021
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. /*
  2. ID: mickyta1
  3. TASK: contact
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define f first
  11. #define s second
  12. typedef pair<int, int> pii;
  13.  
  14. const int L = 2e5 + 5;
  15.  
  16. map<int, int> mp;
  17. vector<pii> fq;
  18. vector<int> ans;
  19. int len;
  20. char str[L];
  21.  
  22. bool comp(const pii &lhs, const pii &rhs){
  23.     if(lhs.s != rhs.s){
  24.         return lhs.s > rhs.s;
  25.     }
  26.     return lhs.f < rhs.f;
  27. }
  28.  
  29. int charToInt(char c){
  30.     return c - '0' + 1;
  31. }
  32.  
  33. void base3ToBit(int x){
  34.     if(x == 0){
  35.         return;
  36.     }
  37.     base3ToBit(x / 3);
  38.     cout << x % 3 - 1;
  39. }
  40.  
  41. void addWordLength(int l){
  42.     int hsh = 0;
  43.     int base = 1;
  44.     for(int i = 1; i <= l; ++i){
  45.         hsh *= 3;
  46.         hsh += charToInt(str[i]);
  47.         if(i != 1){
  48.             base *= 3;
  49.         }
  50.     }
  51.     ++mp[hsh];
  52.     for(int i = l + 1; i <= len; ++i){
  53.         hsh -= base * charToInt(str[i - l]);
  54.         hsh *= 3;
  55.         hsh += charToInt(str[i]);
  56.         ++mp[hsh];
  57.     }
  58. }
  59.  
  60. void printAns(){
  61.     int upb = ((int)ans.size() + 5) / 6;
  62.     for(int i = 0; i < upb; ++i){
  63.         int j;
  64.         for(j = 0; j < 5 && 6 * i + j < (int)ans.size() - 1; ++j){
  65.             base3ToBit(ans[6 * i + j]);
  66.             cout << ' ';
  67.         }
  68.         base3ToBit(ans[6 * i + j]);
  69.         cout << '\n';
  70.     }
  71. }
  72.  
  73. int main(){
  74.     freopen("contact.in", "r", stdin);
  75.     freopen("contact.out", "w", stdout);
  76.  
  77.     int lwb, upb, limRnk;
  78.     scanf("%d%d%d", &lwb, &upb, &limRnk);
  79.     len = 1;
  80.     while(scanf(" %s", str + len) != EOF){
  81.         len += strlen(str + len);
  82.     }
  83.     --len;
  84.     upb = min(upb, len);
  85.     for(int i = lwb; i <= upb; ++i){
  86.         addWordLength(i);
  87.     }
  88.     for(pii p : mp){
  89.         fq.push_back(p);
  90.     }
  91.     sort(fq.begin(), fq.end(), comp);
  92.     int rnk = 0;
  93.     int lst = 0;
  94.     for(pii &p : fq){
  95.         if(rnk > limRnk){
  96.             break;
  97.         }
  98.         int x = p.f;
  99.         int f = p.s;
  100.         if(f != lst){
  101.             if(lst != 0){
  102.                 cout << lst << '\n';
  103.                 printAns();
  104.             }
  105.             lst = f;
  106.             ++rnk;
  107.             ans.clear();
  108.         }
  109.         ans.push_back(x);
  110.     }
  111.     if(rnk <= limRnk){
  112.         cout << lst << '\n';
  113.         printAns();
  114.     }
  115.  
  116.     fclose(stdin);
  117.     fclose(stdout);
  118.     return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement