您的位置:首页 > 其它

Ananagrams id:156

2016-03-26 11:09 218 查看
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 arelative 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  drIed
noel dire Disk mace Rob dries
#

Sample output

Disk
NotE
derail
drIed
eye
ladder
soon

分析:

由于一个单词往往由多个字母构成,如果要判断一个单词的字母重排后能否构成另外一个单词,操作实在太复杂,也极为昂贵。可以每读入一个单词,就将其字母排序。然后将所有字母转成小写。这样可以很快判断出两个单词能否等价,因为如果两个单词的字母重排后可以使得两个单词相同,那么将两个单词按照字典顺序排序后两个单词也肯定相等。其中要用到STL中的map容器,把string类型的单词作map容器中的键,该单词出现的次数作为map中键相对应的value(值)。代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>

using namespace std;

vector<string> words;//用这个vector来存储所有的单词
map<string, int> cnt;//保存单词出现的次数

string reshape(const string &str) {
string ans = str;
for(int i = 0; i < ans.length(); i++)
ans[i] = tolower(ans[i]);
sort(ans.begin(), ans.end());
return ans;
}

int main()
{
string str;

while(cin >> str) {     //对输入字符串进行整理
if(str[0] == '#')      break;
words.push_back(str);
string s = reshape(str);
if(!cnt.count(s))   cnt[s] = 0;
cnt[s]++;
}

vector<string>  ans;        //该vector用来存储所有要输出到答案的单词
for(int i = 0; i < words.size(); i++) {
string s = reshape(words[i]);
if(cnt[s] == 1)
ans.push_back(words[i]);
}

sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++)
cout << ans[i] << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: