1. 程式人生 > >poj 1035KINA Is Not Abbreviation

poj 1035KINA Is Not Abbreviation

std 字母數 pri 數組 找到 i++ ring 字符 sin

題目鏈接: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 }

poj 1035KINA Is Not Abbreviation