poj 1035 <set + 指针移动 解决 单词检测>
2016-07-30 09:31
375 查看
Spell checker
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
Sample Output
Source
Northeastern Europe 1998
看学长写的好嗨皮----学长博客
题意:
先给出一个字典,然后输入单词--让你判断这个单词在字典中是否存在--不存在时看是否可以通过换一个字符--或增一个字符--或减一个字符来得到一个单词
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;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24347 | Accepted: 8879 |
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
Source
Northeastern Europe 1998
看学长写的好嗨皮----学长博客
题意:
先给出一个字典,然后输入单词--让你判断这个单词在字典中是否存在--不存在时看是否可以通过换一个字符--或增一个字符--或减一个字符来得到一个单词
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;
}
相关文章推荐
- 一段简单的代码,实现iOS源生播放器,在线播放视频
- NYOJ 106 背包问题(贪心,经典题目)
- StrictMode使用详解,strictmode
- Java学习之线程锁--单例模式写法--synchronized
- BSOJ1878 山头狙击战 解题报告
- iOS 第三方框架- AFNetworking的基本使用
- Java 将字节数组转化为16进制的多种方案
- 这一节我们来看看异常链
- 《书读完了》—— 随笔
- 多文件,大数据下载优化
- 微信红包的架构设计简介{转}
- 面试常考
- Python使用SMTP发送邮件(163,yeah等网易邮箱已测试可以)
- 算法4_查找算法
- js实现继承的三种方式
- Git远程操作详解
- Java中byte与16进制字符串的互相转换
- HDU 3065 病毒侵袭持续中 (AC自动机 + hdu有毒)
- Android RecyclerView 使用完全解析 体验艺术般的控件
- 在Linux环境下mysql的root密码忘记解决方法 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库。 2.修改MySQL的登录设置: # vi /etc/my.c