码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1035——Spell checker(字符串处理)

时间:2014-10-29 21:09:25      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   ar   for   sp   

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)增加一个字符 2)删掉一个字符 3)改变一个字符

解题思路:

    字符串处理,先判断在不在字典中。

    不在的话再判断是否可以变性为字典中的字符串。注意判断语句的写法即可。

Code:

  1 /*************************************************************************
  2     > File Name: poj1035.cpp
  3     > Author: Enumz
  4     > Mail: 369372123@qq.com 
  5     > Created Time: 2014年10月19日 星期日 15时27分59秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<cstdlib>
 11 #include<string>
 12 #include<cstring>
 13 #include<list>
 14 #include<queue>
 15 #include<stack>
 16 #include<map>
 17 #include<set>
 18 #include<algorithm>
 19 #define MAXN 1000
 20 using namespace std;
 21 char dic[100000][100];
 22 char word[1000000];
 23 int Num_dic;
 24 bool Find_same(char *word)
 25 {
 26     bool ok=0;
 27     for (int i=1;i<=Num_dic;i++)
 28         if (strcmp(dic[i],word)==0)
 29         {
 30             ok=1;
 31             break;
 32         }
 33     return ok;
 34 }
 35 void Find_opt(char *word)
 36 {
 37     for (int i=1;i<=Num_dic;i++)
 38     {
 39         int len_word=strlen(word);
 40         int len_dic=strlen(dic[i]);
 41         if (len_word==len_dic)
 42         {
 43             int cnt=0;
 44             for (int j=0;j<=len_word-1;j++)
 45                 if (dic[i][j]!=word[j]) cnt++;
 46             if (cnt==1)
 47                 printf(" %s",dic[i]);
 48         }
 49         else if (len_word-len_dic==1)
 50         {
 51             int k1=0,k2=0,cnt=0;
 52             while (1)
 53             {
 54                 if (k1==len_dic&&k2==len_word) 
 55                     break;
 56                 if (cnt>=2)
 57                     break;
 58                 if (dic[i][k1]==word[k2])
 59                     k1++,k2++;
 60                 else k2++,cnt++;
 61             }
 62             if (cnt==1)
 63                 printf(" %s",dic[i]);
 64         }
 65         else if (len_dic-len_word==1)
 66         {
 67             int k1=0,k2=0,cnt=0;
 68             while (1)
 69             {
 70                 if (k1==len_dic&&k2==len_word)
 71                     break;
 72                 if (cnt>=2)
 73                     break;
 74                 if (dic[i][k1]==word[k2])
 75                     k1++,k2++;
 76                 else k1++,cnt++;
 77             }
 78             if (cnt==1)
 79                 printf(" %s",dic[i]);
 80         }
 81     }
 82 }
 83 int main()
 84 {
 85     Num_dic=1;
 86     while (1)
 87     {
 88         scanf("%s",dic[Num_dic]);
 89         if(strcmp(dic[Num_dic],"#")==0)
 90         {
 91             Num_dic--;
 92             break;
 93         }
 94         else Num_dic++;
 95     }
 96     while (1)
 97     {
 98         scanf("%s",word);
 99         if (strcmp(word,"#")==0)
100             break;
101         if (Find_same(word))
102             printf("%s is correct\n",word);
103         else 
104         {
105             printf("%s:",word);
106             Find_opt(word);
107             printf("\n");
108         }
109     }
110     return 0;
111 }

 

POJ1035——Spell checker(字符串处理)

标签:des   style   blog   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/Enumz/p/4060305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!