poj1035Spell checker(字符串模拟)

http://poj.org/problem?id=1035

以前做过很类似的  四种情况分开讨论

View Code
  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 using namespace std;

  6 char str[10010][20],cs[10010][20];

  7 int kk[10010];

  8 int judge1(char *x,char *y)

  9 {

 10     int i,j,k=0;

 11     for(i = 0; i < strlen(x) ; i++)

 12     {

 13         if(x[i]!=y[i])

 14         k++;

 15     }

 16     if(k>1)

 17     return 0;

 18     return 1;

 19 }

 20 int judge2(char *x,char *y)

 21 {

 22     int i,j=0,k=0,flag;

 23     for(i = 0; i <strlen(x)&&j<strlen(y);i++)

 24     {

 25         if(x[i]!=y[j])

 26         {

 27             k++;

 28             i--;

 29         }

 30         j++;

 31     }

 32     if(k>1)



 33     return 0;

 34     return 1;

 35 }

 36 int main()

 37 {

 38     int i,j,k=0,g;

 39     char c[20];

 40     while(cin>>str[k])

 41     {

 42         if(strcmp(str[k],"#")==0)

 43         break;

 44         kk[k] = strlen(str[k]);

 45         k++;

 46     }

 47     int f = 0;

 48     while(cin>>c)

 49     {

 50         if(strcmp(c,"#")==0)

 51         break;

 52         int tk = strlen(c);

 53         g = 0;

 54         f=0;

 55         for(i = 0 ; i < k ; i++)

 56         {

 57             if(tk==kk[i])

 58             {

 59                 if(strcmp(c,str[i])==0)

 60                 {

 61                     f = 1;

 62                     break;

 63                 }

 64                 else

 65                 if(judge1(c,str[i]))

 66                 {

 67                     g++;

 68                     strcpy(cs[g],str[i]);

 69                 }

 70             }

 71             if(tk+1==kk[i])

 72             {

 73                 if(judge2(c,str[i]))

 74                 {

 75                     g++;

 76                     strcpy(cs[g],str[i]);

 77                 }

 78             }

 79             if(tk-1==kk[i])

 80             {

 81                 if(judge2(str[i],c))

 82                 {

 83                     g++;

 84                     strcpy(cs[g],str[i]);

 85                 }

 86             }

 87         }

 88         if(f)

 89         {

 90             printf("%s is correct\n",c);

 91         }

 92         else

 93         {

 94             printf("%s:",c);

 95             for(i = 1 ; i <= g ; i++)

 96             printf(" %s",cs[i]);

 97             puts("");

 98         }

 99     }

100     return 0;

101 }

 

你可能感兴趣的:(check)