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
#
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

题目大意:

    给定一个字典,判断输入的数是否在字典中。

    若不在字典再判断 是否可以通过以下三个变性中的一个变成字典中的字符串。

    1)增加一个字符 2)删掉一个字符 3)改变一个字符

解题思路:

    字符串处理,先判断在不在字典中。

    不在的话再判断是否可以变性为字典中的字符串。注意判断语句的写法即可。

Code:

 /*************************************************************************
> File Name: poj1035.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月19日 星期日 15时27分59秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 1000
using namespace std;
char dic[][];
char word[];
int Num_dic;
bool Find_same(char *word)
{
bool ok=;
for (int i=;i<=Num_dic;i++)
if (strcmp(dic[i],word)==)
{
ok=;
break;
}
return ok;
}
void Find_opt(char *word)
{
for (int i=;i<=Num_dic;i++)
{
int len_word=strlen(word);
int len_dic=strlen(dic[i]);
if (len_word==len_dic)
{
int cnt=;
for (int j=;j<=len_word-;j++)
if (dic[i][j]!=word[j]) cnt++;
if (cnt==)
printf(" %s",dic[i]);
}
else if (len_word-len_dic==)
{
int k1=,k2=,cnt=;
while ()
{
if (k1==len_dic&&k2==len_word)
break;
if (cnt>=)
break;
if (dic[i][k1]==word[k2])
k1++,k2++;
else k2++,cnt++;
}
if (cnt==)
printf(" %s",dic[i]);
}
else if (len_dic-len_word==)
{
int k1=,k2=,cnt=;
while ()
{
if (k1==len_dic&&k2==len_word)
break;
if (cnt>=)
break;
if (dic[i][k1]==word[k2])
k1++,k2++;
else k1++,cnt++;
}
if (cnt==)
printf(" %s",dic[i]);
}
}
}
int main()
{
Num_dic=;
while ()
{
scanf("%s",dic[Num_dic]);
if(strcmp(dic[Num_dic],"#")==)
{
Num_dic--;
break;
}
else Num_dic++;
}
while ()
{
scanf("%s",word);
if (strcmp(word,"#")==)
break;
if (Find_same(word))
printf("%s is correct\n",word);
else
{
printf("%s:",word);
Find_opt(word);
printf("\n");
}
}
return ;
}

最新文章

  1. JSP学习网站
  2. 【hive】——metastore的三种模式
  3. Xcode 8
  4. python中常用的函数与库一
  5. Flink - state
  6. Cocoa Drawing Guide学习part1——基础和图形上下文 (转)
  7. ajax+jsp自动刷新
  8. Effective C++ 2.构造 析构 赋值运算
  9. BZOJ 4551 树
  10. Linux之一次性安装开发工具:yum groupinstall Development tools
  11. sql中truncate 、delete与drop区别
  12. ios xcode如何在控制台打印frame
  13. IOC容器 - Autofac概述
  14. C语言中的静态局部变量
  15. 在eclipse中首次新建项目的时候,出现Project interpreter not specified
  16. C++ STL源代码学习(map,set内部heap篇)
  17. vim简单命令教程-firstblood
  18. 用Python复习离散数学(一)
  19. 如何理解java是一个面向对象的语言?(转自Hollis的直面java)
  20. day 09 函数的进阶

热门文章

  1. Polyline转Polygon
  2. C++运用SDK截屏
  3. 线程模式HS/HA和L/F的区别, HS/HA的实现原理图
  4. windows 程序设计 SetPolyFillMode关于ALTERNATE、WINDING的详细解释
  5. 第三章 用SDK编译出第一个在Linux下的软件界面
  6. Hadoop fs命令详解
  7. linux的SVN搭建与同步
  8. 手机网站中 限制图片宽度 JS图片等比例缩放
  9. JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器
  10. sublime 编辑完自动生成tmp的备份