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. Linux中线程的挂起与恢复(进程暂停)

    http://www.linuxidc.com/Linux/2013-09/90156.htm 今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制.也没有完整 ...

  2. VMware虚拟机中为Linux 添加虚拟硬盘(VirtualBox方法类似)

    修改1:2014-06-24 11:38:21 Linux添加硬盘是在原来安装的硬盘空间不够或者需要使用其他硬盘上的东西时候的解决办法,因为大多数初学者习惯使用虚拟机,这里以在Vmware虚拟机中实现 ...

  3. 关于在.NET中 DAL+IDAL+Model+BLL+Web

    其实三层架构是一个程序最基本的 在.Net开发中通常是多层开发比如说    BLL 就是business Logic laywer(业务逻辑层) 他只负责向数据提供者也就是DAL调用数据 然后传递给 ...

  4. 纯C实现面向对象之接口编程

    创建如下文件目录 : Shape.h #include <stdlib.h> //接口 #ifndef Interface #define Interface struct #endif ...

  5. Linux学习笔记 (八)Shell概述

    一.什么是Shell? Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动,挂起,停止甚至是编写一些程序.Shell还是一 ...

  6. 页面找不到js方法的原因,关于EasyUI

    有时EasyUI中datagride写法不正确,会导致无法加载页面上其他的js方法.datagride中的逗号是一个也不能多.一定要注意: 例如以下代码中标红的逗号就会导致后边的js不能正常加载. c ...

  7. 使用CAsyncSocket总结

    最近想起CAsyncSocket这个类,记得很早以前用过,现在却想不起来怎么用了,翻了翻以前的代码又看了看msdn感觉这个类做简单的异步socket太简单了,几行代码就可以搞定,在此先做个总结. 不管 ...

  8. ibatis中<![CDATA[使用解释

    http://hi.baidu.com/taoxincheng0/blog/item/3916c4ec413f03c22e2e2160.html ibatis中什么时候需要用到: <![CDAT ...

  9. 通过xsd schema结构来验证xml是否合法

    import sys import StringIO import lxml from lxml import etree from StringIO import StringIO # Constr ...

  10. Android应用TranslateAnimation移动之后,利用视图的setLayoutPara

    Android中利用TranslateAnimation移动时,不设置mTranslateAnimation.setFillAfter(true);,而利用视图的setLayoutParams来重新定 ...