Word Amalgamation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1646 Accepted Submission (s): 758
Problem Description
In millions of newspapers initialize ss the United States there is a word game called Jumble. the object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. your task is to write a program that can unscramble words.
Input
The input contains four parts:
1. a dictionary, which consists of at least one and at most 100 words, one per line;
2. a line containing XXXXXX, which signals the end of the dictionary;
3. one or more scrambled 'word' that you must unscramble, each on a line by itself; and
4. another line containing XXXXXX, which signals the end of the file.
All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X's .) the dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. each word in this list must appear on a line by itself. if the list is empty (because no dictionary words can be formed), output the line ''not a valid word" instead. in either case, output a line containing six asterisks to signal the end of the list.
Sample Input
Tarp
Given
Score
Refund
Only
Trap
Work
Earn
Course
Pepper
Part
XXXXXX
Resco
Nfudh
Aptr
Sett
Oresuc
XXXXXX
Sample Output
Score
******
Refund
******
Part
Tarp
Trap
******
NOT A VALID WORD
******
Course
******
The most important thing is map usage.
Question:
Input dictionary XXXXXX end dictionary input and then input string if the string can constitute A string in the dictionary output this string otherwise output NOT A VALID WORD
Enter XXXXXX to end the reorganization Data Input
Train of Thought: simply sort each character in each dictionary string in ascending order and then add the original string and sorted string to the map as a group.
Brute Force Method
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
Using namespace std;
Struct node
{
Char s [10];
Char st [10];
Bool operator <(const node & B) const
{
Return strcmp (s, B. s) <0;
}
} Rc [2, 103];
Int main ()
{
Char s [10];
Int I, n = 0;
While (scanf ("% s", s), strcmp (s, "XXXXXX ")! = 0)
{
Strcpy (rc [n]. s, s );
Sort (s, s + strlen (s ));
// Puts (s );
Strcpy (rc [n]. st, s );
N ++;
}
Sort (rc, rc + n );
Int f;
While (scanf ("% s", s), strcmp (s, "XXXXXX ")! = 0)
{
Sort (s, s + strlen (s ));
F = 0;
For (I = 0; I <n; I ++)
If (strcmp (rc [I]. st, s) = 0)
F = 1, printf ("% s \ n", rc [I]. s );
If (! F)
Printf ("not a valid word \ n ");
Printf ("******* \ n ");
}
Return 0;
}
[Cpp]
/* String */
[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <map>
# Include <string>
# Include <algorithm>
Using namespace std;
Map <string, string> node;
Int main ()
{
String s;
While (cin> s & s! = "XXXXXX ")
{
String t = s;
Sort (s. begin (), s. end (); // This method is awesome.
Node. insert (pair <string, string> (t, s ));
}
While (cin> s)
{
If (s = "XXXXXX") break;
Int flag = 1;
Map <string, string >:: iterator it;
Sort (s. begin (), s. end ());
For (it = node. begin (); it! = Node. end (); it ++)
{
If (it-> second = s)
{
Flag = 0; cout <it-> first <endl;
}
}
If (flag) cout <"not a valid word" <endl;
Cout <"********" <endl;
}
Return 0;
}