poj 1035 Spell checker
Spell checker
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13967 | Accepted: 5136 |
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.
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.
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
这道题是一道处理字符串的题目,题意还是比较明确的,我们主要就是看是否有相同的,还是需要删一个,加一个,还是改一个,这四种分别是四种操作,可以写成
4个函数,分别书写,这样思路就比较清晰了,然后关键是要注意细节的处理,包括移动字母时该从哪里移动到哪里,还有什么时候要打空格和回车等。总之该题是一道
非常需要耐心和细心的模拟题。
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; bool del(char x[],char y[],int len1,int len2); bool change(char x[],char y[],int len1,int len2); bool insert(char x[],char y[],int len1,int len2); bool flag; int main() { int i; char begin[10010][20]; char end[55][20]; int len1[10010],len2[55]; i=0; scanf("%s",begin[i]); int num1=0; int num2=0; while(begin[i][0]!='#') { len1[i]=strlen(begin[i]); num1++; i++; scanf("%s",begin[i]); } i=0; scanf("%s",end[i]); while(end[i][0]!='#') { len2[i]=strlen(end[i]); num2++; i++; scanf("%s",end[i]); } int j; int k; bool first=true; for(i=0;i<num2;i++) { first=true; flag=false; bool second; for(j=0;j<num1;j++) { if(len2[i]==len1[j]) second=change(begin[j],end[i],len1[j],len2[i]); if(flag==true) {printf("%s is correct\n",end[i]);break;} } if(flag==true) continue; for(j=0;j<num1;j++) { if(len2[i]-len1[j]==1) if(del(begin[j],end[i],len1[j],len2[i])) if(first) {printf("%s: %s",end[i],begin[j]);first=false;} else {printf(" %s",begin[j]);} if(len2[i]==len1[j]) if(change(begin[j],end[i],len1[j],len2[i])) if(first) {printf("%s: %s",end[i],begin[j]);first=false;} else {printf(" %s",begin[j]);} if(len1[j]-len2[i]==1) if(insert(begin[j],end[i],len1[j],len2[i])) if(first) {printf("%s: %s",end[i],begin[j]);first=false;} else {printf(" %s",begin[j]);} } if(first && flag==false) printf("%s:\n",end[i]); else printf("\n"); } system("pause"); return 0; } bool del(char x[],char y[],int len1,int len2) { int i; char xc[35],yc[35]; strcpy(xc,x); strcpy(yc,y); for(i=0;i<len1;i++) if(xc[i]!=yc[i]) break; if(i==len1) return true; while(i<len1) { yc[i]=yc[i+1]; i++; } for(i=0;i<len1;i++) if(xc[i]!=yc[i]) break; if(i!=len1) return false; else return true; } bool change(char x[],char y[],int len1,int len2) { int i; char xc[35],yc[35]; strcpy(xc,x); strcpy(yc,y); for(i=0;i<len1;i++) if(xc[i]!=yc[i]) {yc[i]=xc[i];break;} if(i==len1) {flag=true;return false;} for(i=1;i<len1;i++) if(xc[i]!=yc[i]) break; if(i!=len1) return false; else return true; } bool insert(char x[35],char y[35],int len1,int len2) { int i; char xc[35],yc[35]; strcpy(xc,x); strcpy(yc,y); for(i=0;i<len1;i++) if(xc[i]!=yc[i]) break; int j; if(i!=len2) for(j=len2-1;j>=i;j--) yc[j+1]=yc[j]; yc[i]=xc[i]; for(i=0;i<len1;i++) if(xc[i]!=yc[i]) break; if(i!=len1) return false; else return true; }