首页 > 其他 > 详细

uva156反片语

时间:2015-02-06 21:56:54      阅读:408      评论:0      收藏:0      [点我收藏+]

背景:学习stl过程中遇到的简单题,但我不会。

思路:将单词标准化,然后就可以运用映射map了。

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
map<string,int> cnt;
vector<string> words;
string repr(const string& s)
{
    string ans=s;
    for(int i=0;i<ans.length();i++)
        ans[i]=tolower(ans[i]);
    sort(ans.begin(),ans.end());
    return ans;
}
int main()
{
    string s;
    while(cin>>s)
    {
        if(s[0]=='#') break;
        words.push_back(s);
        string r=repr(s);
        if(!cnt.count(r)) cnt[r]=0;//标记该单词是否已经出现过
        cnt[r]++;
    }
    vector<string> ans;
    for(int i=0;i<words.size();i++)
        if(cnt[repr(words[i])]==1) ans.push_back(words[i]);
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++)
        cout <<ans[i]<< endl;
    return 0;
}

uva156反片语

原文:http://blog.csdn.net/qiweigo/article/details/43566479

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