POJ Spell checker(1035)-字符串处理

题意: 在给定的字典中查找单词;  

        对于每个要检查的单词,若有错,有3种情况:

        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. 

题解: 由于多个解时要按给出字典序输出, 故按顺序查找判断即可; 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>`
#include <algorithm>
#include <cctype>
#include <set>
#include <vector>
#define INF 0x7fffffff
#define eps (1e-9)
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
int n,m,tot=0;
char dic[10009][20];
int check(char c[],char s[]){
    int i,cnt =0;
    int ls=strlen(s) ,lc=strlen(c);
    //printf("\n-s %s %d c-%s %d --        ",s,ls,c,lc );
    if(ls==lc){
       for(i=0;i<ls;i++)   if(c[i]!=s[i])  cnt++;
       if(cnt==1)   return 1;              return 0;
    }
    if(ls-lc ==1){
       int pc=0,ps=0;
       for(i=0;i<ls;i++){
           if(s[ps]!=c[pc]){  if(ps!=pc)   return 0;  ps++;  }
           else{     ps++;       pc++;     }
       }   return 1;
    }
    if(lc-ls ==1){
        int pc=0,ps=0;
        for(i=0;i<lc;i++){
            if(c[pc]!=s[ps]){ if(pc!=ps)   return 0;  pc++;  }
            else{     ps++;      pc++;     }
        }   return 1;
    }
    return 0;
}
int indic(char s[]){
    for(int i=0;i< tot;i++){
        if(strcmp(s,dic[i])==0)  return 1;
    }   return 0;
}
int main()
{
    //freopen("D:\data.txt","r",stdin);
    int i,k;      char stp[20];
    while(1){
       scanf("%s",dic[tot]);
       if(dic[tot][0]=='#') break;       tot++;
    }
    while(1){
       scanf("%s",stp);               if(stp[0]=='#')  break;
       if(indic(stp))
       {        printf("%s is correct\n",stp);         continue;     }
       printf("%s:",stp);
       for(i=0;i< tot;i++){
           if(check(dic[i],stp))         printf(" %s",dic[i]);
       }
       putchar(10);
    }
    return 0;
}


转载于:https://www.cnblogs.com/shiAokai/p/3967500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值