poj 1035 Spell checker(字符串处理)
来源:互联网 发布:nba怎么查询端口号 编辑:程序博客网 时间:2024/04/30 05:31
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
iishashavebemymorecontestmetooifaward#meawaremcontesthavooorifimre#
Sample Output
me is correctaware: awardm: i my mecontest is correcthav: has haveoo: tooor:i is correctfi: imre: more me
题意:
输入一部字典,以#结束;然后输入单词,检查这个单词在不在字典中,规则如下,输入以#结束
1、 若这个单词能在字典中找到,则输出is corret
2、 若这个单词能通过 替换一个字符 或 删除一个字符 或 添加一个字符 后,在字典中找得到,则输出这些单词,输出顺序根据 输入的那部字典的字典序
3、 若某个单词无论操作与否都无法在字典中找得到,则输出空,就是直接换行
解题思路:
字符串处理,就是逐个比较 待查单词 与 字典单词 的长度,当且仅当两者长度之差的绝对值<=1时才进行检查操作。暴力模拟
#include <iostream>#include <string.h>using namespace std;char dict[10001][16],str[16]; //dict数组用来存储字典(最多10000个单词,每个单词长度最多15),str用来存储待查的单词int len[10001]={0},num1=0,len1; //len数组是与dict数组对应的,用来存储每个单词的长度,num1记录字典中单词的个数,len1是待 查单词的长度void check(char *str1,char *str2){ int dis=0; //dis用来存有多少个字符不一样 for(int i=0;i<len1;i++) if(str1[i]==str2[i]) continue; else dis++; if(dis>1) //两个单词不同的字符数必须只有一个 return; else { cout<<" "<<str2;return;} //符合规则就将单词输出}
void Add(char *str1,char *str2){ int dis=0; //dis用来存有多少个字符不一样 for(int i=0,j=0;i<strlen(str2);i++) // 待测单词长度短,所以我们以字典中的单词为母串,i是str2的下标,j是str1的下标 if(str1[j]!=str2[i]) dis++; else j++; if(dis>1) //两个单词不同的字符数必须只有一个 return; else { cout<<" "<<str2;return; //符合规则就将单词输出 }}
void delect(char *str1,char *str2){ int dis=0; //dis用来存有多少个字符不一样 for(int i=0,j=0;i<len1;i++) // 待测单词长度长,所以我们以待测单词为母串,i是str1的下标,j是str2的下标 if(str1[i]!=str2[j]) dis++; else j++; if(dis>1) //两个单词不同的字符数必须只有一个 return; else { cout<<" "<<str2;return; //符合规则就将单词输出 }}int main(){ while(cin>>dict[num1]&&dict[num1][0]!='#') //输入字典中的单词,以#结束 { len[num1]=strlen(dict[num1]); //同时记录每个单词的长度 num1++; //个数增1 } while(cin>>str&&str[0]!='#') //输入待查单词,以#结束 { int flag1=0,i; //flag1作为标志is correct的情况 len1=strlen(str); //计算待查单词的长度 for(i=0;i<num1;i++) //先看看待测单词在字典中是不是存在 if(strcmp(str,dict[i])==0) { flag1=1; //存在的话flag1为真, break; } if(flag1==1) cout<<str<<" is correct"<<endl; else //不存在此单词,就开始3种变换 { cout<<str<<":"; for(i=0;i<num1;i++) //遍历保证按照字典中的顺序输出 { if(len1==len[i]) //长度相等时就替换一个字符 { check(str,dict[i]); } else if(len[i]-len1==1) //待测单词长度短1个,就添加一个字符 { Add(str,dict[i]); } else if(len1-len[i]==1) //待测单词长度长1个,就删除一个字符 { delect(str,dict[i]); } } cout<<endl; } } return 0;}
- poj 1035 Spell checker(字符串处理)
- POJ 1035 Spell checker(字符串处理)
- poj 1035 Spell checker ( 字符串处理 )
- POJ Spell checker(1035)-字符串处理
- POJ 1035 Spell checker【字符串暴力处理】
- 字符串 模拟 Spell checker (poj 1035)
- POJ 1035 Spell checker(字符串比较)
- POJ 1035 Spell checker 字符串
- POJ 1035-Spell checker(字符串)
- POJ 1035 Spell checker (字符串)
- Spell checker (poj 1035 字符串)
- poj 1035 Spell checker &nyoj 162 Spell checker(优化)
- POJ 1035 Spell checker (字符串模拟)
- POJ 1035:Spell checker:字符串操作
- poj 1035 Spell checker 字符串,暴力
- POJ 1035 Spell checker 字符串暴力
- POJ 1035 Spell checker 字符串容错检查
- POJ 1035 Spell checker 【字符串暴力查找】
- Java+Swing+SQL+Server2005开发酒店管理系统源代码下载
- activeX com 组件开发 一
- Packet Tracer 5.0实验(一) 交换机的基本配置与管理
- emulator:ERROR:This AVD's configuration is missing a kernel file! 解决方法
- Tencent Position & Organize
- poj 1035 Spell checker(字符串处理)
- 【移动开发】Ken Burns特效的幻灯片
- MFC颜色组合框
- 单源最短路径算法之Bellman-Ford算法
- 内的异常功能
- aircrack-ng 添加Mac OS X 支持 airodump-ng和aireplay-ng都可以用了
- Redis常用命令
- C# struct的陷阱:无法修改“...”的返回值,因为它不是变量
- 抛出一个异常