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 ...
随机推荐
- sping boot 入门
http://www.cnblogs.com/ityouknow/p/5662753.html http://blog.csdn.net/lxhjh/article/details/51711148 ...
- ubuntu16.04_install_saltstack_更新版本
一.ubuntu16.04版本,安装saltstack 参考官方文档: https://repo.saltstack.com/#ubuntu 二.安装方法 Installs the latest re ...
- linux sudo使用学习记录
sudo在linux中非常重要,它能够使普通的用户临时拥有root权限.但是如果让用户滥用sudo命令的话可能会造成严重的影响. 例如:修改root的密码,切换到root用户等等. 所以我们虽然需要赋 ...
- OS中处理机调度模型和调度算法
OS中处理机调度模型和调度算法 调度层次 1.1. 高级调度(长程调度,作业调度) 功能:依据某种算法.把在外存队列上处于后备队列的那些作业调入内存.以作业为操做对象. 作业:比程序更为广泛的概念,不 ...
- shell中单引号、双引号、反引号的区别
'单引号' 忽略所有特殊字符 "双引号" 忽略大部分特殊字符,除了$ ` `反引号` 输出执行结果
- Python标准库 (pickle包,cPickle包)
在之前对Python对象的介绍中 (面向对象的基本概念,面向对象的进一步拓展),我提到过Python“一切皆对象”的哲学,在Python中,无论是变量还是函数,都是一个对象.当Python运行时,对象 ...
- 怎样优化cocos2d/x程序的内存使用和程序大小
再次感谢原创者:Steffen Itterheim.原创博客原文地址: http://www.learn-cocos2d.com/2012/11/optimize-memory-usage-bundl ...
- 可伸缩Web架构与分布式系统(1)
开源软件近年来已变为构建一些大型网站的基础组件.并且伴随着网站的成长,围绕着它们架构的最佳实践和指导准则已经显露.这篇文章旨在涉及一些在设计大型网站时需要考虑的关键问题和一些为达到这些目标所使用的组件 ...
- Github——Git设置及GitHub的使用
把github上的help. First : 安装:ubuntu 下,终端输入命令: sudo apt-get install git-core git-gui git-doc Next : 设置SS ...
- html5表单验证(Bootstrap)
html5表单验证(Bootstrap) 邮箱验证: <input name="email" type="text" placeholder=&quo ...