zoukankan      html  css  js  c++  java
  • Word Amalgamation(枚举 + 排序)

    Word Amalgamation

    Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247

    Description

    In millions of newspapers across 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 'words' 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
    nfudre
    aptr
    sett
    oresuc
    XXXXXX
    
    

    Sample Output

    score
    ******
    refund
    ******
    part
    tarp
    trap
    ******
    NOT A VALID WORD
    ******
    course
    ******
    

    HINT

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 char a[110][20] ;
     6 char b[110][20] ;
     7 
     8 void Sort (int n)
     9 {
    10     for (int i = 1 ; i < n; i++) {
    11         if (strcmp (a[i] , a[i - 1]) < 0) {
    12             char ans[20] ;
    13             strcpy (ans , a[i] ) ;
    14             int j ;
    15             for (j = i - 1 ; j >= 0 && strcmp (ans , a[j]) < 0 ; j--) {
    16                strcpy (a[j + 1] , a[j] ) ;
    17             }
    18             strcpy (a[j + 1] , ans ) ;
    19         }
    20     }
    21 }
    22 
    23 int main ()
    24 {
    25     //freopen ("a.txt" , "r" , stdin ) ;
    26     int n = 0 , s ;
    27     do {
    28         gets (a[n++]) ;
    29     } while (strcmp (a[n - 1] , "XXXXXX") != 0 ) ;
    30     n-- ;
    31     Sort (n) ;
    32     int k = 0 ;
    33     do {
    34         gets (b[k++]) ;
    35     } while (strcmp (b[k - 1] , "XXXXXX") != 0 ) ;
    36     k-- ;
    37     for (int i = 0 ; i < k ; i++) {
    38         bool flag = 0 ;
    39         for (int j = 0 ; j < n ; j++) {
    40             int l1 , l2 ;
    41             char s1[20] ;
    42             char s2[20] ;
    43             strcpy (s1 , b[i]) ; l1 = strlen (s1) ; //puts (s1) ;
    44             strcpy (s2 , a[j]) ; l2 = strlen (s2) ; //puts (s2) ;
    45             if (l1 != l2)
    46                 continue ;
    47             sort (s1 , s1 + l1 ) ; //puts (s1) ;
    48             sort (s2 , s2 + l2  ) ; //puts (s2) ;
    49             for (s = 0 ; s < l1 ; s++) {
    50                 if (s1[s] != s2[s])
    51                     break ;
    52             }
    53             if (s == l1) {
    54                 printf ("%s
    " , a[j]) ;
    55                 flag = 1 ;
    56             }
    57         }
    58         if (!flag) {
    59             puts ("NOT A VALID WORD") ;
    60             puts ("******") ;
    61         }
    62         else {
    63             puts ("******") ;
    64         }
    65     }
    66     return 0 ;
    67 }
    View Code
  • 相关阅读:
    列表和元组
    UVM宏
    UVM中重要函数
    组合模式(composite)
    装饰器模式(Decorator)
    适配器模式(Adapter)
    桥接模式
    原型模式(prototype)
    单例模式(singleton)
    UML类图
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4363619.html
Copyright © 2011-2022 走看看