POJ 一 1035 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

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
Description:
        
        本题大意是你是一个开发新型拼写检测程序组的程序员一枚,现如今要求你负责其中的一个模块来检测给出的words是否都在给定的dictionary中,并以正确的形式出现.
        说一个word在dictionary中出现,当这个word满足一下条件的时候:
1.word不经修改就与dictionary中的某个单词匹配
2.删除word中的一个字母使得变动后的word与dictionary中的某个单词匹配
3.替换word中的一个字母使得变动后的word与dictionary中的某个单词匹配
4.插入一个字母到word中使得变动后的word与dictionary中的某个单词匹配


输入:
输入的第一部分代表的是字典中的所有的单词,(dictionary)
每行一个单词并且以#代表结束


第二部分代表的是需要与字典中匹配的单词(word)
同样也是以#结束

输出

1.如果单词(word)未经任何修改就与dictionary中的单词匹配
那么输出:(word)is correct
2.如果单词(word)经过增、删、替换等修改后与dictionary中的单词匹配
那么输出:(word)+:+空格+所有在字典dictionary中出现的经过变换后与之匹配的单词(而并非原单词),单词之间以空格分隔


3.如果word经过一系列变化后并未在字典中出现预支匹配的单词,
那么输出单词(word)+:


做的时候,思路太乱,没有理清思路就动手做,导致错的一塌糊涂。
写出的代码比较字符串的思想很新颖,通过挨个比较字符来比较字符串
这样做很大程度上降低了题目难度;
以后遇到难题,一定要理清思路再动手。
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

bool change(char* a,char* b);
bool del(char* a,char* b);
bool rep(char* a,char* b);
bool add(char* a,char* b);

int main(void)
{
    int  dicnum,wordnum,diclen[10005];
    char dic[10005][20],word[55][20];

    
    dicnum=0;
    while(scanf("%s",dic[dicnum]))
    {
     if(dic[dicnum][0]=='#')
     break;
     dicnum++;                         
    }
    wordnum=0;
    while(scanf("%s",word[wordnum]))
    {
     if(word[wordnum][0]=='#')
     break;
     wordnum++;
    }
    for(int i=0;i<dicnum;i++)
    diclen[i]=strlen(dic[i]);
    for(int i=0;i<wordnum;i++)
    {
     bool flag=false;
     int index[10005],jindex=0;
     int len=strlen(word[i]);
     for(int j=0;j<dicnum;j++)
     {
      if(len==diclen[j])
      {
       if(strcmp(word[i],dic[j])==0)
       {
        flag=true;
        break;
       }      
       else if(change(word[i],dic[j])) 
       {index[jindex++]=j;}           
      } 
      else if(diclen[j]-len==1)
      {
       if(add(word[i],dic[j]))
       index[jindex++]=j;
      }
      else if(len-diclen[j]==1)
      {
       if(del(word[i],dic[j]))
       index[jindex++]=j;     
      } 
     }
     if(flag)
      printf("%s is correct\n",word[i]);      
     else
     {
      printf("%s: ",word[i]);
      for(int k=0;k<jindex;k++)
      printf("%s ",dic[index[k]]);
      printf("\n");    
     }        
    } 
    return 0;
}

bool change(char* a,char* b)
{
     int flag=0;
     while(*a)
     {
      if(*(a++)!=*(b++))
      flag++;
      if(flag>1)
      return false;
     }
     return true;
}

bool del(char* a,char* b)
{
     int flag=0;
     while(*a)
     {
      if(*a!=*b)
      {
       flag++;
       a++;     
       if(flag>1)
       return false;
      }
      else
      a++,b++;  
     }
     return true;
}

bool add(char* a,char* b)
{
     int flag=0;
     while(*b)
     {
      if(*a!=*b)
      {
       flag++;
       b++;      
       if(flag>1)
       return false;
      }  
      else
      a++,b++;
     }
     return true;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值