Ananagrams
Ananagrams |
Most crossword puzzle fans are used to anagrams--groupsof words with the same letters in different orders--for exampleOPTS, SPOT, STOP, POTS and POST. Some words however do not have thisattribute, no matter how you rearrange their letters, you cannot formanother word. Such words are called ananagrams, an example isQUIZ.
Obviously such definitions depend on the domain within which we areworking; you might think that ATHENE is an ananagram, whereas anychemist would quickly produce ETHANE. One possible domain would be theentire English language, but this could lead to some problems. Onecould restrict the domain to, say, Music, in which case SCALE becomesa relative ananagram (LACES is not in the same domain) but NOTEis not since it can produce TONE.
Write a program that will read in the dictionary of a restricteddomain and determine the relative ananagrams. Note that single letterwords are, ipso facto, relative ananagrams since they cannot be``rearranged'' at all. The dictionary will contain no morethan 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80characters long, but may contain any number of words. Words consist ofup to 20 upper and/or lower case letters, and will not be brokenacross lines. Spaces may appear freely around words, and at least onespace separates multiple words on the same line. Note that words thatcontain the same letters but of differing case are considered to beanagrams of each other, thus tIeD and EdiT are anagrams. The file willbe terminated by a line consisting of a single #.
Output
Output will consist of a series of lines. Each line will consist of asingle word that is a relative ananagram in the input dictionary.Words must be output in lexicographic (case-sensitive) order. Therewill 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
这题大意是说有一种单词被称为anagrams,这种单词的顺序通过调换能够组成另外的单词,现在给我们一串的字符串,要求查找其中的非anagrams的单词,并输出,我是通过建立一个结构体储存各个单词及其小写排序后的原型以及标记该单词原型是否有重复。对于输入的单词不断的和结构体中的各单词原型,若都不相等则将其加入结构体数组中,最后通过标记输出。
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int N = 100;
const int L = 30;
struct Node1 {
char s_1[L];
char s_2[L];
bool flag;
}a[N];
bool cmp1(Node1 a,Node1 b) {
return strcmp(a.s_2,b.s_2) < 0;
}
bool cmp2(Node1 a,Node1 b) {
return strcmp(a.s_1,b.s_1) < 0;
}
int main() {
char tmp[L];
int t = 0;
int len;
memset(a,0,sizeof(a));
while ((cin >> tmp) && tmp[0] != '#') {
strcpy(a[t].s_1,tmp);
len = strlen(tmp);
for (int i = 0; i < len; i++)
if (isupper(tmp[i]))
tmp[i] = tolower(tmp[i]);
sort(tmp,tmp+len);
strcpy(a[t].s_2,tmp);
t++;
}
sort(a,a+t,cmp1);
//for (int i = 0; i < t; i++)
// cout << a[i].s_1 << " " << a[i].s_2 << endl;
if (strcmp(a[0].s_2,a[1].s_2))
a[0].flag = true;
for (int i = 1; i < t-1; i++)
if (strcmp(a[i].s_2,a[i-1].s_2) && strcmp(a[i].s_2,a[i+1].s_2))
a[i].flag = true;
if (strcmp(a[t-1].s_2,a[t-2].s_2))
a[t-1].flag = true;
sort(a,a+t,cmp2);
for (int i = 0; i < t; i++) {
if (a[i].flag)
cout << a[i].s_1 << endl;
}
return 0;
}