POJ 1035 Spell checker 字符串 难度:0
题目
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的更多相关文章
- 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
Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u J ...
- 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(串)
题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...
- 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 简单字符串匹配
在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...
- POJ1035——Spell checker(字符串处理)
Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...
随机推荐
- vue模拟后端获取数据——json-server与express
转载自: https://blog.csdn.net/weixin_39728230/article/details/80293892 https://blog.csdn.net/lxkll/arti ...
- pytorch-1.0 踩坑记录
参加百度的一个竞赛,官方要求把提交的代码测试环境pyorch1.0,于是将自己计算机pytorch升级到1.0. 在ubuntu下用conda install pytorch 命令安装时,效果很差,解 ...
- ie67的冷知识
1. _display:inline;是什么意思 只有ie6认识带下划线的,一般这种写法是用来消除ie6对margin双倍的bug的,比如你写margin-left:10px;那么ie6下显示的是20 ...
- 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...
- (转)stm32启动文件详解
在<<STM32不完全手册里面>>,用的是STM32F103RBT6,所有的例程都采用了一个叫STM32F10x.s的启动文件,里面定义了STM32的堆栈大小以及各种中断的名字 ...
- gitignore有时候为啥过滤不了文件或目录
一.问题介绍 使用Git过程中,有时候我们想过滤项目中的部分文件,在.gitignore中加入该文件名称或该文件所在目录的名称,比如我们的项目日志文件(.log文件) 但是有时候发现不管用.不好使. ...
- phpunit——执行测试文件和测试文件中的某一个函数
phpunit --filter testDeleteFeed // 执行某一个测试函数 phpunit tests/Unit/Services/Feed/FeedLogTest.php // 执行某 ...
- 廖雪峰网站:学习python函数—函数参数(三)
1.*args # 位置参数,计算x2的函数 def power(x): return x * x p = power(5) print(p) # 把power(x)修改为power(x, n),用来 ...
- 4月24 php基础及函数的应用
PHP是一种被广泛认可应用,运行在服务端的脚本语言,PHP需要安转PHP应用程序服务器去解释执行,是用来协助Web服务器工作的编程语言,因此所写的任何代码只有在www文件下的才能实施预览.PHP是一种 ...
- 创建属性Attribute
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); var root = xmlDoc.DocumentElement;//取到 ...