poj 1035 <set + 指针移动 解决 单词检测>

来源:互联网 发布:magento2 cms block 编辑:程序博客网 时间:2024/06/06 00:04

Spell checker
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 24347 Accepted: 8879

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

iishashavebemymorecontestmetooifaward#meawaremcontesthavooorifimre#

Sample Output

me is correctaware: awardm: i my mecontest is correcthav: has haveoo: tooor:i is correctfi: imre: 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;}



0 0