【问题描述】
作为一个新的拼写检查程序开发团队的成员,您将编写一个模块,用已知的所有形式正确的词典来检查给定单词的正确性。
如果字典中没有这个词,那么可以用下列操作中的一个来替换正确的单词(从字典中):
-
- 从单词中删除一个字母;
-
- 用一个任意字母替换单词中的一个字母;
-
- 在单词中插入一个任意字母。
你的任务是编写一个程序,为每个给定的单词找到字典中所有可能的替换。
- 在单词中插入一个任意字母。
【输入形式】
输入的第一部分包含所有字典中的词,每个单词占用一行,以一个单一字符“#”作为结束。所有单词都不相同,字典中至多1000个单词。
接下来的部分包含所有需要进行检查的单词,同样每个单词占用一行。这部分也以一个单一字符“#”作为结束。至多有50个单词需要检查。
在输入中所有的单词(字典中的和需要检查的)都仅由小写字母组成,每个最多包含15个字符。
【输出形式】
对于每个在输入中出现的单词,按照它们在输入的第二部分出现的顺序输出一行。如果该单词是正确的(也就是说它包含在字典中)则输出信息:“is correct”;如果该单词不正确,则首先输出该单词,然后输入符号’:’(冒号),之后空一格,写出它所有可能的替代,以空格分隔。这些替代的单词按照它们在字典中(输入的第一部分)出现的顺序写出。如果没有可替代的单词,则在冒号后面直接输出换行。
【样例输入】
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
【样例输出】
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
题解
思路
- 1.查询问题,我直接滋map
- 2.仔细一看,单词还有三个变化,删除,替换,插入,字母有26个,替换和插入会比较麻烦,我们逆向思维。变成同时删除和字典删除
- 3.通过2,我们简化了操作,但是还需要剪枝就是对字符串操作前先进行一个长度判读,比如替换,就需要长度相同
- 4.结果全部存到结构体的vector中,方便后面进行输入循序的排序,因为map查出来的,是没有顺序的
- 注意:就是输出格式注意一下吧,然后单词最多15个字符。这就比26个字母要短,所以反向思维不仅更简单而且还能降低复杂度!!!
代码
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
/***********************vector_单词的结构体********************************/
struct word{
string name;//单词名字
int index;//输入的顺序
};
/***********************vector_单词排序规则********************************/
bool cmp(word a,word b){
return a.index<b.index;
}
/*******************获得单词删除一个字母后的vector*************************/
vector<string> get_delete_vector(string str){
vector<string> str_delete;
if(str.length()==1) return str_delete;
for(int i=0;i<str.length();i++){
string temp=str;
temp.erase(i,1);
str_delete.push_back(temp);
}
return str_delete;
}
int main(){
map<string,int> m;//用来查询字母
string str;
int cnt=0;//用来继续输入的顺序
while(cin>>str&&str!="#"){
m[str]=cnt;//记录输入的顺序,方便后面vector排序
cnt++;
}
while(cin>>str&&str!="#"){
vector<word> ans;//用来存放查询结果的vector
if(m.find(str)!=m.end()){
cout<<str<<" is correct"<<endl;
}else{
word temp;
map<string,int>::iterator it,it2;
/*******************删除一个字母部分**************/
vector<string> word_delete=get_delete_vector(str);
for(int i=0;i<word_delete.size();i++){
if(m.find(word_delete[i])!=m.end()){
it2=m.find(word_delete[i]);
temp.name=it2->first;
temp.index=it2->second;
ans.push_back(temp);
}
}
/*****************替换单词中的一个字母************/
for(it=m.begin();it!=m.end();it++){
if(it->first.length()==str.length()){//长度符合要求 需要相同,然后删除同位的某个字母
for(int i=0;i<str.length();i++){
string temp1=str,temp2=it->first;
temp1.erase(i,1);
temp2.erase(i,1);
if(temp1==temp2){
temp.name=it->first;
temp.index=it->second;
ans.push_back(temp);
break;
}
}
}
}
/*******************插入一个字母******************/
/*理解为字典里面的删除一个字母等于现在的字母*/
for(it=m.begin();it!=m.end();it++){
if(it->first.length()-1==str.length()){//长度要符合要求
for(int i=0;i<it->first.length();i++){
string temp1=it->first;
temp1.erase(i,1);
if(temp1==str){
temp.name=it->first;
temp.index=it->second;
ans.push_back(temp);
break;
}
}
}
}
sort(ans.begin(),ans.end(),cmp);
cout<<str<<": ";
for(int i=0;i<ans.size();i++){
cout<<ans[i].name<<" ";
}
cout<<endl;
}
}
return 0;
}