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 drIed
noel dire Disk mace Rob dries
#
Sample output
Disk
NotE
derail
drIed
eye
ladder
soon
一个map作为单个字符串排序后与原字符串的映射;
一个map作为排序后字符串的出现次数;
一个vector记录所有字符串,用来检索;
另一个vector用来记录所有出现次数为1的字符串,并作为输出。
本来自己写感觉没什么问题的,但是和Liu的代码比起来简直是太不美观了,我的代码比较乱,可读性太低。另外一直在CE,后来才发现是定义的是time会同另外一个一个time函数同名,所以出错。CE,Compilation Error,编译错误。
Liu的比较清晰。但是时间效率没有我的高,所以说我这是典型的以空间换时间,但说实话,我的这个时间效率的提升真的没有必要。
另外,count()和insert()函数仅map和set可以使用,sort()对vector使用时应使用迭代器begin()和end()。
附代码如下:
本人代码:
#include<map>
#include<cctype>
#include<vector>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
map<string,string> alpha;
map<string,int> times;
vector<string> words;
vector<string> anan;
int main(){
//freopen("in.txt","r",stdin);
string s;
while(cin>>s&&s!="#"){
stringstream ss(s);
string buf;
while(ss>>buf){
string tmp(buf);
int len=tmp.size();
for(int i=0;i<len;i++){
tmp[i]=tolower(tmp[i]);
}
sort(tmp.begin(),tmp.end());
words.push_back(tmp);
if(!times[tmp]){
alpha[tmp]=buf;
times[tmp]++;
}else times[tmp]++;
//cout<<times[tmp]<<endl;
//cout<<tmp<<endl;
}
}
for(int i=0;i<words.size();i++){
string x(words[i]);
if(times[x]==1)anan.push_back(alpha[x]);
//cout<<times[x]<<endl;
//cout<<words[i]<<endl;
}
sort(anan.begin(),anan.end());
for(int i=0;i<anan.size();i++)cout<<anan[i]<<endl;
//fclose(stdin);
return 0;
}
Liu的代码:
#include<map>
#include<cctype>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
map<string,int> alpha;
vector<string> words;
string change(string str){
for(int i=0;i<str.size();i++){
str[i]=tolower(str[i]);
}
sort(str.begin(),str.end());
return str;
}
int main(){
//freopen("in.txt","r",stdin);
string s;
while(cin>>s){
if(s[0]=='#')break;
words.push_back(s);
//cout<<s<<endl;
s=change(s);
//cout<<s<<endl;
if(!alpha.count(s))alpha[s]=0;
alpha[s]++;
//cout<<alpha[s]<<endl;
}
vector<string> ans;
for(int i=0;i<words.size();i++){
//cout<<words[i]<<endl;
if(alpha[change(words[i])]==1)ans.push_back(words[i]);
}
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<endl;
}
//fclose(stdin);
return 0;
}