JuliaMelkozerova

HW3A

Mar 20th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. bool cmp(const pair <char, int> &a, const pair <char, int> &b) {
  10.     if (a.second == b.second)
  11.         return a.first < b.first;
  12.     return a.second > b.second;
  13. }
  14.  
  15. int main() {
  16.     string S;
  17.     cin >> S;
  18.     vector <pair <char, int>> LC(1);
  19.    
  20.     LC[0].first = S[0];
  21.     LC[0].second = 1;
  22.    
  23.    
  24.     pair <char, int> lc_pair;
  25.     bool new_char = true;
  26.     for (int i = 1; i < S.length(); i++) {
  27.         new_char = true;
  28.        
  29.         for (int j = 0; j < LC.size(); j++) {
  30.             if (S[i] == LC[j].first) {
  31.                 LC[j].second++;
  32.                 new_char = false;
  33.             }    
  34.         }
  35.        
  36.         if (new_char) {
  37.             lc_pair.first = S[i];
  38.             lc_pair.second = 1;
  39.             LC.push_back(lc_pair);
  40.         }
  41.     }
  42.  
  43.     sort(&LC[0], &LC[LC.size()], cmp);
  44.  
  45.     for (auto lc : LC) {
  46.         cout << lc.first << ' ' << lc.second << endl;
  47.     }
  48.    
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment