现在的位置: 首页 > 综合 > 正文

UVa156 Ananagrams,map

2018年12月19日 ⁄ 综合 ⁄ 共 2263字 ⁄ 字号 评论关闭

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;
}


抱歉!评论已关闭.