Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24347 | Accepted: 8879 |
Description
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 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
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
Source
看学长写的好嗨皮----学长博客
题意:
先给出一个字典,然后输入单词--让你判断这个单词在字典中是否存在--不存在时看是否可以通过换一个字符--或增一个字符--或减一个字符来得到一个单词
set + 指针的移动来解决这个真的好溜---
#include <set>
如:set<int,less<int> > set1;
less<int>是一个标准类,用于形成升序排列函数对象。降序排列是用greater<int>。
begin() 返回指向第一个元素的迭代器
clear() 清除所有元素
count() 返回某个值元素的个数
empty() 如果集合为空,返回true(真)
end() 返回指向最后一个元素之后的迭代器,不是最后一个元素
equal_range() 返回集合中与给定值相等的上下限的两个迭代器
erase() 删除集合中的元素
find() 返回一个指向被查找到元素的迭代器,如果没找到则返回end()
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个集合变量
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数
代码:
#include<cstring>
#include<iostream>
#include<string>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
struct node{
int len;
char ch[25];
}trie[10010];
int xxx[3]={0,1,1};
int yyy[3]={1,0,1};
int find(char xx[],char yy[])
{
int lx=strlen(xx);
int ly=strlen(yy);
int aa=lx-ly;
if (aa<-1||aa>1)
return 0;
int x=0,y=0;
for (;x<lx&&y<ly&&xx[x]==yy[y];)
x++,y++;
for (int i=0;i<3;i++)//替换--增--减--
{
int xl=x+xxx[i];int yl=y+yyy[i];
if (strcmp(xx+xl,yy+yl)==0) return 1;
}
return 0;
}
int main()
{
set <string> slo;
int kp=0;
while (gets(trie[kp].ch))
{
if (trie[kp].ch[0]=='#')
break;
slo.insert(trie[kp].ch);
trie[kp].len=strlen(trie[kp].ch);
kp++;
}
char ko[25];
while (gets(ko))
{
if (ko[0]=='#')
break;
if (slo.count(ko))
printf("%s is correct\n",ko);
else
{
printf("%s:",ko);
for (int i=0;i<kp;i++)
if (find(ko,trie[i].ch))
printf(" %s",trie[i].ch);
printf("\n");
}
}
return 0;
}