poj1035

Spell checker

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23998   Accepted: 8754

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 the dictionary then it can be replaced by correct words
(from the dictionary) that can be obtained by one of the following operations:

?deleting of one letter from the word;
?replacing of one letter in the
word with an arbitrary letter;
?inserting of one arbitrary letter into the
word.
Your task is to write the program that will find all possible
replacements from the dictionary for every given word.

Input

The first part of the input file contains all words
from the dictionary. Each word occupies its own line. This part is finished by
the single character ‘#‘ on a separate line. All words are different. There will
be at most 10000 words in the dictionary.
The next part of the file contains
all words that are to be checked. Each word occupies its own line. This part is
also finished by the single character ‘#‘ on a separate line. There will be at
most 50 words that are to be checked.
All words in the input file (words
from the dictionary and words to be checked) consist only of small alphabetic
characters and each one contains 15 characters at most.

Output

Write to the output file exactly one line for every
checked word in the order of their appearance in the second part of the input
file. If the word is correct (i.e. it exists in the dictionary) write the
message: " is correct". If the word is not correct then write this
word first, then write the character ‘:‘ (colon), and after a single space write
all its possible replacements, separated by spaces. The replacements should be
written in the order of their appearance in the dictionary (in the first part of
the input file). If there are no replacements for this word then the line feed
should immediately follow the colon.

Sample Input

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

Sample Output

me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me

Source

Northeastern Europe 1998

比着别人的博客抄了一遍(肯定不是一点也没改),抄完改了5遍才A了

强烈建议理解,noip字符串也就这个难度了

字符串处理格式模板

#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
#define N 50
char dict[N*200+1][16];
char word[N+1][16];
int dictnum=0; //字典计数器
int wordnum=0; //单词计数器
int change(char* word,char* dict)//检查字符串word能否通过变换得到dict
{//wordLen==dictlen
    int dif=0;  //记录word与dict中在相同位置出现不同字符的个数
    while(*word){
        if(*(word++)!=*(dict++)){
            dif++;
            if(dif>1) return 0;
        }
    }
    return 1;
}
int del(char* word,char* dict)//检查字符串word能否通过删除得到dict
{//wordLen==dictlen+1
    int dif=0;  //记录word与dict中在对应位置出现不同字符的个数
    while(*word){
        if(*word!=*dict){
            word++;dif++;  //word后移一位再匹配
            if(dif>1) return 0;
        }
        else
            word++,dict++;
    }
    return 1;
}
int add(char* word,char* dict)//检查字符串word能否通过添加得到dict
{//wordLen==dictlen-1
    int dif=0;  //记录word与dict中在对应位置出现不同字符的个数
    while(*dict){
        if(*word!=*dict){
            dict++;dif++; //dict后移一位再匹配
            if(dif>1) return 0;
        }
        else
            word++,dict++;
    }
    return 1;
}
int main()
{
    while(cin>>dict[dictnum] && dict[dictnum++][0]!=‘#‘);dictnum--;
    while(cin>>word[wordnum] && word[wordnum++][0]!=‘#‘);wordnum--;
    int* dictlen=new int[dictnum];  //记计算字典中各个单词的长度
    for(int i=0;i<dictnum;i++)
        dictlen[i]=strlen(dict[i]);
    for(int i=0;i<wordnum;i++){
        int* address=new int[dictnum];  //记录word[i]通过变化得到的单词在dict中的下标
        int pa=0; //address指针
        int flag=0;  //标记字典中是否含有单词word[i]
        int len=strlen(word[i]);
        for(int k=0;k<dictnum;k++){//遍历字典
            if(dictlen[k]==len){ //change or Equal
                if(!strcmp(word[i],dict[k])){
                    flag=1;break;
                }
                else if(change(word[i],dict[k]))
                    address[pa++]=k;
            }
            else if(len-dictlen[k]==1){  //delete
                if(del(word[i],dict[k]))
                    address[pa++]=k;
            }
            else if(dictlen[k]-len==1){  //add
                if(add(word[i],dict[k]))
                    address[pa++]=k;
            }
        }
        if(flag)
            cout<<word[i]<<" is correct"<<endl;
        else{
            cout<<word[i]<<": ";
            for(int j=0;j<pa;j++)
                cout<<dict[address[j]]<<‘ ‘;
            cout<<endl;
        }
        delete address;
    }
    return 0;
}
时间: 2024-07-31 06:30:14

poj1035的相关文章

POJ1035 Spell-checker(哈希,串处理)

本文出自:http://blog.csdn.net/svitter 题意: 检查字典. 一开始,输入字典中的字符,以#结束. 随后,输入查询的字符,以#结束. 其中,符合要求的查询项目有: 1.去除一个字符,可以匹配 2.取代一个字符,可以匹配 3.添加一个字符,可以匹配 输入输出分析: 1.注意不要将#包含进入字典. 2.对于每一个字符进行分析. 题目分析: 使用哈希表或者直接暴力解题. 一个字符指针指向要查询的单词,一个字典指针指向字典中的单词. 分为三种处理情况: 1.对于去除一个字符的情

POJ1035 Spell checker

在做用户查找时 因为要把查找的结果动态加载和显示,所以,那些html元素要由Ajax动态生成.用户打开查找界面时,有系统推荐的用户,而当用户按条件查找后,查找的结果动态加载和显示.所以考虑到用js来搞. 这个for循环就是移除已有的表单.然后根据Ajax请求过来的数据,动态生成新的表单对象.一定要注意j变量从大往小循环,否则,删除div元素后会引起serchResultLenth=serchResult.children.length;长度的变化(这个问题摸索了好久,才搞定,切记) for(va

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

poj1035(Spell checker)

题目地址:Spell checker 题目大意: 给你一个关于字符串的字典,让你根据字典里的字符串判断输入字符串的正确性.如果字符串不正确,可以通过以下的操作来输出字符串的可能性:1.可以替换一个字符,2.可以删除一个字符,3.可以添加一个字符.如果满足以上操作,说明都算是字符串的可能性,然后输出. 解题思路: 判断四种情况即可,如果正确直接输出,如果出现1.2.3这几种情况,先将字符串存到一个字符数组里s[N][N].如果全部判断完,没有正确性,输出可能性的字符串. 代码: 1 #includ

POJ1035——spell checker

正好最近被人问到一个数据结构的问题,难住了.所以决定来刷刷数据结构的题,从初级的开始,当回炉再造了. 题目大概意思: 作为一个拼写的checker,遵从如下几个规则-- (1)对待检查单词,删除一个字母后,能在字典中找到: (2)对待检查单词,替换一个字母(任意字母替换)后,能在字典中找到: (3)对待检查单词,插入一个任意字母后,能在字典中找到: INPUT: 第一部分是字典,字典最多10000各单词,字典输入结束以'#'号结束,每个单词占据一行 第二部分是要检查的单词,最多50个,也以'#'

POJ1035&amp;&amp;POJ3080&amp;&amp;POJ1936

字符串处理专题,很早就写好了然而忘记写blog了 1035 题意:给你一些单词作为字典.然后让你查找一些单词.对于每个单词,如果在字典中就输出它.否则输出所有它通过删除||增加||替换一个字符能得到的单词 由于数据范围很小,所以我们直接暴力跑一下即可 CODE #include<string> #include<iostream> #include<map> using namespace std; const int N=10005; map <string,b

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅