当前位置: 代码迷 >> 综合 >> POJ 1035 Spell checker 【字符串】
  详细解决方案

POJ 1035 Spell checker 【字符串】

热度:93   发布时间:2023-11-11 10:56:21.0

题目传送门:点击打开链接

Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25934   Accepted: 9503

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:替换该单词中的一个字母    2:删去该单词中的一个字母   3:加上一个字母   如果存在相等的单词,则输出correct



#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
char ans[20];
struct ssr
{char re[20];int len;
} input[10005];bool deal(char str1[],char str2[])
{int len1=strlen(str1);int len2=strlen(str2);int temp=0;if(len1==len2)///满足替换一个单词{for(int i=0; i<len1; i++){if(str1[i]==str2[i]){temp++;}}if(temp==len1-1){return 1;}else{return 0;}}else if(len1<len2)///满足加上一个单词{for(int i=0; i<len2; i++){if(str1[temp]==str2[i]){temp++;}}if(temp==len1){return 1;}else{return 0;}}else///满足删去一个单词{for(int i=0; i<len1; i++){if(str1[i]==str2[temp]){temp++;}}if(temp==len2){return 1;}else{return 0;}}
}int main()
{M(ans,0);M(input,0);int flag=0;while(~scanf("%s",input[flag].re)){if(input[flag].re[0]=='#'){break;}input[flag].len=strlen(input[flag].re);flag++;}while(~scanf("%s",ans)){if(ans[0]=='#'){break;}int len2=strlen(ans);int flag2=0;for(int i=0; i<flag; i++){if(strcmp(ans,input[i].re)==0)///查找到完全相等的{printf("%s is correct\n",ans);flag2=1;break;}}if(flag2){continue;}else{printf("%s:",ans);for(int i=0; i<flag; i++){if(len2==input[i].len-1||len2==input[i].len+1||len2==input[i].len)///在长度上满足题目所给的三个条件{if(deal(ans,input[i].re)){printf(" %s",input[i].re);}}}printf("\n");}}return 0;
}