3 solutions

  • 0
    @ 2024-9-17 20:37:43
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <climits>
    #include <queue>
    #include <unordered_map>
    using namespace std;
    
    int main()
    {
        int a, b;
        string c;
        cin >> a >> b;
        cin >> c;
    
        unordered_map<char, int> mp; // 通过mp 存放字符次数
        string res;
        for (int i = 0; i < c.size(); i++)
        {
            mp[c[i]]++;
            if ((i + 1) % b == 0)
            {
                // 将字符和其出现次数存入 vector
                vector<pair<char, int>> freq(mp.begin(), mp.end());
    
                // 按出现次数降序排序,如果次数相同则按 ASCII 码值降序排序
                sort(freq.begin(), freq.end(), [](const pair<char, int> &a, const pair<char, int> &b)
                     {
                         if (a.second != b.second)
                             return a.second > b.second; // 根据出现次数降序
                         return a.first > b.first;       // 如果出现次数相同,根据 ASCII 码值降序
                     });
    
                // 输出前 a 个字符
                for (int i = 0; i < a && i < freq.size(); ++i)
                {
                    cout << freq[i].first;
                }
            }
        }
        cout << res;
    }
    
    • 0
      @ 2024-9-17 20:17:29
      #include <algorithm>
      #include <iostream>
      #include <iterator>
      #include <vector>
      #include <climits>
      #include <queue>
      #include <unordered_map>
      using namespace std;
      struct compare
      {
          bool operator()(const pair<char, int> &a, const pair<char, int> &b)
          {
              if (a.second != b.second)
                  return a.second < b.second; // 根据出现次数降序
              return a.first < b.first;       // 如果出现次数相同,根据 ASCII 码值升序
          }
      };
      int main()
      {
          int a, b;
          string c;
          cin >> a >> b;
          cin >> c;
      
          unordered_map<char, int> mp; // 通过mp 存放字符次数
          string res;
          for (int i = 0; i < c.size(); i++)
          {
              mp[c[i]]++;
              if ((i + 1) % b == 0)
              {
                  priority_queue<pair<char, int>, vector<pair<char, int>>, compare> q; // 按 ASCII 码值从大到小排列的大根堆;
                  for (auto &pair : mp)
                  {
                      q.push(pair);
                  }
                  priority_queue<pair<char, int>, vector<pair<char, int>>, compare> q1 = q;//复制一个用于弹出
                  for (int j = 0; j < a && !q1.empty(); j++)
                  {
                      res += q1.top().first;
                      q1.pop();
                  }
              }
          }
          cout << res;
      }
      
      • 0
        @ 2024-9-11 14:03:28
        from collections import Counter
        
        a, b, c = list(input().split())
        a = int(a)
        b = int(b)
        
        cnt = Counter()
        for i in range(0, len(c), b):
            temp = c[i:i+b]
            for x in temp:
                cnt[x] += 1
            ans = sorted(cnt.items(), key=lambda x:[-x[1],-ord(x[0])])
            for j in range(a):
                print(ans[j][0], end = "")
        

        每次都重新排序有点傻

        • 1

        Information

        ID
        17
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        3
        Tags
        # Submissions
        206
        Accepted
        64
        Uploaded By