现在的位置: 首页 > 综合 > 正文

[PKU 1035] Spell checker

2013年06月16日 ⁄ 综合 ⁄ 共 4023字 ⁄ 字号 评论关闭

本题我最初完全使用STL容器和迭代器, Time Limit Exceeded。换为数组和通过下标访问之后速度还不错。904K,360MS。

题目如下:

Spell checker

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 13269

 

Accepted: 4882

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

代码:

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
using namespace std;
struct Dictionary //定义字典
{
    char content[10001][20];//字典中单词数量最大10000
    int size;//当前实际存放单词数量
};
 
int main()
{
    Dictionary dic;
    dic.size=0;
    //为字典添加单词,遇到#终止
    while(scanf("%s",dic.content[dic.size]))
    {
        if(dic.content[dic.size][0]=='#')
            break;
        else
        {
            dic.size++;
        }
    }
     
    char currentWord[20];//存放当前单词的字符数组
    while(scanf("%s",currentWord))
    {
        bool haveExact=false;//标记是否有完全匹配
        if(currentWord[0]=='#')
            break;
        else
        {
            printf("%s",currentWord);//无论那种情况,输出中首先是当前单词
 
            vector<string> canbe;//存放可能经过删除,增加,和替换而匹配的单词
 
            for(int i=0;i<dic.size;i++)
            {
                int dicWordLen=strlen(dic.content[i]);//字典中当前正在比较的单词的长度
                int currWordLen=strlen(currentWord);//当前单词长度
                if(!strcmp(dic.content[i],currentWord))
                {
                    printf(" is correct\n");//有完全匹配,输出is correct
                    haveExact=true;
                    canbe.clear();
                    break;
                }
                /*
                非完全匹配分三种情况处理:
                1.dicWordLen==currWordLen
                2.dicWordLen-currWordLen==1
                3.dicWordLen-currWordLen==-1
                无论哪种情况,都找到第一个不同的字母,跳过,如果后面字母完全相同,则满足条件,加入canbe
                情况1中,两个单词均跳过本字母
                情况2,3中,只在较长单词中跳过本字母
                */
                else if(dicWordLen==currWordLen)
                {
                    int firstDif;
                    for(firstDif=0;firstDif<currWordLen && dic.content[i][firstDif]==currentWord[firstDif];firstDif++)
                    {
                        ;
                    }
                    for(++firstDif;firstDif<currWordLen && dic.content[i][firstDif]==currentWord[firstDif];firstDif++)
                    {
                        ;
                    }
 
                    if(firstDif==currWordLen)
                        canbe.push_back(dic.content[i]);
 
                }
                else if(dicWordLen-currWordLen==1)
                {
                    int firstDif2;
                    for(firstDif2=0;firstDif2<currWordLen && dic.content[i][firstDif2]==currentWord[firstDif2];firstDif2++)
                    {
                        ;
                    }
                    for(++firstDif2;firstDif2< dicWordLen && dic.content[i][firstDif2]==currentWord[firstDif2-1];firstDif2++)
                    {
                        ;
                    }
                    if(firstDif2==dicWordLen)
                        canbe.push_back(dic.content[i]);
 
 
                }
                else if(dicWordLen-currWordLen==-1)
                {
                    int firstDif3;
                    for(firstDif3=0;firstDif3<dicWordLen && dic.content[i][firstDif3]==currentWord[firstDif3];firstDif3++)
                    {
                        ;
                    }
                    for(++firstDif3;firstDif3< currWordLen && dic.content[i][firstDif3-1]==currentWord[firstDif3];firstDif3++)
                    {
                        ;
                    }
                    if(firstDif3==currWordLen)
                        canbe.push_back(dic.content[i]);
                }
 
            }
            //保证按题目格式输出
            if(!canbe.empty() ||!haveExact)
            {
                printf(":");
                for(vector<string>::iterator it=canbe.begin();it!=canbe.end();it++)
                    cout<<" "<<*it;
                cout<<endl;
            }
        }
    }
}

抱歉!评论已关闭.