首页 > 代码库 > 串 --- 匹配

串 --- 匹配

Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18351   Accepted: 6732

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

 

【题目来源】

Northeastern Europe 1998

 

【题目大意】

首先输入一个字典,然后输入要检查的字符串,对每个字符串进行检查,题目很好理解,这儿就不多说了。

 

【题目分析】

1.整体思路:字典输入完成后,对输入的每个字符串进行比较,暴力枚举;

2.对于每个单词如果完全匹配,立即输出字典中匹配的单词,并结束本次比较,继续下一个字典单词的比较;

  如果未完全匹配,则有:

                1.Len1==len2
                2.len1-len2==1
                3.len1-len2==-1
                无论哪种情况,都找到第一个不同的字母,跳过,如果后面字母完全相同,则满足条件,加入canbe
                情况1中,两个单词均跳过本字母
                情况2,3中,只在较长单词中跳过本字母

 

还有就是注意输出格式,为此献了3次pe;

 

#include<cmath>
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string a[10005],b[55],ans[10001];
bool check(string x,string y)
{
    int len1=x.size();
    int len2=y.size();
    if(abs(len1-len2)>1) return 0;
    if(len1==len2)
    {
        int ans=0;
        for(int i=0;i<len2;i++)
        {
            if(x[i]!=y[i])
            {
                ans++;
                if(ans>1) return 0;
            }

        }
        return 1;
    }
    else if(len1>len2)
    {
        int ans=0;
        for(int i=0,j=0;i<len1&&j<len2;)
        {
            if(x[i]!=y[j])
            {
                i++;
                ans++;
                if(ans>1) return 0;
            }
            else
            {
                i++;
                j++;
            }
        }
        return 1;
    }
    else
    {
        int ans=0;
        for(int i=0,j=0;i<len1&&j<len2;)
        {
            if(x[i]!=y[j])
            {
                j++;
                ans++;
                if(ans>1) return 0;
            }
            else
            {
                i++;
                j++;
            }
        }
        return 1;
    }
}
int main()
{
    string s;
    int n=0,m=0;
    while(cin>>s && s[0]!=#)
    {
        a[n++]=s;
    }
    while(cin>>s && s[0]!=#)
    {
        b[m++]=s;
    }
    for(int i=0;i<m;i++)
    {
        int k=0,flag=0;
        for(int j=0;j<n;j++)
        {
            if(b[i]==a[j])
            {
                flag=1;
                cout<<b[i]<<" is correct"<<endl;
                break;
            }
            else
            {
                if(check(b[i],a[j]))
                ans[k++]=a[j];
            }
        }
        if(!flag)
        {
            cout<<b[i]<<:;
            for(int j=0;j<k;j++)
            cout<< <<ans[j];
            printf("\n");
        }
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18351   Accepted: 6732

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