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环境变量---找不到或无法加载主类
默认安装在C:\ProgramFiles\Java\jdk1.7.0目录下 环境变量配置为 PATH=.;%JAVA_HOME%\bin CLASSPATH=.;%JAVA_HOME%\lib\dt. ...
- Corrupted Metadata/failed to mount /sysroot
公司电脑CentOS系统突然断电,开机后,无法进入系统.查看系统log如下: mount: mount /dev/mapper/cl-root on /sysroot failed:Structure ...
- Python + Selenium WebDriver Api 知识回顾
一直再用 Selenium WebDriver 但是用的都比较零散,也没有做过总结,今天借此机会,整理一下,方便大家使用时查阅 webDriver 的属性 ['CONTEXT_CHROME', 'C ...
- Python全栈开发-Day5-常用模块学习
本节大纲: 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 pyyaml处理 configparser hashlib re正则 ...
- c# 读取txt方法
string strLine; try { FileStream aFile = new FileStream("Log.txt", FileMode.Open); StreamR ...
- java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合
java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...
- spring security+freemarker获取登陆用户的信息
spring security+freemarker获取登陆用户的信息 目标页面之间获取 ${Session.SPRING_SECURITY_CONTEXT.authentication.princi ...
- English trip V1 - B 1. How much is it? 它是多少钱? Teacher:Corrine Key: is/are
In this lesson you will learn to ask about prices. 本节课你将学习询问关于价格 课上内容(Lesson) one piece of two p ...
- English trip Spoken English & Word List(updating...)
Word List 词汇 Square 英 [skweə] 美 [skwɛr] adj. 平方的:正方形的:直角的:正直的. 使成方形:与…一致 vi. 一致:成方形 n. 平方:广场:正方形 ...
- You Don't Know JS: Async & Performance(第2章,Callbacks)
Chapter 2: Callbacks. Callbacks are by far the most common way that asynchrony in JS programs is exp ...