poj 1035KINA Is Not Abbreviation

题目链接:http://poj.org/problem?id=4054

本题的题意是在下面那部分待检验的单词中找到与之相对应的正确的代词,包含几种情况,一是全部字母相同,二是有一个字母不相同,三是多一个字母,四是少一个字母。

解本道题目:首先把错误的情况分为三种

1.字母数相同

2.待检验的单词的字母数比正确的单词的字母数多一

3.待检验的单词的字母数比正确的单词的字母数少一

然后注意一下细节;找到前面三种类型的单词,不要急着将单词输出,将这些单词保存在另外一个数组里面,如果遇到正确的单词,直接输出is correct,否则输出数组里面的字符串

代码如下:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 char dic[10005][16],che[55][16];
 6 char res[10005][16];
 7 char temp[16];
 8 int flag;
 9 int num;
10 int main(){
11     int len1=0,len2=0;
12     bool flag;
13     while(1){
14         cin>>dic[len1];
15         if(dic[len1][0]=='#'){
16             break;
17         }
18         len1++;
19     }
20     while(1){
21         cin>>che[len2];
22         if(che[len2][0]=='#'){
23             break;
24         }
25         len2++;
26     }
27     for(int i=0;i<len2;i++){
28         int len4=strlen(che[i]);
29         flag=0;
30         num=0;
31         for(int k=0;k<10005;k++){
32             for(int j=0;j<16;j++){
33                 res[k][j]='\0';
34             }
35         }
36         for(int j=0;j<len1;j++){
37             int len3=strlen(dic[j]);
38             if(len3==len4){                     //字符串长度相等的情况
39                 if(strcmp(che[i],dic[j])==0){
40                     printf("%s is correct",dic[j]);
41                     flag=1;
42                     break;
43                 }
44                 else{
45                     int sum=0;
46                     for(int k=0;k<len3;k++){
47                         if(che[i][k]!=dic[j][k]){
48                             sum++;
49                         }
50                     }
51                     if(sum==1){
52                        strcpy(res[num],dic[j]);
53                        num++;
54                     }
55                 }
56             }
57             else if(len3==len4+1){              //字典里面的单词比待检验的单词的数量多一
58                for(int k=0;k<len3;k++){
59                     strcpy(temp,dic[j]);
60                     for(int h=k;h<len3;h++){
61                         temp[h]=temp[h+1];
62                     }
63                     if(strcmp(temp,che[i])==0){
64                         strcpy(res[num],dic[j]);
65                         num++;
66                         break;
67                     }
68                }
69             }
70             else if(len4==len3+1){               //待检验的单词比字典里面的字母数多一
71                 for(int k=0;k<len4;k++){
72                     strcpy(temp,che[i]);
73                     for(int h=k;h<len4;h++){
74                         temp[h]=temp[h+1];
75                     }
76                     if(strcmp(temp,dic[j])==0){
77                         strcpy(res[num],dic[j]);
78                         num++;
79                         break;
80                     }
81                 }
82             }
83         }
84         if(flag==0){
85            // cout<<num<<endl<<endl;
86             printf("%s: ",che[i]);
87             for(int j=0;j<num;j++){
88                 printf("%s ",res[j]);
89                // cout<<a++<<endl;
90             }
91         }
92         printf("\n");
93     }
94     return  0;
95 }
posted @ 2017-07-19 20:36  木子丘  Views(330)  Comments(0Edit  收藏  举报