Spell checker POJ - 1035

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

#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::cin;

class checker
{
    char *direction[10000];
    int counter;
public:
    checker(){counter = -1;}
    ~checker(){for(int i = 0;i < counter;i++) delete[] direction[i];}
    void add(char*);
    char *getDirection(int i){return direction[i];}
    int getCounter(){return counter;}
    int* judge_1(char *cmp);
    int* judge_2(char *cmp);
    int* judge_3(char *cmp);
};
int *merge(int *,int*,int*);
int main()
{
    checker check;
    char temp[100];
    cin >> temp;
    while(temp[0] != '#')
    {
        check.add(temp);
        cin >> temp;
    }
    cin >> temp;
    while(temp[0] != '#')
    {
        int i;
        for(i = 0;i < check.getCounter() + 1;i++)
        {
            if(!strcmp(temp,check.getDirection(i)))//有一模一样的
            {
                cout << temp << " is correct" << endl;
                break;
            }
        }
        if(i == check.getCounter() + 1 && strcmp(temp,check.getDirection(check.getCounter())) != 0)//没找到完全相同的
        {
            cout << temp << ':' ;
            int *p1 = check.judge_1(temp);
            int *p2 = check.judge_2(temp);
            int *p3 = check.judge_3(temp);
            int *p = merge(p1,p2,p3);
            for(int i = 1;i < p[0] + 1;i++)
                cout << ' ' << check.getDirection(p[i]);
            cout << endl;
            delete []p;
        }
        cin >> temp;
    }
    return 0;
}

int *merge(int * a ,int* b,int* c)
{
    a[0]--;
    b[0]--;
    c[0]--;
    int *p = new int[1000],*pa = a,*pb = b,*pc = c,index = 0,i = 1,j = 1, k = 1;
    while(a[0] || b[0] || c[0])
    {
        int min = 1000000000;
        if(a[0])
        if(min > a[i])
            min = a[i];
        if(b[0])
        if(min > b[j])
            min = b[j];
        if(c[0])
        if(min > c[k])
            min = c[k];
        if(a[0])
        if(min == a[i])
        {
            a[0]--;
            p[++index] = a[i++];
        }
        if(b[0])
        if(min == b[j])
        {
            b[0]--;
            p[++index] = b[j++];
        }
        if(c[0])
        if(min == c[k])
        {
            c[0]--;
            p[++index] = c[k++];
        }
    }
    p[0] = index;
    delete [] a;
    delete [] b;
    delete [] c;
    return p;
}

void checker::add(char* a)
{
    int len = strlen(a);
    char *p = new char[len + 1];
    strcpy(p,a);
    direction[++counter] = p;
}
int* checker::judge_1(char *cmp)
{
    int *index = new int[100];
    int flag = 0,len = strlen(cmp);
    int coun = 0;
    for(int i = 0;i <= counter;i++)
    {
        //cout << direction[i] << endl;
        if(strlen(direction[i]) == len)
        {
            flag = 0;
            char* p = direction[i],*p0 = cmp;
            while(*p)//确定只有一个不同
            {
                if(*p != *p0)
                    if(flag) break;
                    else flag = 1;
                p0++; p++;
            }
            if(*p == 0)//能走到最后,说明顶多就一个不同
                index[++coun] = i;
        }
    }
    index[0] = coun + 1;
    return index;
}
int* checker::judge_2(char *cmp)//判断只缺了一个
{
    int *index = new int[100];
    int flag = 0,len = strlen(cmp);
    int coun = 0;
    for(int i = 0;i <= counter;i++)
    {
        //cout << direction[i] << endl;
        if(strlen(direction[i]) == len + 1)
        {
            flag = 0;
            char* p = direction[i],*p0 = cmp;
            while(*p)//确定只有一个不同
            {
                if(*p != *p0)
                {
                    if(flag) break;
                    else
                    {
                        flag = 1;
                        p0--;
                    }
                }
                p0++; p++;
            }
            if(*p == 0)//能走到最后,说明顶多就一个不同
                index[++coun] = i;
        }
    }
    index[0] = coun + 1;
    return index;
}
int* checker::judge_3(char *cmp)
{
    int *index = new int[100];
    int flag = 0,len = strlen(cmp);
    int coun = 0;
    for(int i = 0;i <= counter;i++)
    {
        //cout << direction[i] << endl;
        if(strlen(direction[i]) == len - 1)
        {
            flag = 0;
            char* p = direction[i],*p0 = cmp;
            while(*p0)//确定只有一个不同
            {
                if(*p != *p0)
                {
                    if(flag) break;
                    else
                    {
                        flag = 1;
                        p--;
                    }
                }
                p0++; p++;
            }
            if(*p0 == 0)//能走到最后,说明顶多就一个不同
                index[++coun] = i;
        }
    }
    index[0] = coun + 1;
    return index;
}

发布了39 篇原创文章 · 获赞 4 · 访问量 5754

猜你喜欢

转载自blog.csdn.net/weixin_45725137/article/details/105234171
今日推荐