码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1035 -- Spell checker

时间:2014-07-19 22:21:14      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18712   Accepted: 6856

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

字符串的题目,给一个词典,然后判断输入的单词是否在词典中,如果不在,则找长度减一/加一/正好/中的单词且满足只有一个字母不一样,
第一次用LSC做的,可能姿势不对,超时了,直接暴力能过。

bubuko.com,布布扣
  1 /*======================================================================
  2  *           Author :   kevin
  3  *         Filename :   SpellChecker1.cpp
  4  *       Creat time :   2014-07-19 13:09
  5  *      Description :
  6 ========================================================================*/
  7 #include <iostream>
  8 #include <algorithm>
  9 #include <cstdio>
 10 #include <cstring>
 11 #include <queue>
 12 #include <cmath>
 13 #define clr(a,b) memset(a,b,sizeof(a))
 14 #define M 10005
 15 using namespace std;
 16 char words[M+10][20];
 17 int cnt;
 18 void init()
 19 {
 20     cnt = 0;
 21     while(scanf("%s",words[cnt])){
 22         if(strcmp(words[cnt],"#") == 0){
 23             break;
 24         }
 25         else cnt++;
 26     }
 27 }
 28 bool judge1(char a[],char b[],int len1,int len2)
 29 {
 30     int cnt = 0;
 31     for(int i = 0; i < len1; i++){
 32         if(a[i] != b[i])
 33             cnt++;
 34     }
 35     if(cnt == 1) return true;
 36     return false;
 37 }
 38 bool judge2(char a[],char b[],int len1,int len2)
 39 {
 40     int flag = 0;
 41     for(int i = 0; i < len1; i++){
 42         flag = 1;
 43         for(int j = 0,k = 0; j < len1 && k < len2; ){
 44             if(j == i){
 45                 j++; continue;
 46             }
 47             if(a[j] != b[k]){
 48                 flag = 0;
 49             }
 50             j++; k++;
 51         }
 52         if(flag) return true;
 53     }
 54     return false;
 55 }
 56 bool judge3(char a[],char b[],int len1,int len2)
 57 {
 58     int flag;
 59     for(int i = 0; i < len2; i++){
 60         flag = 1;
 61         for(int j = 0,k = 0; j < len1 && k < len2; ){
 62             if(k == i){
 63                 k++; continue;
 64             }
 65             if(a[j] != b[k])
 66                 flag = 0;
 67             j++; k++;
 68         }
 69         if(flag) return true;
 70     }
 71     return false;
 72 }
 73 int main(int argc,char *argv[])
 74 {
 75     init();
 76     char str[20];
 77     while(scanf("%s",str)){
 78         if(strcmp(str,"#") == 0){
 79             break;
 80         }
 81         int flag = 0;
 82         for(int i = 0; i < cnt; i++){
 83             if(strcmp(words[i],str) == 0){
 84                 printf("%s is correct\n",str);
 85                 flag = 1;
 86                 break;
 87             }
 88         }
 89         if(!flag){
 90             printf("%s:",str);
 91             int len1 = strlen(str);
 92             for(int i = 0; i < cnt; i++){
 93                 int len2 = strlen(words[i]);
 94                 if(len1 == len2){
 95                     if(judge1(words[i],str,len2,len1)){
 96                         printf(" %s",words[i]);
 97                     }
 98                 }
 99                 if(len1+1 == len2){
100                     if(judge2(words[i],str,len2,len1)){
101                         printf(" %s",words[i]);
102                     }
103                 }
104                 if(len1 == len2+1){
105                     if(judge3(words[i],str,len2,len1)){
106                         printf(" %s",words[i]);
107                     }
108                 }
109             }
110             printf("\n");
111         }
112     }
113     return 0;
114 }
View Code

poj 1035 -- Spell checker,布布扣,bubuko.com

poj 1035 -- Spell checker

标签:des   style   blog   http   color   strong   

原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3855107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!