首页 > 其他 > 详细

UVa 156 <map>启蒙

时间:2018-08-19 21:38:52      阅读:152      评论:0      收藏:0      [点我收藏+]

map函数的启蒙篇,如何实现字符串对于数值的映射达到查重效果(map中同一个元素只能存在一次)。
map函数提供了find()以及count()。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

vector<string>  word;
map <string, int> map1;

string fun(string x)
{
    string temp = x;
    for(int i = 0 ; i < (int)temp.length() ; i++)
        temp[i] = tolower(temp[i]);
    sort(temp.begin(), temp.end());
    return temp;
}

int main()
{
    string s;
    while(cin >> s)
    {
        if(s[0] == '#' )
            break;

        word.push_back(s);
        string low = fun(s);
        if(!map1.count(low))
            map1[low] = 0;
        map1[low]++;
    }

    vector<string> ans;
    for(int i = 0 ; i < (int)word.size(); i++)
        if(map1[fun ( word[i] ) ] == 1)
            ans.push_back(word[i]);

    sort(ans.begin(), ans.end());
    for(int i = 0 ; i < (int)ans.size(); i++)
        cout << ans[i] << endl;
    return 0;
}

UVa 156 <map>启蒙

原文:https://www.cnblogs.com/ronnielee/p/9502600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!