Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22541   Accepted: 8220

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
用单词的长度做hash,刚开始用C++写超时,后改成C便AC了。
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *dic[];
char dirc[][][];
int rank1[];
int rank2[][];
int cmp ( const void *a , const void *b)
{
return *(int *)a - *(int *)b;
}
void findRight(char* str){
int i=strlen(str);
int flag=;
int result[];
memset(result,,sizeof(result));
int dsize;
int count=;
dsize=rank1[i];
for(int k=;k<dsize;k++){
char* str2=dirc[i][k];
int diff=;
for(int m=;m<i;m++){
if(str[m]!=str2[m]){
diff++;
}
}
if(diff==)
result[count++]= rank2[i][k];
else if(diff==){
flag=;
break;
}
}
if(!flag){
dsize=rank1[i-];
for(int k=;k<dsize;k++){
char* str2=dirc[i-][k];
int diff=;
int m=,n=;
for(;n<i&&m<i-;){
if(str[n]!=str2[m]){
diff++;
if(diff>=)
break;
n++;
}else{
m++;
n++;
} }
diff+=strlen(str2)-m;
if(diff<=)
result[count++]=rank2[i-][k];
}
dsize=rank1[i+];
for(int k=;k<dsize;k++){
char* str2=dirc[i+][k];
int diff=;
int m=,n=;
for(;n<i&&m<i+;){ if(str[n]!=str2[m]){
diff++;
if(diff>=)
break;
m++;
}else{
m++;
n++;
} }
diff+=i-n;
if(diff<=)
result[count++]=rank2[i+][k];
} }
if(flag)
printf("%s is correct\n",str);
else{
if(count>)
qsort(result,count,sizeof(int),cmp);
printf("%s: ",str);
for(int k=;k<count;k++){
printf("%s ",dic[result[k]]);
}
printf("\n");
} } int main() {
char word[];
int r=;
memset(rank1,,sizeof(rank1));
while(gets(word)){
if(word[]=='#')
break;
int len=strlen(word);
strcpy(dirc[len][rank1[len]],word);
dic[r]=dirc[len][rank1[len]];
rank2[len][rank1[len]]=r;
r++;
rank1[len]++;
}
while(gets(word)){
if(word[]=='#')
break;
findRight(word);
}
return ;
}

下面是用C++写的超时版本

 #include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> directory[];
vector<string> dir;
vector<int> rank[]; vector<string> input[];
void findRight(string str){
int i=str.length();
bool flag=;
vector<int> result;
int dsize;
dsize=directory[i].size();
for(int k=;k<dsize;k++){
string str2=directory[i][k];
int diff=;
for(int m=;m<i;m++){
if(str[m]!=str2[m]){
diff++;
}
}
if(diff==)
result.push_back(rank[i][k]);
else if(diff==){
flag=;
break;
}
}
if(!flag){
dsize=directory[i-].size();
for(int k=;k<dsize;k++){
string str2=directory[i-][k];
int diff=;
int m=,n=;
for(;n<i&&m<i-;){
if(str[n]!=str2[m]){
diff++;
if(diff>=)
break;
n++;
}else{
m++;
n++;
} }
diff+=str2.length()-m; if(diff<=)
result.push_back(rank[i-][k]);
}
dsize=directory[i+].size();
for(int k=;k<dsize;k++){
string str2=directory[i+][k];
int diff=;
int m=,n=;
for(;n<i&&m<i+;){ if(str[n]!=str2[m]){
diff++;
if(diff>=)
break;
m++;
}else{
m++;
n++;
} }
diff+=str.length()-n;
if(diff<=)
result.push_back(rank[i+][k]);
} }
if(flag)
cout<<str<<" is correct"<<endl;
else{
dsize=result.size();
sort(result.begin(),result.end());
cout<<str<<": ";
for(int k=;k<dsize;k++){
cout<<dir[result[k]]<<' ';
}
cout<<endl;
} } int main() {
string str;
int r=;
while(cin>>str){
if(str=="#")
break;
directory[str.length()].push_back(str);
dir.push_back(str);
rank[str.length()].push_back(r++); }
while(cin>>str){
if(str=="#")
break;
findRight(str);
}
return ;
}

Spell checker - poj 1035 (hash)的更多相关文章

  1. Spell checker POJ 1035 字符串

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25426   Accepted: 9300 De ...

  2. poj 1035 Spell checker ( 字符串处理 )

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

  3. poj 1035 Spell checker

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

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

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

  5. POJ 1035:Spell checker

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22574   Accepted: 8231 De ...

  6. POJ 1035 代码+具体的目光

    Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19319 Accepted: 7060 Descri ...

  7. Spell checker

     Spell checker Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  8. Spell checker(暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 De ...

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

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

随机推荐

  1. python可变的参数列表

    一般的计算机语言中参数的个数是不能改变的,但是在python中实参的个数是可以改变的.主要是通过形参中的*arg和**arg来实现的,使用可变参数必须遵守下面规则: 1.位置参数必须出现在这*arg参 ...

  2. shell产生随机数七种方法

    shell实例浅谈之三产生随机数七种方法   一.问题 Shell下有时需要使用随机数,在此总结产生随机数的方法.计算机产生的的只是“伪随机数”,不会产生绝对的随机数(是一种理想随机数).伪随机数在大 ...

  3. Objective-C:Objective-C 和 Core Foundation 对象相互转换的内存管理

    Objective-C 和 Core Foundation 对象相互转换的内存管理 iOS允许Objective-C 和 Core Foundation 对象之间可以轻松的转换,拿 NSString ...

  4. pandas判断缺失值的办法

    参考这篇文章: https://blog.csdn.net/u012387178/article/details/52571725 python pandas判断缺失值一般采用 isnull(),然而 ...

  5. 使用Nmon监控Linux的系统性能

    Nmon(得名于 Nigel 的监控器)是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的一款计算机性能系统监控工具.Nmon 可以把操作系统的统计数据展示在屏幕上 ...

  6. D3.js系列——交互式操作和布局

    一.图表交互操作 与图表的交互,指在图形元素上设置一个或多个监听器,当事件发生时,做出相应的反应. 交互,指的是用户输入了某种指令,程序接受到指令之后必须做出某种响应.对可视化图表来说,交互能使图表更 ...

  7. struts2,action上传文件

    通过servlet实现文件上传,可以用用servlet接受到request的值的话:主要是这句话 List<?> items = upload.parseRequest(request); ...

  8. More is better——并查集求最大集合(王道)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  9. websocket 和 socket.io 之间的区别

    socket.io封装了websocket,同时包含了其它的连接方式,比如Ajax.原因在于不是所有的浏览器都支持websocket,通过socket.io的封装,你不用关心里面用了什么连接方式.你在 ...

  10. Intent 介绍

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...