Link to the question: Ultraviolet A 156 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.
Obviusly such definitions depend on the domain within which we are working; you might think that Athene is an anagram, whereas any chemist wowould quickly produce ethane. one possible domain wocould be the entire English language, but this cocould lead to some problems. one cocould restrict the domain to, say, music, in which case scale becomes a relative anagram (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 anagrams. 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 in any number of words. words consist of up to 20 upper and/or lower case letters, and will not be broken into SS 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 anantionary in the input dictionary. words must be output in lexicographic (case-sensitive) Order. there will always be at least one relative ananan.pdf.
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
Question
A word can be changed in any order, that is, after the letters are rearranged, the same form (Case Insensitive) can be found in the dictionary, indicating that it can be changed. Now you need to find the words in the same form after replacement, and then sort the output in Lexicographic Order.
Analysis
The question is not difficult, but the main problem is the processing of strings. In fact, you don't need to re-arrange each word. You just need to convert the letters in each word into the same case and then sort them in ascending or descending order, and then compare them one by one to see if they are equal, if they are equal, they can be found. Both are marked as true. If they are not equal, they cannot be found. This is false, which is the word we are looking, note that the original form of the word is output. In fact, the key issue lies in string sorting. If string is used for processing, it is very simple. However, if the two-dimensional character array is used to add the sort function, the problem may occur, if DIC [] [] is used for storage and sorting, an error is reported, but * DIC [] is used for storage. This may be related to the sort function prototype.
Code
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;bool cmp1(char a, char b){ return a < b;}bool judge(char a[], char b[]){ if(strlen(a) != strlen(b)) return false; int len = strlen(a); char aa[1010], bb[1010]; strcpy(aa, a); strcpy(bb, b); for(int i = 0; i < len; i++) { if(aa[i] >= 'A' && a[i] <= 'Z') aa[i] += 'a'-'A'; if(b[i] >= 'A' && b[i] <= 'Z') bb[i] += 'a'-'A'; } sort(aa, aa+len, cmp1); sort(bb, bb+len, cmp1); if(strcmp(aa, bb) == 0) return true; return false;}bool cmp2(char* a, char* b){ return strcmp(a, b) < 0;}int main(){ int n = 0, cnt; char str[1010][30]; char *dic2[30]; bool vis[1010]; while(scanf("%s", str[n++])) if(str[n-1][0] == '#') break; n--; memset(vis, false, sizeof(vis)); cnt = 0; for(int i = 0; i < n; i++) { if(!vis[i]) for(int j = i+1; j < n; j++) if(judge(str[i], str[j])) vis[i] = vis[j] = true; if(!vis[i]) dic2[cnt++] = str[i]; } sort(dic2, dic2+cnt, cmp2); for(int i = 0; i < cnt; i++) printf("%s\n", dic2[i]); return 0;}