POJ 1035 Spell checker(字符串比较)

来源:互联网 发布:sql注入语句 编辑:程序博客网 时间:2024/04/28 20:09
题意

输入一部字典,输入若干单词

1、  若某个单词能在字典中找到,则输出corret

2、  若某个单词能通过 变换删除添加一个字符后,在字典中找得到,则输出这些单词,输出顺序根据  输入的那部字典的字典序

3、  若某个单词无论操作与否都无法在字典中找得到,则输出空


POJ 1035


分析

存下字典单词,对每个要查找的单词,暴力扫描字典单词,根据单词长度,选择操作并检查。


#include<iostream>#include<cstring>#include<cstdio>using namespace std;char dic[10002][17],temp[20];int ans[10002];int sum,len_t;void fun(){    int len_dic,cnt=0;    for(int i=0;i<sum;i++){        int dif=0,k,p;        len_dic=strlen(dic[i]);        if(len_dic==len_t){            for(k=0;k<len_dic;k++){                if(temp[k]!=dic[i][k]) dif++;                if(dif>=2) break;            }            if(dif==0) {                cout<<temp<<" is correct"<<endl;                return ;            }            else if(dif==1) ans[cnt++]=i;        }        else if(len_dic==len_t-1){            for(k=0,p=0;k<len_t;k++,p++){                if(temp[k]!=dic[i][p]){                    dif++;p--;                }                if(dif>=2) break;            }            if(dif==1) ans[cnt++]=i;        }        else if(len_dic==len_t+1){            for(k=0,p=0;p<len_dic;k++,p++){                if(temp[k]!=dic[i][p]){                    dif++;k--;                }                if(dif>=2) break;            }            if(dif==1) ans[cnt++]=i;        }    }    cout<<temp<<":";    for(int i=0;i<cnt;i++) cout<<" "<<dic[ans[i]];    cout<<endl;}int main(){    //freopen("in.txt","r",stdin);    sum=0;    while(gets(dic[sum])&&dic[sum][0]!='#') sum++;    while(gets(temp)&&temp[0]!='#'){        len_t=strlen(temp);        fun();    }}



0 0
原创粉丝点击