题目网址: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(串)的更多相关文章

  1. poj 1035 Spell checker

    Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   J ...

  2. poj 1035 Spell checker ( 字符串处理 )

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16675   Accepted: 6087 De ...

  3. [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 De ...

  4. POJ 1035 Spell checker 字符串 难度:0

    题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...

  5. poj 1035 Spell checker(水题)

    题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...

  6. poj 1035 Spell checker(hash)

    题目链接:http://poj.org/problem?id=1035 思路分析: 1.使用哈希表存储字典 2.对待查找的word在字典中查找,查找成功输出查找成功信息 3.若查找不成功,对word增 ...

  7. POJ 1035 Spell checker (模拟)

    题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...

  8. POJ 1035 Spell checker 简单字符串匹配

    在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...

  9. POJ训练计划1035_Spell checker(串处理/暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18418   Accepted: 6759 De ...

随机推荐

  1. DBCP

    DBCP(DataBase Connection Pool)数据库连接池,由Apache公司开发.连接池的运用避免了反复建立连接造成的资源浪费,预先建立一些连接放在数据库连接池中,需要时取出,不需要时 ...

  2. centos7下mysql 用户管理和权限设置

    1.进入mysql命令行,输入root及密码[root@localhost ~]# mysql -u root -pEnter password: Welcome to the MySQL monit ...

  3. Tomcat启动时设置Jdk版本

    1. Window版本Tomcat 到bin下的setclasspath.bat文件,在文件的开始处添加如下代码: set JAVA_HOME=D:\Program Files\Java\jdk1.8 ...

  4. RocksDB线程局部缓存

    概述 在开发过程中,我们经常会遇到并发问题,解决并发问题通常的方法是加锁保护,比如常用的spinlock,mutex或者rwlock,当然也可以采用无锁编程,对实现要求就比较高了.对于任何一个共享变量 ...

  5. .Net Core 3.0 gRPC部署问题解决

    前言 .Net Core3.0终于如约而至的来了.在3.0中增加了许多东西.也有了许多的变化.今天我们看的就是在3.0中使用gRPC并遇到的问题.gRPC现在可以非常方便简洁的在.Net Core中使 ...

  6. Mac配置环境变量path

    查看当前配置的path有哪些: 1. echo $PATH  当前所有的 2. cat /etc/paths  这个文件是操作系统自带的 mac系统环境变量的加载顺序(优先级): /etc/profi ...

  7. Hadoop点滴-HDFS文件系统

    1.HDFS中,目录作为元数据,保存在namenode中,而非datanode中 2.HDFS的文件权限模型与POSIX的权限模式非常相似,使用  r  w  x 3.HDFS的文件执行权限(X)可以 ...

  8. 并发编程之多线程(Java)

    一.线程与进程区别 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以线程基本上是 ...

  9. Navicat使用常见的两个问题及解决方法,提高开发效率

    Navicat使用常见问题 在我们日常开发过程中,一般不会直接使用命令行来操作 MYSQL 数据库,而会选择一些图形化界面去帮助我们来进行此类操作,常用的有:SQLyog(Logo也是小海豚),Nav ...

  10. 利用基本数据封装类(如:Integer,Float)等实现数据类型转换

    /** * 利用基本数据封装类进行数据类型转换 * @author dyh * */ public class TypeConversion { public static void main(Str ...