UVa156 Ananagrams,map

Ananagrams

Most crosswordpuzzle fans are used to anagrams--groups of wordswith the same letters in different orders--for example OPTS, SPOT, STOP, POTSand POST. Some words however do not have this attribute, no matter how yourearrange their letters, you cannot form another word. Such words are calledananagrams, an example isQUIZ.

Obviously suchdefinitions depend on the domain within which we are working; you might thinkthat ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE.One possible domain would be the entire English language, but this could leadto some problems. One could restrict the domain to, say, Music, in which caseSCALE becomes a relative ananagram (LACES is not inthe same domain) but NOTE is not since it can produce TONE.

Write a programthat will read in the dictionary of a restricted domain and determine therelative ananagrams. Note that single letter words are, ipso facto, relativeananagrams since they cannot be ``rearranged'' at all. The dictionary willcontain no more than 1000 words.


Input

Input will consistof a series of lines. No line will be more than 80 characters long, but maycontain any number of words. Words consist of up to 20 upper and/or lower caseletters, and will not be broken across lines. Spaces may appear freely aroundwords, and at least one space separates multiple words on the same line. Notethat words that contain the same letters but of differing case are consideredto be anagrams of each other, thus tIeD and EdiT are anagrams. The file will beterminated by a line consisting of a single #.


output

Output willconsist of a series of lines. Each line will consist of a single word that is arelative ananagram in the input dictionary. Words must be output inlexicographic (case-sensitive) order. There will always be at least onerelative ananagram.


Sample input

ladder came tape soonleader acme RIDE lone Dreis peat

 ScAlE orb eye  Rides dealer  NotE derail LaCeS  drIed

noel dire Disk maceRob dries

#


Sample output

Disk

NotE

derail

drIed

eye

ladder

soon

 

#include <cstdio>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

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

string repr(string s){
    for(int i=0; i<s.size(); ++i)
        s[i] = tolower(s[i]);
    sort(s.begin(), s.end());
    return s;
}
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]);
    }
    for(int i=0; i<ans.size(); ++i){
        cout<<ans[i]<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值