POJ 1035 Spell checker(水~)
来源:互联网 发布:c语言书籍哪本好 编辑:程序博客网 时间:2024/05/16 12:37
Description
输入一部字典,输入若干单词
1、 若某个单词能在字典中找到,则输出corret
2、 若某个单词能通过变换或删除或添加一个字符后,在字典中找得到,则输出这些单词,输出顺序根据 输入的那部字典的字典序
3、 若某个单词无论操作与否都无法在字典中找得到,则输出空
Input
一部字典,以#结束字典输入,多次单词查询,以#结束输入
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
Solution
字符串处理
对于每个查询的单词,对字典中单词逐个比较,只有当两者长度差<=1的时候才检查操作
Code
#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<algorithm>#include<vector>using namespace std;vector<string>s;int main(){ string c; while(1)//输入字典 { cin>>c; if(c=="#") break; else s.push_back(c); } while(1)//多次查询 { cin>>c; if(c=="#") break; cout<<c;//按格式输出 int flag=0; for(int i=0;i<s.size();i++)//找到相同单词 if(c==s[i]) flag=1; if(flag) { cout<<" is correct"<<endl; continue; } cout<<": ";//按格式输出 for(int i=0;i<s.size();i++)//检查字典中每个单词 { int t=s[i].length()-c.length(); if(t<-1||t>1)//长度差>1,不符合条件 continue; if(t==0)//若长度相同则判断是否只相差一个字符 { int res=0;//不同的字符个数 for(int j=0;j<s[i].length();j++) if(s[i][j]!=c[j]) res++; if(res==1)//若只相差一个字符则满足条件 cout<<s[i]<<" ";//按格式输出 } else if(t==-1||t==1)//若长度相差一则判断是否只增添或删减了一个字符 { if(t==1)//删减一个字符 { int res=0;//相同的字符数 for(int j=0;j<s[i].size();j++) if(s[i][j]==c[res]) res++; if(res==c.size())//满足条件 cout<<s[i]<<" ";//按格式输出 } if(t==-1)//增添一个字符 { int res=0;//相同的字符数 for(int j=0;j<c.size();j++) if(s[i][res]==c[j]) res++; if(res==s[i].size())//满足条件 cout<<s[i]<<" ";//按格式输出 } } } cout<<endl;//按格式输出 } return 0;}
0 0
- POJ 1035Spell checker(水)
- POJ 1035 Spell checker(水~)
- poj 1035 Spell checker &nyoj 162 Spell checker(优化)
- poj-1035-Spell checker
- poj 1035 Spell checker
- Poj 1035 --Spell checker
- Spell checker--POJ 1035
- poj 1035 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
- poj 1035 Spell checker
- POJ 1035 Spell checker
- POJ 1035 Spell checker
- poj 1035 Spell checker
- To_review_100_1---循环链接表的整理
- xml中常用的转义符
- github命令行上传项目的莫名的坑
- xcode 快捷键
- leetcode:N-queens
- POJ 1035 Spell checker(水~)
- 文件之标准/O
- Lua 与C/C++ 交互系列: Lua调用C/C++函数(1).
- 黑马程序员——JAVA笔记——单例设计模式
- Hadoop旅程开始
- 工厂方法——探索之旅
- LeetCode Letter Combinations of a Phone Number
- 日语学习之沪江N3基础 20150620 -1
- New XAMPP security concept: Access to the requested object is only available from the local network