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

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 }

 

转载于:https://www.cnblogs.com/Enumz/p/4060305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值