POJ 1035 Spell checker(串)
题目网址:http://poj.org/problem?id=1035
思路:
看到题目第一反应是用LCS ——最长公共子序列 来求解。因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O(n*m),n,m分别对应两个字符串的长度。还要乘上字典的个数和所要匹配的单词数,不出意外地。。超时了。
后面就想到了暴力求解,直接枚举所有情况,先判断字符串长度差是否大于1,大于1的话直接进行下一个循环,否则再继续划分。(len对应字典词长度,l对应要查询的词长度)
假设匹配成功,只会有以下三种情况。
1. len==l ,只可能是完美匹配,或是替换一个字母
2. len-1==l,查询的词要添加一个字母
3. len==l-1,查询的词要删去一个字母
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
char word[][];
char str[];
int main(){
int cnt=-;
while (gets(word[++cnt])!=NULL && word[cnt][]!='#');
while (gets(str)!=NULL && str[]!='#') {
vector<int>v;
int ok=;
int l=(int)strlen(str);
printf("%s",str);
for (int i=; i<cnt; i++) {
int len=(int)strlen(word[i]);
int cur=,j=,k=;
if(fabs(len-l)>) continue;
if(len-l==){//情况1
while (str[j] && word[i][j] && cur<=) {
if (str[j]!=word[i][j]) cur++;
j++;
}
if(cur==){
ok=;
printf(" is correct\n");
break;
}
}else if(len-l==){//情况2
while (str[k] && word[i][j]) {
if(str[k]!=word[i][j]){
cur++,j++;
continue;
}
j++;k++;
}
}else if(l-len==){//情况3
while (str[k] && word[i][j]) {
if(str[k]!=word[i][j]){
cur++,k++;
continue;
}
j++;k++;
}
}
if(cur<=) v.push_back(i); }
if(!ok){
printf(":");
for (int j=; j<v.size(); j++) {
printf(" ");
printf("%s",word[v[j]]);
}
printf("\n");
}
}
return ;
}
另附上用LCS做的超时版本。。有用LCS过的 欢迎讨论
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
char word[][];
char str[];
int main(){
int cnt=-;
while (gets(word[++cnt])!=NULL && word[cnt][]!='#');
while (gets(str)!=NULL && str[]!='#') {
vector<int>v;
int ok=;
int l=(int)strlen(str);
printf("%s",str);
for (int i=; i<cnt; i++) {
int lcs[][];
int Max=;
int len=(int)strlen(word[i]);
if(fabs(len-l)>) continue;
memset(lcs, , sizeof(lcs));
for (int j=; j<len; j++) {
for (int k=; k<l; k++) {
if(word[i][j]==str[k]) lcs[j+][k+]=lcs[j][k]+;
else lcs[j+][k+]=max(lcs[j+][k], lcs[j][k+]);
Max=max(Max, lcs[j+][k+]);
}
}
if(max(len,l)==Max){//完全匹配
ok=;
printf(" is correct\n");
break;
}else if(max(len,l)==Max+){//增删改其中的一个情况
if(len==Max || l==Max){//增删情况
v.push_back(i);
}else{
int j=;
int cur=;
while (word[i][j] && str[j]) {
if(word[i][j]==str[j]) cur++;
j++;
}
if(cur==Max){//修改一个的情况
v.push_back(i);
}
}
}
}
if(!ok){
printf(":");
for (int j=; j<v.size(); j++) {
printf(" ");
printf("%s",word[v[j]]);
}
printf("\n");
}
}
return ;
}
POJ 1035 Spell checker(串)的更多相关文章
- poj 1035 Spell checker
Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u J ...
- poj 1035 Spell checker ( 字符串处理 )
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16675 Accepted: 6087 De ...
- [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18693 Accepted: 6844 De ...
- POJ 1035 Spell checker 字符串 难度:0
题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...
- poj 1035 Spell checker(水题)
题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...
- poj 1035 Spell checker(hash)
题目链接:http://poj.org/problem?id=1035 思路分析: 1.使用哈希表存储字典 2.对待查找的word在字典中查找,查找成功输出查找成功信息 3.若查找不成功,对word增 ...
- POJ 1035 Spell checker (模拟)
题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...
- POJ 1035 Spell checker 简单字符串匹配
在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...
- POJ训练计划1035_Spell checker(串处理/暴力)
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18418 Accepted: 6759 De ...
随机推荐
- sql字段为逗号分开的字符串值的关联查询
1.TREE表: [strID] [int] IDENTITY(1,1) NOT NULL,[strName] [nvarchar](50) NOT NULL, 2.SubInfo CREATE TA ...
- IDEA微服务项目的application.yml没有绿色叶子的解决办法
1.今天在写微服务项目的时候成功入坑,那么问题是啥呢?接下来和我一起走入bug的世界吧,让我们看看究竟是怎么回事. *问题描述 1.application.yml是灰色的小格子 2.实在难看 *需要解 ...
- github博客Hexo引流到微信
相信有不少小伙伴都在github上创建了属于自己的博客,其中用Hexo的Next主题应该不少,那么,我们究竟该如何将博客的流量引流到微信呢?今天就来带你看一看. 如何引流 现在网上有一种套路,当你在看 ...
- mybatis中Insert后主键返回
1.Mapper的写法,返回的这个int是受影响的行号 int insertNewUser(User newUser); 2.xml的写法 <!--返回主键 形式1 --> <ins ...
- 格子游戏Grid game CodeForce#1104C 模拟
题目链接:Grid game 题目原文 You are given a 4x4 grid. You play a game — there is a sequence of tiles, each o ...
- jquery的api以及用法总结-属性/css/位置
属性/css 属性 .attr() attr()设置普通属性,prop()设置特有属性 获取或者设置匹配的元素集合中的第一个元素的属性的值 如果需要获取或者设置每个单独元素的属性值,需要依靠.each ...
- SSH Config 管理多主机
使用 一般我们使用ssh连接远程主机的时候,使用命令是: ssh root@ip ssh –i [identity-file] -p [port] user@hostname 但是如果ip地址过多,其 ...
- 利用sqlalchemy 查询视图
这个问题 google 百度 中英文搜了一上午.最新的回答还是 7年前.最后自己靠着官方文档的自己改出来一个比较方便的方法 使用环境 python == 3.7.0 SQLAlchemy === 1. ...
- JsonConfig的jsonConfig.setExcludes的用法
1.问题描述 在项目中经常会有两个类存在一对多或者多对一的关联关系,这样在查询多的一方时,会深入查询关联的一方,而我们可能并不需要去深入查询那些数据,此时使用JsonConfig的jsonConfig ...
- 【ASP.NET 基础】WCF入门教程一(什么是WCF)?
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...