串 --- 匹配
Spell checker
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18351 | Accepted: 6732 |
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.
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.
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
【题目来源】
【题目大意】
首先输入一个字典,然后输入要检查的字符串,对每个字符串进行检查,题目很好理解,这儿就不多说了。
【题目分析】
1.整体思路:字典输入完成后,对输入的每个字符串进行比较,暴力枚举;
2.对于每个单词如果完全匹配,立即输出字典中匹配的单词,并结束本次比较,继续下一个字典单词的比较;
如果未完全匹配,则有:
1.Len1==len2
2.len1-len2==1
3.len1-len2==-1
无论哪种情况,都找到第一个不同的字母,跳过,如果后面字母完全相同,则满足条件,加入canbe
情况1中,两个单词均跳过本字母
情况2,3中,只在较长单词中跳过本字母
还有就是注意输出格式,为此献了3次pe;
#include<cmath> #include<cstdio> #include<string> #include<iostream> #include<algorithm> using namespace std; string a[10005],b[55],ans[10001]; bool check(string x,string y) { int len1=x.size(); int len2=y.size(); if(abs(len1-len2)>1) return 0; if(len1==len2) { int ans=0; for(int i=0;i<len2;i++) { if(x[i]!=y[i]) { ans++; if(ans>1) return 0; } } return 1; } else if(len1>len2) { int ans=0; for(int i=0,j=0;i<len1&&j<len2;) { if(x[i]!=y[j]) { i++; ans++; if(ans>1) return 0; } else { i++; j++; } } return 1; } else { int ans=0; for(int i=0,j=0;i<len1&&j<len2;) { if(x[i]!=y[j]) { j++; ans++; if(ans>1) return 0; } else { i++; j++; } } return 1; } } int main() { string s; int n=0,m=0; while(cin>>s && s[0]!='#') { a[n++]=s; } while(cin>>s && s[0]!='#') { b[m++]=s; } for(int i=0;i<m;i++) { int k=0,flag=0; for(int j=0;j<n;j++) { if(b[i]==a[j]) { flag=1; cout<<b[i]<<" is correct"<<endl; break; } else { if(check(b[i],a[j])) ans[k++]=a[j]; } } if(!flag) { cout<<b[i]<<':'; for(int j=0;j<k;j++) cout<<' '<<ans[j]; printf("\n"); } } return 0; }
Spell checker
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18351 | Accepted: 6732 |
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.
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.
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
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3757928.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?