Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16675   Accepted: 6087

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

Source

 
 
题意:给一个字典,给一些词。判断这些词是否拼写正确。就是如果在字典里找到了这个词就是正确,输出" %s(这个词) is correct"。如果没找到,就判断字典里有没有
           和这个词接近的,输出这些词。接近有三层意思:1、可以是在这个词里增加任意一个字母,判断字典里是否存在这个新单词。2、可以是去掉任意一个字母,判断字
           典里是否存在这个新单词。3、可以是任意改变其中的一个字母,找字典里有无这个新单词。碰到接近的词就记录下来,这是输入中词可能拼错的正确写法。按题意
           输出即可。(就是个判词系统)
分析:模拟+暴力!增加一个字母判断有无这个词,前提是这个词比字典里正确的词少一个字母且其余的字母都相同。同理,减少一个字母,前提这个词比字典里正确的词
           多一个字母且其余的字母都相同。改动一个词,要求待判断的词与正确的词长度相同且只有一个字母不一样。在字典里找得到输入词的情况直接用strcmp()。
感想:输入的写法需要注意。用new动态内存分配不怎么熟悉啊。。。。囧~!!!还有要记得delete释放空间。。
 
据说这题还可以用字典树写。可惜我还要先学 “字典树”~!
 
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; char dict[10005][20];
char word[55][20];
int dictnum,wordnum; void Input()
{
while(scanf("%s",&dict[dictnum])&&dict[dictnum++][0]!='#');
while(scanf("%s",&word[wordnum])&&word[wordnum++][0]!='#');
dictnum--; //剔除“#”
wordnum--;
} bool Change(char *dict,char *word)
{
int dif=0;
while(*word)
{
if(*(word++)!=*(dict++))
{
dif++;
if(dif>1)
return false;
}
}
return true;
} bool Add(char *dict,char *word)
{
int dif=0;
while(*dict)
{
if(*dict!=*word)
{
dict++;
dif++;
if(dif>1)
return false;
}
else
{
dict++;
word++;
}
}
return true;
} bool Del(char *dict,char *word)
{
int dif=0;
while(*word)
{
if(*word!=*dict)
{
word++;
dif++;
if(dif>1)
return false;
}
else
{
word++;
dict++;
}
}
return true;
} int main()
{
Input();
int *dictlen=new int[dictnum];
for(int i=0;i<dictnum;i++)
dictlen[i]=strlen(dict[i]);
for(int j=0;j<wordnum;j++)
{
int len=strlen(word[j]);
int *address=new int[dictnum];
int pa=0;
int flag=false; for(int k=0;k<dictnum;k++)
{
if(len==dictlen[k])
{
if(strcmp(dict[k],word[j])==0)
{
flag=true;
break;
}
else if(Change(dict[k],word[j]))
address[pa++]=k;
}
else if(len-dictlen[k]==1)
{
if(Del(dict[k],word[j]))
address[pa++]=k;
}
else if(dictlen[k]-len==1)
{
if(Add(dict[k],word[j]))
address[pa++]=k;
}
}
if(flag)
printf("%s is correct\n",word[j]);
else
{
printf("%s: ",word[j]);
for(int d=0;d<pa;d++)
printf("%s ",dict[address[d]]);
printf("\n");
}
delete address;
}
return 0;
}
 

11933189

fukan

1035

Accepted

424K

94MS

C++

2480B

2013-08-06 20:36:48

 

poj 1035 Spell checker ( 字符串处理 )的更多相关文章

  1. POJ 1035 Spell checker 字符串 难度:0

    题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...

  2. poj 1035 Spell checker

    Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   J ...

  3. [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 De ...

  4. POJ 1035 Spell checker(串)

    题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...

  5. poj 1035 Spell checker(水题)

    题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...

  6. poj 1035 Spell checker(hash)

    题目链接:http://poj.org/problem?id=1035 思路分析: 1.使用哈希表存储字典 2.对待查找的word在字典中查找,查找成功输出查找成功信息 3.若查找不成功,对word增 ...

  7. POJ 1035 Spell checker (模拟)

    题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...

  8. POJ 1035 Spell checker 简单字符串匹配

    在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...

  9. POJ1035——Spell checker(字符串处理)

    Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...

随机推荐

  1. merc_timer_handle_t函数的使用

    merc_timer_handle_t,是定义一个时间类型,这个时间类型可以用来接收2个函数之间的wasted time 但是在项目中出现这个情况: 因为在脚本中添加了该函数:

  2. JavaScript中变量、作用域、内存问题

    这几天,闲的没事看看JavaScript高级编程,感觉JavaScript真的很强大,尤其是采用面向对象的编程方式. 一.   基本类型和引用类型的值: ECMAScript变量可能包含两种不同数据类 ...

  3. OOD沉思录 --- 类和对象的关系 --- 使用关系

    使用关系 对象A的方法MethodA使用了B的方法MethodB,则表示A对B存在使用关系 使用关系的最关键问题在于,A如何找到B,存在6种方案 方案一: A包含了B,B作为一个成员定义在A的类中,那 ...

  4. jquery DOM操作(一)

    上一篇文章是记录了jquery中选择器的作用,这里只要记录下jquery中的DOM操作,还是按照上篇的格式来. 下面是测试用的html代码,接下来DOM的操作会在下面的代码下进行. <body& ...

  5. BeautifulSoup解析库

    解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差 lxml HTML解 ...

  6. 「TJOI 2018」游园会 Party

    「TJOI 2018」游园会 Party 题目描述 小豆参加了 \(NOI\) 的游园会,会场上每完成一个项目就会获得一个奖章,奖章只会是 \(N, O, I\) 的字样. 在会场上他收集到了 \(K ...

  7. luoguP3232 [HNOI2013]游走 贪心 + 概率期望 + 高斯消元

    首先,题目中的无向简单连通图代表着没有自环,重边... 总分的期望 = 每条边的期望之和...................每条边的期望又可以拆成$u \to v$的期望和$v \to u$的期望 ...

  8. ajax请求jesery接口无法获取参数的问题解决方案

    jesery是强大的RESTful api框架, 很多人在用它做web项目时会遇到这样一个问题: ajax请求jesery接口无法获取输入参数, 可明明接口已经指明了Consume是applicati ...

  9. PHP -- 配置Apache遇到的问题

    在本地电脑用XAMPP+php+mysql配置项目的时候,能够正常运行. 但是通过远程进入VPN配置的时候,配置方式一样,但是老是无法显示. 后来看了错误日志后,发现之前在没完全配置完的时候运行,生成 ...

  10. redis实现简单延时队列(转)

    继之前用rabbitMQ实现延时队列,Redis由于其自身的Zset数据结构,也同样可以实现延时的操作 Zset本质就是Set结构上加了个排序的功能,除了添加数据value之外,还提供另一属性scor ...