poj1035

Posted 神犇(shenben)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

比着别人的博客抄了一遍(肯定不是一点也没改),抄完改了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;
}

 

 

以上是关于poj1035的主要内容,如果未能解决你的问题,请参考以下文章

poj1035

POJ 1035 Spell checker (模拟)

poj-1035-Spell Checker

Spell checker POJ 1035 字符串

poj 1035 字符串匹配

POJ 1035Spell checker