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. 连接sqlexpress

    sqlexpress在visualstudio安装时可选择安装. 数据源添加 localhost\sqlexpress window身份认证即可.

  2. USB High Speed Inter-Chip (HSIC) IP: What is it? And why should I use it?

    来源: https://www.synopsys.com/dw/dwtb.php?a=hsic_usb2_device What is HSIC? HSIC (High-Speed Inter-Chi ...

  3. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  4. iOS:使用集成的支付宝SDK的支付流程

    基本步骤: 1.先与支付宝签约,获得商户的ID(partner)和账号ID(seller),这一部分主要是又公司负责: 2.下载相应的公钥私钥文件,用来给签名进行加密: 3.下载支付宝集成的SDK,网 ...

  5. linux systemctl service examples

    一.脚本服务化目的 1.python 在 文本处理中有着广泛的应用,为了满足文本数据的获取,会每天运行一些爬虫抓取数据.但是网上买的服务器会不定时进行维护,服务器会被重启.这样我们的爬虫服务就无法运行 ...

  6. requireJS简介和一个完整实例

    什么是 requireJS ? requireJS 是用JavaScript编写的JS框架,主要功能是可以按不同的先后依赖关系对 JavaScript 等文件的进行加载工作,可简单理解为JS文件的加载 ...

  7. 【前端自动化构建 grunt、gulp、webpack】

    参考资料: 用自动化构建工具增强你的工作流程!:http://www.gulpjs.com.cn/ gulp详细入门教程:http://www.ydcss.com/ JavaScript构建(编绎)系 ...

  8. POJ3264 Balanced Lineup 【线段树】+【单点更新】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 ...

  9. JUC同步器框架

    The java.util.concurrent Synchronizer Framework 前提 AQS(java.util.concurrent.locks.AbstractQueuedSync ...

  10. ajax 底层源码解析

    对象: XMLHttpRequest 属性:readyState请求状态,开始请求时值为0直到请求完成这个值增长到4 responseText目前为止接收到的响应体,readyState<3此属 ...