uva 156(Ananagrams)

题目大意:

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

题目分析:

  需要把输入中的每个单词标准化,从而判断是否出现过;

怎么标准化呢, 将单词大写变成小写,按照一个你规定的顺序排序,如果标准化的单词没有重复的,该单词自然符合条件;


代码如下:

#include <iostream>
#include <map>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>

using namespace std;

map<string, int> suffix;  //保存合适的下标;
vector<string> words;

string repr(string &s){
    string r = s;
    for(auto &c: r){
        c = tolower(c);
    }
    sort(r.begin(), r.end());
    return r;
}

int main()
{
    string s, stbuf;
    while(cin >> s && s[0] != '#'){
        words.push_back(s);
        string r = repr(s);  //标准化;

        if(!suffix.count(r))
            suffix[r] = words.size()-1;
        else suffix[r] = -1;
    }
    vector<string> ans;
    for(auto c: suffix){
        if(c.second != -1)
            ans.push_back(words[c.second]);
    }
    sort(ans.begin(), ans.end());
    for(auto c: ans){
        cout << c << endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值