zoukankan      html  css  js  c++  java
  • Spell checker(暴力)

    Spell checker
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 20188   Accepted: 7404

    Description

    You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
    If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations: 
    ?deleting of one letter from the word; 
    ?replacing of one letter in the word with an arbitrary letter; 
    ?inserting of one arbitrary letter into the word. 
    Your task is to write the program that will find all possible replacements from the dictionary for every given word. 

    Input

    The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character '#' on a separate line. All words are different. There will be at most 10000 words in the dictionary. 
    The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character '#' on a separate line. There will be at most 50 words that are to be checked. 
    All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most. 

    Output

    Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: " is correct". If the word is not correct then write this word first, then write the character ':' (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.

    Sample Input

    i
    is
    has
    have
    be
    my
    more
    contest
    me
    too
    if
    award
    #
    me
    aware
    m
    contest
    hav
    oo
    or
    i
    fi
    mre
    #

    Sample Output

    me is correct
    aware: award
    m: i my me
    contest is correct
    hav: has have
    oo: too
    or:
    i is correct
    fi: i
    mre: more me

    Source

      1 #include<stdio.h>
      2 #include<string.h>
      3 char st[10010][20] ;
      4 struct word_check
      5 {
      6     char src[20] ;
      7     bool flag ;
      8     int cnt , no[100] ;
      9 }word[60];
     10 
     11 int k , f;
     12 
     13 void check ()
     14 {
     15     int diff ;
     16     int ans ;
     17     for (int i = 0 ; i < f ; i++)
     18         for (int j = 0 ; j < k ; j++)
     19             if (strcmp (word[i].src , st[j]) == 0) {
     20                 word[i].flag = 1 ;
     21             }
     22 
     23             for (int i = 0 ; i < f ; i++)
     24                 if (!word[i].flag)
     25                     for (int j = 0 ; j < k ; j++) {
     26                         diff = strlen (word[i].src) - strlen (st[j]) ;
     27                         if (diff == 0) {
     28                             ans = 0 ;
     29                             for (int l = 0 ; st[j][l] != '' ; l++) {
     30                                 if (st[j][l] != word[i].src[l])
     31                                     ans ++ ;
     32                                 if (ans > 1)
     33                                     break ;
     34                             }
     35                             if (ans == 1) {
     36                                 word[i].no [word[i].cnt] = j ;
     37                                 word[i].cnt ++ ;
     38                             }
     39                         }
     40                         else if (diff == 1) {
     41                             int  a = 0 ;
     42                             ans = 0 ;
     43                             for (int l = 0 ; word[i].src[l] != '' ; l++ , a++) {
     44                                 if (st[j][a] != word[i].src[l]) {
     45                                     ans ++ ;
     46                                     a-- ;
     47                                 }
     48                                 if (ans > 1)
     49                                     break ;
     50                             }
     51                             if (ans == 1) {
     52                                 word[i].no[word[i].cnt] = j ;
     53                                 word[i].cnt ++ ;
     54                             }
     55                         }
     56                         else if (diff == -1) {
     57                             int a = 0 ;
     58                             ans = 0 ;
     59                             for (int l = 0 ; st[j][a] != '' ; l++ , a++) {
     60                                 if (st[j][a] != word[i].src[l]) {
     61                                     ans ++ ;
     62                                     l-- ;
     63                                 }
     64                                 if (ans > 1)
     65                                     break ;
     66                             }
     67                             if (ans == 1) {
     68                                 word[i].no[word[i].cnt] = j ;
     69                                 word[i].cnt ++ ;
     70                             }
     71                         }
     72                     }
     73 
     74     for (int i = 0 ; i < f ; i++) {
     75         if (word[i].flag) {
     76             printf ("%s is correct
    " , word[i].src) ;
     77         }
     78         else {
     79             printf ("%s:" , word[i].src) ;
     80             for (int j = 0 ; j < word[i].cnt ; j++) {
     81                 printf (" %s" , st[word[i].no[j]]) ;
     82 
     83             }
     84             //printf ("%d
    " , word[i].cnt) ;
     85             printf ("
    ") ;
     86         }
     87     }
     88 }
     89 
     90 
     91 int main ()
     92 {
     93   //  freopen ("a.txt" , "r" , stdin) ;
     94     while (~ scanf ("%s" , st[0])) {
     95         k = 0 ;
     96         f = 0 ;
     97         getchar () ;
     98         while (1) {
     99             gets (st[++k]) ;
    100             if (strcmp (st[k] , "#") == 0)
    101                 break ;
    102         }
    103         while (1) {
    104             gets (word[f].src) ;
    105             word[f].cnt = 0 ;
    106             f++ ;
    107             if (strcmp (word[f - 1].src , "#") == 0)
    108                 break ;
    109         }
    110         f-- ;
    111      /*   for (int i = 0 ; i < k ; i++)
    112             printf ("%d " , word[i].flag) ;
    113         puts ("");
    114         for (int i = 0 ; i < f ; i++)
    115             puts (word[i].src) ;*/
    116         check () ;
    117     }
    118     return 0 ;
    119 }
    View Code
  • 相关阅读:
    java后台保存JSON
    查询树节点及其所有上级节点sql语句
    查询树节点及其所有下级节点sql语句
    Hibernate查询机制使用原生sql语法查询
    SSH框架通过poi导出excel表格
    java通过poi导入excel数据
    各类型日期date的相互转化
    推荐一下我喜欢的软件
    青岛市赛总结——远征石油大学
    My learn of git
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4320406.html
Copyright © 2011-2022 走看看