Spell checker 字典树&&普通查找(加计数)
2013-10-30 19:46
162 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
[align=left]Sample Output[/align]
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
***************************************************************************************************************************
字典树
***************************************************************************************************************************
View Code
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
[align=left]Sample Output[/align]
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
***************************************************************************************************************************
字典树
***************************************************************************************************************************
#include<iostream> #include<string> #include<cstring> #include<cstdio> #include<cmath> using namespace std; char str1[10101][55],str2[55]; int i,j; class node { public: node *p[26]; int it; bool de; node() { for(it=0;it<26;it++) p[it]=NULL; de=false;; } }; node *root; void get_num(char*s) { node*r=root; int len1=strlen(s); int it; for(it=0;it<len1;it++) { if(r->p[s[it]-'a']==NULL) { r->p[s[it]-'a']=new node(); } r=r->p[s[it]-'a']; } r->de=true; } bool find(char *s) { int len2=strlen(s); int it; node *r=root; for(it=0;it<len2;it++) { if(r->p[s[it]-'a']==NULL) return false; r=r->p[s[it]-'a']; } if(r->de) return true; return false; } bool solve(char s1[],char s2[]) { int len1=strlen(s1); int len2=strlen(s2); int ncount=0; if(len1!=len2) { int it=0,jt=0; while(it<len1&&jt<len2) { if(s1[it]!=s2[jt]) { ncount++; if(len1>len2) { it++; } else jt++; } else { it++; jt++; } } if(ncount>1) return false; else return true; } else { int it=0; while(it<len1) { if(s1[it]!=s2[it]) ncount++; it++; } if(ncount==1) return true; else return false; } } int main() { int st=0; root=new node(); while(scanf("%s",str1[st])&&str1[st][0]!='#') { get_num(str1[st]); st++; } while(scanf("%s",str2)&&str2[0]!='#') { if(find(str2))//判断是否有 printf("%s is correct\n",str2); else { printf("%s:",str2); for(i=0;i<st;i++) { int temp=strlen(str1[i])-strlen(str2); if(temp<=1&&temp>=-1) if(solve(str1[i],str2))//判断是否有一个不同或可替换 printf(" %s",str1[i]); } printf("\n"); } } return 0; }
View Code
相关文章推荐
- poj 3295 Tautology && poj 1035 Spell checker 【字典树】
- POJ 1035 Spell checker 查找单词
- 二维数组查找(普通数组&vector数组)
- poj1035 spell checker (简单的字符串查找题)
- [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)
- ZOJ3380- Patchouli's Spell Cards(概率DP+计数)
- POJ 1035 Spell checker 【字符串暴力查找】
- linux下查找当前目录下,所以包含&quot;&lt;s3c2410.h&gt;&quot;的普通文件
- [ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)
- leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)
- 【数据结构】算法9.17-9.18 哈希表-插入&查找
- 查找链表的中点if(fast!= NULL && fast->next != NULL) { //要用并运算,1->2中slow不能跳,所以要并运算fast->next != NULL
- Google 面试题 第K小的数字 二分逼近&二分查找
- 二分·二分查找之k小数
- [BZOJ 1005] 明明的烦恼 && [BZOJ 1211] 树的计数【组合数学】
- 二分查找求函数的区间最小值&&http://acm.hdu.edu.cn/showproblem.php?pid=2899
- POJ 1035 Spell checker (trie树)
- POJj1035 Spell checker 快排+二分+字符串处理
- 在一个二叉排序树(BST)(left<right)中查找某个节点的后序节点
- [bzoj3343]&&[洛谷2810] [分块][块内排序][二分查找]教主的魔法