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 ...
随机推荐
- 排序——选择排序(java描述)
百度百科的描述如下:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元 ...
- npm install 报错ERR! 404 Not Found: event-stream@3.3.6
在win下开发的node工程,在linux下用dockerfile部署时,遇到npm install时报错 Step / : RUN npm install ---> Running in 2e ...
- 记录python接口自动化测试--简单总结一下学习过程(第十目)
至此,从excel文件中循环读取接口到把测试结果写进excel,一个简易的接口自动化测试框架就完成了.大概花了1周的时间,利用下班和周末的时间来理顺思路.编写调试代码,当然现在也还有很多不足,例如没有 ...
- Run-time code to create charts:
tChart1.Series.Clear(); tChart1.Series.Add(new Steema.TeeChart.Styles.Bar());tChart1.Series[0].Clear ...
- android -------- Data Binding的使用 ( 六) 自定义属性
今天来说说DataBinding在自定义属性的使用 默认的android命名空间下,我们会发现并不是所有的属性都能直接通过data binding进行设置,比如margin,padding,还有自定义 ...
- laravel调度任务
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation ...
- k8s相关端口表-以及周边工具
k8s端口 kube-api 6443 kube-controller-manager 10252 kube-scheduler 10251 kubelet 10250 kube-proxy 1025 ...
- 【oauth2.0】【2】JAVA 客户端模式
含义:用户直接向客户端注册,客户端以自己的名义要求"服务提供商"提供服务,其实不存在授权问题 步骤: (A)客户端向认证服务器进行身份认证,并要求一个访问令牌(token). (B ...
- python-day76--django-Form组件
django中Form组件 1. 用户请求数据验证 2. 自动生成错误信息 3. 打包用户提交正确信息 4. 错误:保留上次输入内容 5. 定制页面上显示的HTML标签 引入: from django ...
- php并发
bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件指针.o ...