• 统计不同的单词(map应用)


    题目描述:

    输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有大写字母在所有的小写字母的前面)。

    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    #include<cctype>
    #include<map>
    using namespace std;
    map<string,int> cnt;
    vector<string> words;
    //对单词进行"标准化" 
    string repr(const string& s)
    {
        string cs=s;
        for(int i=0;i<cs.length();i++)
        {
            cs[i]=tolower(cs[i]);
        }
        sort(cs.begin(),cs.end());
        return cs;
    }
    int main()
    {
        string s;
        while(cin>>s)
        {
            if(s[0]=='#')    break;
            words.push_back(s);
            string cs=repr(s);
            if(!cnt.count(cs))    cnt[cs]=0;
            cnt[cs]++;
        }
        vector<string> ans;
        for(int i=0;i<words.size();i++)
        {
            if(cnt[repr(words[i])]==1)    ans.push_back(words[i]);//若想不到"标准化",则发挥不到map的效果 
        }
        sort(ans.begin(),ans.end());
        for(int i=0;i<ans.size();i++)
        {
            cout<<ans[i]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    c语言中重要函数
    python 类属性、对象属性
    windows下PIP安装模块编码错误解决
    python爬取百思不得姐视频
    ubuntu下刷新dns
    pycharm设置安装python第三方插件
    python将str转换成字典
    pyqt加载图片
    Python端口扫描器
    自己构造用于异步请求的JSON数据
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4927486.html
Copyright © 2020-2023  润新知