POJ 1035 Spell checker 字符串 难度:0

时间:2021-09-25 16:21:36

题目

http://poj.org/problem?id=1035

题意

字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配。在匹配时,如果直接匹配可以找到待匹配串,则直接输出正确信息,否则输出所有满足以下条件的单词:

1. 待匹配串删除任意一个字符后可以匹配的

2. 单词删除任意一个字符后可以匹配的

3. 把待匹配串中某个字符改为单词对应字符后可以匹配的

思路
由于单词表容量较小,直接匹配可以直接使用strcmp进行。若直接匹配不成功,那么需要对每个单词进行再次比较,

对每个单词,仅有以下三种可能,设待匹配串长度为patlen,单词长度为wlen,

1. patlen > wlen:只需找到待匹配串中第一个与单词不同的字符,删除后再看剩下的是否全等即可。

2. patlen < wlen:类似第一种情况,只需找到单词中第一个与待匹配串不同的字符,删除后再看剩下的是否全等即可。

3. patlen == wlen:若能够通过替换匹配,那么两个字符串最多只能有一个字符不同。

感想
做这道题时为了节约思考时间使用了最朴素的比较方法,如果需要提升自己的能力不应该使用这种算法,可以使用dp,kmp,trie树,ac自动机等算法。

代码

 #include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <assert.h>
#include <sstream>
#include <cctype>
#include <queue>
#include <stack>
#include <map>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld; const int wordlen = ;
const int maxn = 1e4 + ; char words[maxn][wordlen];
char pat[wordlen]; int n;
int wlens[maxn]; bool canMatch(char * pat, char * word, int patlen, int wlen){
if(abs(wlen - patlen) > )return false;
if(wlen < patlen){
swap(pat, word);
swap(wlen, patlen);
}
if(wlen == patlen){
int num = ;
for(int i = ;i < patlen;i++){
if(word[i] != pat[i])num++;
}
return num <= ;
}else{
for(int i = , j = ;i < wlen;i++, j++){
if(word[i] != pat[j]){
if(i == j)j--;
else return false;
}
}
return true;
}
} void solve()
{
n = ;
while(scanf("%s", words[n]) == && (words[n][] != '#' || words[n][] != )) {
wlens[n] = strlen(words[n]);
n++;
}
while(scanf("%s", pat) == && (pat[] != '#' || pat[] != ))
{
bool correct = false;
for(int i = ; i < n; i++)
{
if(strcmp(pat, words[i]) == )
{
printf("%s is correct\n", pat);
correct = true;
break;
}
}
if(!correct)
{
int patlen = strlen(pat);
printf("%s:", pat);
for(int i = ; i < n; i++)
{
if(canMatch(pat, words[i], patlen, wlens[i]))
{
printf(" %s",words[i]);
}
}
puts("");
}
}
}
int main(){
freopen("input.txt", "r", stdin);
solve();
return ;
}

POJ 1035 Spell checker 字符串 难度:0的更多相关文章

  1. poj 1035 Spell checker &lpar; 字符串处理 &rpar;

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

  2. &lbrack;ACM&rsqb; POJ 1035 Spell checker &lpar;单词查找,删除替换添加不论什么一个字母)

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

  3. poj 1035 Spell checker

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

  4. POJ 1035 Spell checker (模拟)

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

  5. POJ 1035 Spell checker&lpar;串&rpar;

    题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...

  6. poj 1035 Spell checker&lpar;水题&rpar;

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

  7. poj 1035 Spell checker&lpar;hash&rpar;

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

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

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

  9. POJ1035——Spell checker&lpar;字符串处理&rpar;

    Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...

随机推荐

  1. 【转】C&num;语言之&OpenCurlyDoubleQuote;string格式的日期时间字符串转为DateTime类型”的方法

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...

  2. 【iOS】在Swift中使用JSONModel

    前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...

  3. onethink常用标签的使用示例

    首页文章模型列表输出: <article:list name="article" category="2" row="3" order ...

  4. SQL 中ROLLUP 用法

    SQL 中ROLLUP 用法 ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有 ...

  5. HMM TOOL

    HMM隐马尔科夫模型 MATLAB 工具包对各种数据的处理 HMM 工具包下载地址: http://www.cs.ubc.ca/~murphyk/Software/HMM/hmm.html 工具包使用 ...

  6. Masonry&plus;Infinite-Scroll实现无刷新无分页完美瀑布流&lpar;转&rpar;

    一.Masonry 是基于Jquery插件,用于对CSS布局的可移动层进行重新布局.Masonry愿意石工,可以这样形象的理解,页面上很多大小不一的移动层可以想象成散乱的石头,经过Masonry这个石 ...

  7. xml--通过DOM解析XML

    此文章通过3个例子表示DOM方式解析XML的用法. 通过DOM解析XML必须要写的3行代码. step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) step 2:获得具体的dom解 ...

  8. python序列化与反序列化(json与pickle)

    在python中,序列化可以理解为将python中对象的编码格式转换为json(pickle)格式的字符串,而反序列化可以 理解为将json(pickle)格式的字符串转换为python中对象的编码格 ...

  9. Breastcancer社区评论下载

    首页 某个社区 某社区的一个话题 目标:获取这个网站所有话题的所有评论相关信息 python实现 # -*- coding: utf-8 -*- """ @Datetim ...

  10. js获取上一页、当前页及域名url

    一个业务中可能会用到,跳转到另个页面后, 又后退回之前的页面,之前的页面上有个判断提示一定会出 网上搬了下代码 console.log("js获取当前域名"+window.loca ...