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
类型:
模拟水题,稍微了解一下C++/string
题意:
先输入多个单词以’#’结束存进字典里,然后输入多个单词查询一下字典中是否有该单词,如果没有,判断是否可能是字典中的某个单词多了或少了或者改了一个字母变成的,按如果有,将这些单词按输入顺序输出。
解题思路:
用vector<string>的二维数组存储长度不同的单词,同时将单词存入S二维数组中,以每个单词的输入顺序做为标识。同时将所有单词的输入顺序对应的存储在另一个vector<int>数组中,这样每次判断出来后,存在ans数组中,最后排序一下再依次输出。
做这题时了解了string的一些用法。
因为string是c++新增的,在一些C的使用中需要以str.c_str()的形式调用,或者就直接用C++的形式调用吧(包括输出和一些函数的使用)。
另外string作为独立的容器还包含了一些独立的函数,大家想深入了解转百度,或者和我一起参考http://www.cnblogs.com/gaojun/archive/2010/09/11/1824016.html啦!
AC代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
vector<string>word[17];
vector<int>num[17];
string str;
char S[10005][20];
int ans[10005];
int t=0;
bool judge(int n)
{
int len=word[n].size();
int k=1;
for(int i=0;i<len&&k;i++)
{
for(int j=0;j<n&&k;j++)
{
if(str[j]!=word[n][i][j])
break;
if(j==n-1)
k=0;
}
}
return !k;
}
void find0(int n)
{
if(n==0)
return ;
int len=word[n].size();
for(int x=0,k=0;x<len;x++,k=0)
{
for(int i=0,j=0;i<n;)
{
if(word[n][x][i]!=str[j]){k++;j++;}
else{i++;j++;}
if(k>1)
break;
}
if(k<=1)
ans[t++]=num[n][x];
}
}
void find1(int n)
{
int len=word[n].size();
for(int x=0,k=0;x<len;x++,k=0)
{
for(int i=0;i<n;i++)
{
if(word[n][x][i]!=str[i])
k++;
if(k>1)
break;
}
if(k==1)
ans[t++]=num[n][x];
}
}
void find2(int n)
{
if(n==16)
return ;
int len=word[n].size();
for(int x=0,k=0;x<len;x++,k=0)
{
for(int i=0,j=0;j<n-1;)
{
if(word[n][x][i]!=str[j]){k++;i++;}
else{i++;j++;}
if(k>1)break;
}
if(k<=1)
ans[t++]=num[n][x];
}
}
void c_out()
{
sort(ans,ans+t);
for(int i=0;i<t;i++)
cout<<" "<<S[ans[i]];
}
int main()
{
int m=0;
while(cin>>str)
{
if(str[0]=='#')
break;
else
{
strcpy(S[m++],str.c_str());
int n=str.size();
word[n].push_back(str);
num[n].push_back(m-1);
}
}
while(cin>>str)
{
if(str[0]=='#')
break;
memset(ans,-1,sizeof(ans));
t=0;
int len=str.size();
if(judge(len))
{
printf("%s is correct\n",str.c_str());
continue;
}
cout<<str<<":";
find0(len-1);
find1(len);
find2(len+1);
c_out();
cout<<endl;
}
}