反片语 (using std::map) UVA - 156

题意:输出一些单词,这个单词不能被其他单词重排得到。在判断是不是可以重排得到的时候,不区分大小写。输出按字典序从小到大输出。

思路:把所有单词变成小写,sort 一遍,存到map。然后我再for一次,如果某个string 在 标准化之后的key ==1 那么满足条件,输出。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <sstream>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int INF=2147483647;
const int MAXN=1e5+10;
const ll mod=1e9+7;
using namespace std;
typedef long long ll;

vector <string> words;
map <string,int> cnt;

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(void)
{
    string s;
    while(cin >>s)
    {
        if(s[0]=='#')   break;
        words.push_back(s);
        string ans=repr(s);
        if(cnt.count(ans)==0)
            cnt[ans]=0;
        cnt[ans]++;
    }
    sort(words.begin(),words.end());
    //map的遍历和数组一模一样,和vector一样。set遍历用地址
    for(int i=0;i<words.size();i++)
    {
        if(cnt[repr(words[i])]==1)
            cout << words[i] <<endl;
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务