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、  若这个单词能在字典中找到,则输出is corret

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

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

解题思路:

字符串处理,就是逐个比较 待查单词 与 字典单词 的长度,当且仅当两者长度之差的绝对值<=1时才进行检查操作。暴力模拟

 

#include <iostream> #include <string.h> using namespace std; char dict[10001][16],str[16]; //dict数组用来存储字典(最多10000个单词,每个单词长度最多15),str用来存储待查的单词 int len[10001]={0},num1=0,len1; //len数组是与dict数组对应的,用来存储每个单词的长度,num1记录字典中单词的个数,len1是待 查单词的长度 void check(char *str1,char *str2) {     int dis=0; //dis用来存有多少个字符不一样     for(int i=0;i<len1;i++)       if(str1[i]==str2[i])         continue;       else         dis++;     if(dis>1) //两个单词不同的字符数必须只有一个         return;     else     { cout<<" "<<str2;return;} //符合规则就将单词输出

void Add(char *str1,char *str2) {     int dis=0; //dis用来存有多少个字符不一样     for(int i=0,j=0;i<strlen(str2);i++) // 待测单词长度短,所以我们以字典中的单词为母串,i是str2的下标,j是str1的下标       if(str1[j]!=str2[i])         dis++;       else         j++;     if(dis>1) //两个单词不同的字符数必须只有一个         return;     else     {         cout<<" "<<str2;return; //符合规则就将单词输出     } }

void delect(char *str1,char *str2) {     int dis=0; //dis用来存有多少个字符不一样     for(int i=0,j=0;i<len1;i++) // 待测单词长度长,所以我们以待测单词为母串,i是str1的下标,j是str2的下标       if(str1[i]!=str2[j])         dis++;       else         j++;     if(dis>1) //两个单词不同的字符数必须只有一个         return;     else     {         cout<<" "<<str2;return; //符合规则就将单词输出     } } int main() {     while(cin>>dict[num1]&&dict[num1][0]!='#') //输入字典中的单词,以#结束     {         len[num1]=strlen(dict[num1]); //同时记录每个单词的长度         num1++; //个数增1     }     while(cin>>str&&str[0]!='#') //输入待查单词,以#结束     {         int flag1=0,i; //flag1作为标志is correct的情况         len1=strlen(str); //计算待查单词的长度         for(i=0;i<num1;i++) //先看看待测单词在字典中是不是存在           if(strcmp(str,dict[i])==0)           {             flag1=1; //存在的话flag1为真,             break;           }         if(flag1==1)             cout<<str<<" is correct"<<endl;         else //不存在此单词,就开始3种变换         {             cout<<str<<":";             for(i=0;i<num1;i++) //遍历保证按照字典中的顺序输出             {                 if(len1==len[i]) //长度相等时就替换一个字符                 {                     check(str,dict[i]);                 }                 else if(len[i]-len1==1) //待测单词长度短1个,就添加一个字符                 {                     Add(str,dict[i]);                 }                 else if(len1-len[i]==1) //待测单词长度长1个,就删除一个字符                {                     delect(str,dict[i]);                }            }          cout<<endl;         }     }     return 0; }

你可能感兴趣的:(poj 1035 Spell checker(字符串处理))