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;
}


你可能感兴趣的:(character,poj)