现在有一些英语单词需要做拼写检查,你的工具是一本词典。需要检查的单词,有的是词典中的单词,有的与词典中的单词相似,你的任务是发现这两种情况。单词A与单词B相似的情况有三种:
1、删除单词A的一个字母后得到单词B;
2、用任意一个字母替换单词A的一个字母后得到单词B;
3、在单词A的任意位置增加一个字母后得到单词B。
你的任务是发现词典中与给定单词相同或相似的单词。
输入
第一部分是词典中的单词,从第一行开始每行一个单词,以"#"结束。词典中的单词保证不重复,最多有10000个。
第二部分是需要查询的单词,每行一个,以"#"结束。最多有50个需要查询的单词。
词典中的单词和需要查询的单词均由小写字母组成,最多包含15个字符。
输出
按照输入的顺序,为每个需要检查的单词输出一行。如果需要检查的单词出现在词典中,输出“?x is correct",?x代表需要检查的单词。如果需要检查的单词没有出现在词典中,则输出"?x: ?x1 ?x2 ...?xn",其中?x代表需要检查的单词,?x1...?xn代表词典中与需要检查的单词相似的单词,这些单词中间以空格隔开。如果没有相似的单词,输出"?x:"即可。
样例输入
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
我觉得可能吧有简单的方法,但是我懒(用烂的理由...)
就是输入之后然后判断遍历,然后如果有相似的就存到一个vector里,刚好碰到相等的直接搞出来,然后把之前的vector更新
之前一直不知道为啥RE RE RE RE RE RE RE,以为自己得string写越界了或者过了
后来发现字典里有10000个,我开了100
打扰了,心态崩了
#include<iostream>
#include<string>
#include<vector>
#include<queue>
/*有无快点的方法
RE代码
*/
using namespace std;
string dic[10005];//整本的词典
vector<int>ready(100);//暂时扫到的东西
int dif1(string a, string b)//相似的
{
int dos = abs(int(a.length() - b.length()));
if ( dos > 1)
{
return -1;//不相似的-1
}
if (a.length() == b.length())
{
int cnt=0;
for (int i = 0; i < a.length(); i++)
{
if (a[i] != b[i])
{
cnt++;
}
}
if (cnt == 0)
{
return 1;//相等为1
}
else if (cnt == 1)
{
return 0;//相似的为0
}
return -1;
}
if (a.length() > b.length())//删除,出问题了
{
bool flag = 1;
for (int i = 0; i < b.length(); i++)
{
if (a[i] != b[i])
{
for (int j = i; j < b.length(); j++)
{
if (a[j + 1] != b[j])
{
return -1;
}
}
flag = 0;
break;
}
}
return 0;
}
if (a.length() < b.length())//添加的问题,添加多了?
{
bool flag = 1;
for (int i = 0; i < a.length(); i++)
{
if (a[i] != b[i])
{
for (int j = i; j < a.length(); j++)
{
if (b[j + 1] != a[j])
{
return -1;
}
}
flag = 0;
break;
}
}
return 0;
}
}
int main()
{
string a;
int cnt = 0;
while (cin >> a)
{
if (a == "#")
{
break;
}
dic[cnt++] = a;
}
while (cin >> a)
{
if (ready.size() != 0)
{
ready.clear();
}
if (a == "#")return 0;
bool ri = false;
for (int i = 0; i < cnt; i++)
{
int k = dif1(a, dic[i]);
if (k == -1)continue;
else if (k == 0)
{
ready.push_back(i);
}
else//找到相等的
{
ri = true;
}
}
if (ri)
{
printf("%s is correct\n", a.c_str());
}
else
{
printf("%s:", a.c_str());
if (ready.size() != 0)
{
for (int i = 0; i < ready.size(); i++)
{
printf(" %s", dic[ready[i]].c_str());
}
}
printf("\n");
}
}
return 0;
}