uva 156 Ananagrams 解题报告

来源:互联网 发布:服务器与数据库的区别 编辑:程序博客网 时间:2024/05/17 04:58



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

Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always 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  drIednoel dire Disk mace Rob dries#

sample output

DiskNotEderaildrIedeyeladdersoon


入门经典STL部分例题。题目本身算法较简单,主要为学习map。

涉及到对map的理解与用法,诸如关键字的对应关系,count()函数,[]运算符等。

同时加深了对sort的理解。



#include <iostream>#include <cstdio>#include <string>#include <vector>#include <algorithm>#include <map>using namespace std;string change(string c){    int len=(int)c.length();    for(int i=0;i<len;i++)        c[i]=tolower(c[i]);    //tolower,isalpha等函数在<iostream>与<cctype>中都有    sort(c.begin(),c.end());    //传入的地址由begin()和end()算出    //将构成字符串的字符按字典序(字母对应ascii码从小到大)排列    return c;}int main(){    string s;    vector<string> all;    vector<string> ans;    map<string,int> m;    while(cin>>s)    {        if(s[0]=='#')            break;        all.push_back(s);        string c=change(s);        //count()返回值只有1或0,对应传入的关键字是否已出现        if(!m.count(c))            m[c]=0;        m[c]++;    }    int size=(int)all.size();    for(int i=0;i<size;i++)    {        if(m[change(all[i])]==1)            ans.push_back(all[i]);    }    sort(ans.begin(),ans.end());    //将不定长数组中的多个字符串按照字典序排列    int num=(int)ans.size();    for(int i=0;i<num;i++)        cout<< ans[i] << "\n";    return 0;}








0 0
原创粉丝点击