略懂c语言 2022-05-12 14:02 采纳率: 92.6%
浏览 81
已结题

老是报错,Python通过不了,需要c

题目如下:可以测试
http://acm.usx.edu.cn/aspnet/Question.aspx?qid=1263
代码


import re
 
T = int(input(''))
content = []
for i in range(T):
    txt = ''
    n = int(input(f''))
    for j in range(n):
        a = input(f'')
        txt += a
    content.append(txt)
 
for k in content:
    word = re.findall(r'\w+',k)
    word_dict = {i:word.count(i) for i in word}
    word_list = sorted(list(word_dict.items()), key=lambda x:x[0])
    word_list.sort(key=lambda x:x[1], reverse=True)
    for i,j in word_list:
        print(i,j)
  • 写回答

2条回答 默认 最新

  • 丨秋水丨 2022-05-12 19:11
    关注
    #include<iostream>
    #include<map>
    #include<string>
    #include<cctype>
    #include<vector>
    #include<algorithm>
    typedef std::pair<std::string, int> WordStat;
    typedef std::map<std::string, int>::iterator WordIterator;
    bool cmpfunc(const WordStat& l, const WordStat& r) {
        return l.second == r.second ? l.first < r.first : l.second > r.second;
    }
    int main()
    {
        int testNum;
        std::cin >> testNum;
        while (testNum--)
        {
            int lineNum;
            std::cin >> lineNum;
            std::cin.ignore();
            std::string line;
            std::map<std::string, int> wordMap;
            while (lineNum--)
            {
                std::getline(std::cin, line);
    
                std::string word = "";
                bool lastCharIsAlpha = 0;
                int i = 0;
                while (i < line.size())
                {
                    while (i < line.size() && !isalpha(line[i]))++i;
                    
                    word = "";
                    while (i < line.size() && isalpha(line[i]))
                    {
                        word += line[i];
                        ++i;
                    }
                    if (!word.empty())
                    {
                        wordMap[word]++;
                    }
                }
            }
            std::vector<WordStat>words;
            for (WordIterator iter = wordMap.begin(), iterEnd = wordMap.end(); iter != iterEnd; ++iter)
            {
                words.push_back(std::make_pair(iter->first, iter->second));
            }
            std::sort(words.begin(), words.end(), cmpfunc);
            for (int i = 0; i < words.size(); ++i)
            {
                std::cout << words[i].first << " " << words[i].second << std::endl;
            }
        }
        return 0;
    }
    

    已测试通过

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 修改了问题 5月12日
  • 赞助了问题酬金5元 5月12日
  • 展开全部

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号