UVa 642 Word Amalgamation: Dictionary & string sorting

Source: Internet
Author: User
Tags bool sort strcmp strlen time limit valid

642-word Amalgamation

Time limit:3.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=583

In millions of newspapers across the United States there is a word game called. The object of this game was to solve a riddle, but in order to find the letters that appear in the answer it is necessary t O unscramble four words. Your task is to write a program that can unscramble words.

Input

The input file contains four parts:

1.

A dictionary, which consists of at least one and at most words, one each line;

2.

A line containing XXXXXX, which signals the end of the dictionary;

3.

One or more scrambled ' words ' so 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 to lowercase 中文版 letters and would be at L East one and at most six characters long. (Note that the Sentinel XXXXXX contains Uppercasex ' s.) The dictionary is isn't necessarily in sorted order, but the all word in the dictionary is unique.

Output

For each scrambled word in the input, output a alphabetical list of all dictionary words that can is formed by rearrangin G 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

Nfudre

Aptr

Sett

Oresuc

Xxxxxx

Sample Output

Score

******

Refund

******

Part

Tarp

Trap

******

Not A VALID WORD

******

Course

******

All sorted.

Complete code:

/*0.015s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  
Char word[110][10], sorted[110][10], s[10];  
    
Char *pstr[110];  
BOOL Cmp_string (const char *a, const char *b) {return strcmp (A, B) < 0;  
    int main (void) {int n = 0;  
        while (gets (Word[n]), word[n][0]!= ' X ') {pstr[n] = Word[n];  
    ++n; ///Exchange two string arrays?  
    Just swap the pointers.  
    Sort (pstr, Pstr + N, cmp_string);  
        Note that the order of Word does not change after sorting, but sorted is the dictionary order (int i = 0; i < n; i++) {strcpy (sorted[i), pstr[i]);  
    Sort (Sorted[i], Sorted[i] + strlen (sorted[i));  
        while (gets (s), s[0]!= ' X ') {sort (s, S + strlen (s));  
        BOOL found = false; for (int i = 0; i < n; i++) if (strcmp (sorted[i), s) = = 0) {found = True  
                ;  
            Puts (Pstr[i]); } if (!Found) puts ("Not A VALID WORD");  
    Puts ("Hu Jintao");  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.