UVaOJ 156 - Ananagrams

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving ::Sorting/Searching


Description

Anagram 指的是对于某个单词来说, 仅仅调换了字母排列顺序的单词。

给出一个字典, 它含有多个单词, 找出里面不是其他单词 anagram 的单词。

按字典序输出。


Type

Sorting/Searching


Analysis

要判断两个单词是否为对方的 anagram,

只需要将他们全部变为小写字母,

然后对他们内部的字母进行排序,

最后看得到的字符串是否相等即可。


会判断 anagram 后, 剩下的枚举和字典序输出就很简单了。

枚举和字典序输出, 也可以利用一下 STL 的 set 或 map 一同解决。


Solution

// UVaOJ 156
// Ananagrams
// by A Code Rabbit

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;

string str;

int main() {
    map<string, string> words;
    multiset<string> anagrams;
    while (cin >> str && str != "#") {
        string tmp(str);
        for (int i = 0; i < tmp.length(); i++)
            tmp[i] = tolower(tmp[i]);
        sort(tmp.begin(), tmp.end());
        words.insert(make_pair(str, tmp));
        anagrams.insert(tmp);
    }

    for (map<string, string>::iterator iter = words.begin();
        iter != words.end();
        ++iter)
    {
        if (anagrams.count(iter->second) == 1)
            cout << iter->first << endl;
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值