Spell checker - poj 1035 (hash)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 22541 | Accepted: 8220 |
Description
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 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
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)的更多相关文章
- Spell checker POJ 1035 字符串
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25426 Accepted: 9300 De ...
- poj 1035 Spell checker ( 字符串处理 )
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16675 Accepted: 6087 De ...
- poj 1035 Spell checker
Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u J ...
- [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18693 Accepted: 6844 De ...
- POJ 1035:Spell checker
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22574 Accepted: 8231 De ...
- POJ 1035 代码+具体的目光
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19319 Accepted: 7060 Descri ...
- Spell checker
Spell checker Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- Spell checker(暴力)
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20188 Accepted: 7404 De ...
- POJ1035——Spell checker(字符串处理)
Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...
随机推荐
- iOS 灵活,简易,扩展性强的气泡提示框LFBubbleView(含源码)
一.效果图 二.使用方法 使用简单,4行代码集成. _bubbleView = [[LFBubbleView alloc] initWithFrame:CGRectMake(, , , )]; _bu ...
- jvm-监视管理控制台-jconsole
命令: jconsole 作用: jvm进程运行状态的实时.可视化工具 效果: 连接远程jvm进程: 1.首先远程jvm进程,开启了jmx服务: -Dcom.sun.management.jmxrem ...
- JAVA 按时间排序
排序使用的是 Collections.sort(List,Comparator) 自定义类实现Comparator接口 假如A的值大于B,你返回1.这样调用Collections.sort()方法就是 ...
- 再谈 Promise
读完这篇文章,预计会消耗你 40 分钟的时间. Ajax 出现的时候,刮来了一阵异步之风,现在 Nodejs 火爆,又一阵异步狂风刮了过来.需求是越来越苛刻,用户对性能的要求也是越来越高,随之而来的是 ...
- python字符串转日期
需要两步 为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型 1.字符串与time类型的转换 >>> import time>> ...
- 使用Python实现生产者消费者问题
之前用C++写过一篇生产者消费者的实现. 生产者和消费者主要是处理互斥和同步的问题: 队列作为缓冲区,需要互斥操作 队列中没有产品,消费者需要等待,直到生产者放入产品并通知它.队列慢的情况类似. 这里 ...
- JavaScript--百度百科
JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...
- Python趣味实用小工具
代码地址如下:http://www.demodashi.com/demo/12918.html python 趣味实用小工具 概述 用python实现的三个趣味实用小工具: 图片转Execl工具 , ...
- (一)Shiro笔记——简介、 架构分析
1. Shiro是什么 Apache Shiro是一个强大灵活的开源安全框架,可以完全处理身份验证,授权,企业会话管理和加密. Apache Shiro的首要目标是易于使用和理解. 安全有时可能非常复 ...
- python学习笔记之pdb调试
之前一直说要学python可还是一直停留在看的层面,昨天大神手把书教我pdb调试,说要摆脱IDE集成开发环境编程,感激不尽,立一个flag,python一定要入门! 1.进入方式 1)windows ...