UVA 156 Ananagrams(map、set)

来源:互联网 发布:在淘宝当主播怎么赚钱 编辑:程序博客网 时间:2024/05/24 22:45

Ananagrams        

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ. Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE. Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be “rearranged” at all. The dictionary will contain no more than 1000 words.

Input

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

Output

Outputwillconsistofaseriesoflines. Eachlinewillconsistofasinglewordthatisarelativeananagram intheinputdictionary. Wordsmustbeoutputinlexicographic(case-sensitive)order. Therewillalways be at least one relative ananagram.

Sample Input

ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries #

Sample Output

Disk

NotE

derail

drIed

eye

ladder

soon

【题意】输入一些单词,找出满足:该单词不能通过字母重排、改变字母大小写的方式得到输入的另一个单词。输出时保留输入的大小写和字母顺序,按字典序排序(所有大写字母在小写字母之前)。

【分析】将输入的每个单词逐字母小写并排序,这样处理完之后插入到map中,每次插入时增加该关键字的值,就可以知道每种字母组合的单词出现几次。同时,将原始单词依次插入到set中,然后按序判断set中的单词经过上述处理后在map中对应的值,如果是1的话就输出,自带字典序。map应该以string为关键字,int为值。这题,紫书里先将所有单词标准化处理的思路很值得思考…虽说代码实现起来很简单,但感觉也不是特别容易想到的。还是挺有意思的…

#include<iostream>#include<cstdio>#include<algorithm>#include<map>#include<set>using namespace std;set<string> ipt;map<string,int> st;string lwt(string s){    for(int i=0;i<s.length();i++){        s[i]=tolower(s[i]);    }    sort(s.begin(),s.end());    return s;}int main(){    string s;    //freopen("test.txt","r",stdin);    while(cin>>s){        if(s[0]=='#')            break;        ipt.insert(s);        string ss=lwt(s);        if(!st.count(ss))            st[ss]=0;        st[ss]++;    }    for(set<string>::iterator it=ipt.begin();it!=ipt.end();it++){        if(st[lwt(*it)]==1)            cout<<*it<<endl;    }    return 0;}

原创粉丝点击