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 ...
随机推荐
- 由table理解display:table-cell
转载自:https://segmentfault.com/a/1190000007007885 table标签(display:table) 1) table可设置宽高.margin.border.p ...
- Android JNI 数组操作
JNI 中有两种数组操作,基础数据类型数组和对象数组,JNI 对待基础数据类型数组和对象数组是不一样的. 基本数据类型数组 对于基本数据类型数组,JNI 都有和 Java 相对应的结构,在使用起来和基 ...
- RNN生产唐诗
1. 项目的RNN模型: 项目中的输入x是这样的:
- legend2---开发日志3(thinkphp的入口目录是public的体现是什么)
legend2---开发日志3(thinkphp的入口目录是public的体现是什么) 一.总结 一句话总结:需要深刻理解程序的入口和入口位置都在public目录这里,比如读写文件的初始目录都在这,获 ...
- 拖图UI和纯代码UI
1拖图UI, 优点:适合快速实验各种天马行空的想法 缺点:太多的storyBoard不好管理,不适合较大的项目,如果一个项目有价值,或成熟了,为了维护拓展,就最好改为纯代码 2纯代码UI 优点:1好维 ...
- Setting the Java Class Path
The class path is the path taht Java Runtime Environment(JRE) searches for classes and other resourc ...
- 1.4 Crack小实验
0_day 第一章 基础知识 1.4 Crack小实验 <0day_2th>王清 著 电子书 下载链接:https://pan.baidu.com/s/11TgibQSC3-kYwCInm ...
- java类的设计原则
1.内聚性 类应该描述一个单一的实体,所有的类操作应该在逻辑上相互配合,支持一个连贯性的目标.例如:学生和教职工属于不同的实体,应该定义两个类. 2.一致性 要遵循一定的设计风格和命名习惯.给类.方法 ...
- Spring配置表友好性优化思路
Spring配置表需要尽量保证对程序员的友好性,一下提供一种优化思路. 中途未保存,心态炸了,只贴图了,fuuuuuuuuuuuuuck 第一种(最烂,最不友好):以Json的格式保存在配置表中,程序 ...
- Matlab-11:Gausssidel迭代法工具箱
算法推导: function [u,n]=GaussSeid(A,b,u0,eps,M) %GaussSeid.m为用高斯-塞德尔迭代法求解线性方程组 %A为线性方程组的系数矩阵 %b为线性方程组的常 ...