zoukankan      html  css  js  c++  java
  • poj 1035 Spell checker

    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

    大致题意:

    输入一部字典,输入若干单词

    1、  若某个单词能在字典中找到,则输出corret

    2、  若某个单词能通过 变换 或 删除 或 添加一个字符后,在字典中找得到,则输出这些单词,输出顺序根据  输入的那部字典的字典序

    3、  若某个单词无论操作与否都无法在字典中找得到,则输出空

    直接枚举

      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstring>
      4 #include <cstdio>
      5 #include <algorithm>
      6 using namespace std;
      7 const int maxn=10005;
      8 char dic[maxn][20];
      9 int cnt;
     10 void Read()
     11 {
     12     cnt=0;
     13     while(cin>>dic[cnt],dic[cnt][0]!='#')
     14         cnt++;
     15 }
     16 bool fun(char *str)
     17 {
     18     for(int i=0;i<cnt;i++)
     19     {
     20         if(strcmp(dic[i],str)==0)
     21             return true;
     22     }
     23     return false;
     24 }
     25 bool add(char *dict,char *str)
     26 {
     27     int k=0;
     28     while(*dict)
     29     {
     30         if(*dict==*str)
     31         {
     32             dict++;
     33             str++;
     34         }
     35         else
     36         {
     37             dict++;
     38             k++;
     39             if(k>1)
     40                 return false;
     41         }
     42     }
     43     return true;
     44 }
     45 bool del(char *dict,char *str)
     46 {
     47     int k=0;
     48     while(*str)
     49     {
     50         if(*dict==*str)
     51         {
     52             dict++;
     53             str++;
     54         }
     55         else
     56         {
     57             str++;
     58             k++;
     59             if(k>1)
     60                 return false;
     61         }
     62     }
     63     return true;
     64 }
     65 void change(char *str)
     66 {
     67     printf("%s:",str);
     68     int a,b=strlen(str);
     69     int k,c;
     70     for(int i=0;i<cnt;i++)
     71     {
     72         a=strlen(dic[i]);
     73         if(a==b) //改变一个字符的情况
     74         {
     75              k=0;
     76             for(int j=0;j<a;j++)
     77             {
     78                 if(dic[i][j]!=str[j])
     79                     k++;
     80             }
     81             if(k==1)
     82                 printf(" %s",dic[i]);
     83         }
     84         if(a==b+1) //添加一个字符的情况
     85         {
     86             if(add(dic[i],str))
     87                 printf(" %s",dic[i]);
     88         }
     89         if(a==b-1) //减去一个字符的情况
     90         {
     91             if(del(dic[i],str))
     92                 printf(" %s",dic[i]);
     93         }
     94     }
     95 }
     96 int main()
     97 {
     98     char str[20];
     99     Read();
    100     while(cin>>str,str[0]!='#')
    101     {
    102         if(fun(str))
    103             printf("%s is correct
    ",str);
    104         else
    105         {
    106             change(str);
    107             cout<<endl;
    108         }
    109     }
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    54.施工方案第二季(最小生成树)
    53.FIB词链
    52.1076 排序
    最短路径:我的理解--SPFA算法
    POJ2187Beauty Contest
    CodeForces 279B Books
    SDUT 2527 斗地主
    HDU1020 Encoding
    POJ 2635 The Embarrassed Cryptographer
    POJ 1942 Paths on a Grid(组合数)
  • 原文地址:https://www.cnblogs.com/cxbky/p/4931309.html
Copyright © 2011-2022 走看看