poj 1035 -- Spell checker
2014-07-19 15:57
363 查看
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
Sample Output
View Code
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18712 | Accepted: 6856 |
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 字符串的题目,给一个词典,然后判断输入的单词是否在词典中,如果不在,则找长度减一/加一/正好/中的单词且满足只有一个字母不一样, 第一次用LSC做的,可能姿势不对,超时了,直接暴力能过。
/*====================================================================== * Author : kevin * Filename : SpellChecker1.cpp * Creat time : 2014-07-19 13:09 * Description : ========================================================================*/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #define clr(a,b) memset(a,b,sizeof(a)) #define M 10005 using namespace std; char words[M+10][20]; int cnt; void init() { cnt = 0; while(scanf("%s",words[cnt])){ if(strcmp(words[cnt],"#") == 0){ break; } else cnt++; } } bool judge1(char a[],char b[],int len1,int len2) { int cnt = 0; for(int i = 0; i < len1; i++){ if(a[i] != b[i]) cnt++; } if(cnt == 1) return true; return false; } bool judge2(char a[],char b[],int len1,int len2) { int flag = 0; for(int i = 0; i < len1; i++){ flag = 1; for(int j = 0,k = 0; j < len1 && k < len2; ){ if(j == i){ j++; continue; } if(a[j] != b[k]){ flag = 0; } j++; k++; } if(flag) return true; } return false; } bool judge3(char a[],char b[],int len1,int len2) { int flag; for(int i = 0; i < len2; i++){ flag = 1; for(int j = 0,k = 0; j < len1 && k < len2; ){ if(k == i){ k++; continue; } if(a[j] != b[k]) flag = 0; j++; k++; } if(flag) return true; } return false; } int main(int argc,char *argv[]) { init(); char str[20]; while(scanf("%s",str)){ if(strcmp(str,"#") == 0){ break; } int flag = 0; for(int i = 0; i < cnt; i++){ if(strcmp(words[i],str) == 0){ printf("%s is correct\n",str); flag = 1; break; } } if(!flag){ printf("%s:",str); int len1 = strlen(str); for(int i = 0; i < cnt; i++){ int len2 = strlen(words[i]); if(len1 == len2){ if(judge1(words[i],str,len2,len1)){ printf(" %s",words[i]); } } if(len1+1 == len2){ if(judge2(words[i],str,len2,len1)){ printf(" %s",words[i]); } } if(len1 == len2+1){ if(judge3(words[i],str,len2,len1)){ printf(" %s",words[i]); } } } printf("\n"); } } return 0; }
View Code
相关文章推荐
- POJ1035——Spell checker
- poj 1035 Spell checker
- POJ-1035-Spell checker
- Poj 1035 Spell checker
- poj 1035 Spell Checker
- POJ 1035 Spell checker
- poj1035 Spell checker
- POJ 1035 Spell checker
- poj 1035 Spell checker
- poj 1035 Spell checker
- poj1035——Spell checker
- poj 1035 Spell checker
- poj 1035 Spell checker
- poj 1035 Spell checker
- poj 1035 Spell checker
- POJ 1035 Spell checker
- [POJ_1035]Spell checker
- POJ 1035 Spell checker
- POJ 1035 Spell checker
- poj 1035 Spell checker