题意:给一些单词,然后一些询问,如果能找到原单词,输出correct,否则有下面三种情况:
1.长度相同.
2.带检测字长度比字典里的字少1
3.带检测字长度比字典里的字多1
检查是否只有一个字母不同,输出所以满足相似的单词。
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
const int maxn=100005;
const int INF=0x3f3f3f3f;
typedef long long LL;
using namespace std;
string a[maxn];
int n=0;
void find(string s)
{
//找原单词
for(int i=0;i<n;i++)
{
if(a[i]==s)
{
cout<<s<<" is correct";
return ;
}
}
//检查长度相同或多1或少1的情况
cout<<s<<": ";
for(int i=0;i<n;i++)
{
int len=a[i].length();
if(len==s.length())
{
int t=0;
for(int j=0;j<len;j++)
{
if(a[i][j]==s[j]) t++;
}
if(t==len-1) cout<<a[i]<<" ";
}
else if(len==s.length()-1)
{
int t=0,p=0;
for(int j=0;j<s.length();j++)
{
if(s[j]==a[i][p])
{
t++;
p++;
}
}
if(t==len) cout<<a[i]<<" ";
}
else if(len==s.length()+1)
{
int t=0,p=0;
for(int j=0;j<len;j++)
{
if(a[i][j]==s[p])
{
t++;
p++;
}
}
if(t==s.length()) cout<<a[i]<<" ";
}
}
}
int main()
{
// freopen("E:\\ACM\\test.txt","r",stdin);
string s;
while(cin>>s,s!="#")
{
a[n++]=s;
}
while(cin>>s,s!="#")
{
find(s);
puts("");
}
return 0;
}
本文介绍了一种用于检测输入字符串与预存单词集相似度的算法。该算法能够判断输入字符串是否与预存单词完全一致,或者在长度相同时有一个字符不同、长度相差1时有一个字符不匹配的情况,并输出所有符合条件的相似单词。
181

被折叠的 条评论
为什么被折叠?



