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 ...
随机推荐
- spring中基于注解使用ehcache
继续上篇,这篇介绍服务层缓存,基于注解的方式使用ehcache 注解的标签主要有4个:@Cacheable.@CacheEvict.@CachePut.@Caching,他们的用法是: @Cachea ...
- Leetcode 121.买股票的最佳时机
题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出 ...
- 记一个复杂组件(Filter)的从设计到开发
此文前端框架使用 rax,全篇代码暂未开源(待开源) 原文链接地址:Nealyang/PersonalBlog 前言 貌似在面试中,你如果设计一个 react/vue 组件,貌似已经是司空见惯的问题了 ...
- spring定时任务-文件上传进度条
spring定时任务 导依赖 <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dep ...
- springmvc(三)
Spring MVC上传 Spring MVC为文件上传提供了直接的支持,这种支持是用即插即用的MultipartResolver实现的.SpringMVC使用Apache Commons FileU ...
- FFmpeg(六) 播放视频之GLSurfaceView显示RGB数据
一.播放视频说明 1.两种方式播放视频 ①shader播放YUV,后面再介绍. ②RGB直接显示数据,简单.性能差,用到FFmpeg的格式转换,没有shader效率高.本文介绍这个方式. 2.GLSu ...
- 整理一些大厂的开源平台及github,向他们看齐...
有人苦恼,该如何突破技术的局限性... 有人羡慕,技术上你怎么懂得这么多... 有人哀叹,唉,我已经学不动了... 我的总结(纯属个人想法):身处IT,就得不断学习和积累,才不会被狠狠地甩在身后.什么 ...
- 三天讲透SpringBoot-初识基础使用
这次我们来说一下我们的SpringBoot,现在SpringBoot已经成为趋势,在我这里我用三篇文章的形式来讲清楚我们的SpringBoot,大致分为,第一篇讲搭建,基本使用,第二篇三方集成,自动装 ...
- 【Tomcat】tomcat7 设置成系统服务启动
1.启动cmd 2.cd C:\Program Files\tomcat7\bin 3.service.bat install 4.打开tomcat7w.exe可以启动管理服务
- egret引擎中使用tiled运行在微信小游戏中
egret的官方文档,对tiled的介绍不是很细致,很多东西都需要摸索.现在把踩的坑记录下来.作为一个备忘 引用tiledmap的库 在GitHub上下载egret的tiledmap支持库:https ...