156-ananagrams
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem &problem=92
Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OP TS, SPOT, STOP, POTS and POST. Some words however don't have this attribute, no matter how to you rearrange their letters, you cannot form another word. Such words are called ananagrams, a example is quiz.
Obviously such definitions depend on the domain within which we are working; You might ' Athene is a ananagram, whereas any chemist would quickly produce. One possible domain would be the entire 中文版 language, but this could leads to some problems. One could restrict the domain to, say, Music, into which case SCALE becomes a relative ananagram (laces isn't in the same d Omain) But note isn't since it can produce TONE.
Write a program that would read in the dictionary of a restricted domain and determine the relative ananagrams. Note This single letter words are, ipso facto, relative the ananagrams since they be ' cannot ' to all. The dictionary would contain no more than 1000 words.
Input
Input would consist of a series of lines. No line is more than characters long and but may contain any number of words. Words consist of upper and/or lower case letters, and won't is broken across. Spaces may appear freely around words, and in least one space separates multiple to on the words line. Note This words that contain the same letters but of differing case are considered to is anagrams of each other, thus tied and EdiT are anagrams. The file would be terminated by a line consisting of a single #.
Output
Output would consist of a series of lines. Each line would consist of a single word this is a relative ananagram in the input dictionary. Words must is output in lexicographic (case-sensitive) order. There'll always is 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 Dr Ied
Noel Dire Disk mace Rob Dries
#
Sample output
Disk
Note
derail
dried
eye
Ladder
soon
Train of thought: first use to establish a multimap< sort of word, unsorted word > To process input, finished will only one value key into a set, finally traverse set output. (see Code for details)
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*0.015s*/
#include <cstdio>
#include <cstring>
#include <string>
#include < algorithm>
#include <map>
#include <set>
using namespace std;
Char s[25], origin[25];
Multimap<string, string> map1;
Set<string> Set1;
Multimap<string, string>::iterator pm;
Set<string>::iterator PS;
int main ()
{
int len, I;
while (scanf ("%s", s), s[0]!= ' # ')
{
strcpy (origin, s);
Len = strlen (s);
for (i = 0; i < len; ++i)
if (Isupper (S[i])) s[i] = ToLower (s[i));
Sort (s, S + len);
Map1.insert (Make_pair (S, origin));
for (PM = map1.begin (); PM!= map1.end (); ++pm)
if (Map1.count (pm->first) = = 1)
Set1.insert (Pm->second); For
(PS = Set1.begin (); PS!= set1.end (); ++ps)
puts ((*ps). C_STR ());
return 0;
}