POJ 1035-Spell checker(字符串)

题目地址:POJ 1035

题意:输入一部字典。输入若干单词。 若某个单词能在字典中找到,则输出corret。若某个单词能通过 变换 或 删除 或 加入一个字符后。在字典中找得到。则输出这些单词。输出顺序依据 输入的那部字典的字典序;若某个单词不管操作与否都无法在字典中找得到,则输出空。

思路:关于全然匹配的就直接输出就好。解题关键在不全然匹配的情况:比較时我们先算两个单词长度差之差,有三种情况:len1=strlen(str)len2=strlen(mp[n]]

假设len1==len2则是可能有一个字符不一样;逐个字符比較。统计不同字符数

假设len1+1==len2则是少一个字符。逐个字符比較,假设有字符不同。则mp[n]字符下标往下移动一位。str不变,不同字符数加1

假设len1-1==len2则是多一个字符,逐个字符比較。假设有字符不同。则str字符下标往下移动一位。mp[n]不变。不同字符数加1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef __int64  LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-7;
const int maxn=10010;
char mp[maxn][20];
char str[20];
int check(int n)
{
    int i,j;
    int cnt;
    int len1=strlen(str);
    int len2=strlen(mp[n]);
    if(len1-len2==1) {
        cnt=0;
        i=j=0;
        for(; i<len1;) {
            if(str[i]!=mp[n][j]) {
                cnt++;
                i++;
            } else {
                i++;
                j++;
            }
        }
        if(cnt==1)
            return 1;
        return 0;
    } else if(len1==len2) {
        cnt=0;
        i=j=0;
        for(; i<len1;) {
            if(str[i]!=mp[n][j])
                cnt++;
            i++,j++;
        }
        if(cnt==1)
            return 1;
        return 0;
    } else if(len1-len2==-1) {
        cnt=0;
        i=j=0;
        for(; j<len2;) {
            if(str[i]!=mp[n][j]) {
                cnt++;
                j++;
            } else {
                i++;
                j++;
            }
        }
        if(cnt==1)
            return 1;
        return 0;
    }
    return 0;
}
int main()
{
    int n=0,i;
    while(~scanf("%s",mp[n])) {
        if(mp[n][0]==‘#‘)
            break;
        n++;
    }
    while(~scanf("%s",str)) {
        if(str[0]==‘#‘)
            break;
        for(i=0; i<n; i++) {
            if(strcmp(str,mp[i])==0) {
                printf("%s is correct\n",str);
                break;
            }
        }
        if(i==n) {
            printf("%s:",str);
            for(i=0; i<n; i++)
                if(check(i))
                    printf(" %s",mp[i]);
            puts("");
        }
    }
    return 0;
}
时间: 2024-11-07 03:54:11

POJ 1035-Spell checker(字符串)的相关文章

[ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

POJ 1035 Spell Check 字符串处理

被这样的题目忽悠了,一开始以为使用Trie会大大加速程序的,没想到,一不小心居然使用Trie会超时. 最后反复试验,加点优化,终于使用Trie是可以过的,不过时间大概难高于1500ms,一不小心就会超时. 看来这是一道专门卡Trie的题目,只好放弃不使用Trie了. 也得出点经验,如果字符串很多,如本题有1万个字符串的,那么还是不要使用Trie吧,否则遍历一次这样的Trie是十分耗时的,2s以上. 于是使用暴力搜索法了,这里的技巧是可以使用优化技术: 1 剪枝:这里直接分组搜索,分组是按照字符串

POJ 1035 Spell checker (串)

题目大意: 问你后面输入的串能不能通过  加减一个字符,或者替换一个字符变成字典中的串. 思路分析: 直接模拟替换加减的过程. 比较两个串的长度.要相差为1 的时候才能进行模拟. 模拟的过程就是进行一个个的匹配. 发现失配的次数小于等于 1就可以输出. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <string> #i

[ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

POJ 1035 Spell checker

题目链接 http://poj.org/problem?id=1035 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24142   Accepted: 8794 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the cor

poj 1035 Spell checker(暴力判断)

题目链接:http://poj.org/problem?id=1035 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their f

POJ 1035 Spell checker (模拟)

题目链接 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms. If the word is absent in

【POJ】1035 Spell checker

字典树. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <string> 7 using namespace std; 8 9 typedef struct Trie { 10 int in; 11 Trie *next[26]; 12 } Trie;

POJ1035——Spell checker(字符串处理)

Spell checker DescriptionYou, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms. If the word is ab

poj 1035 纯正的字符串水

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22673   Accepted: 8258 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word