poj 1035
http://poj.org/problem?id=1035
poj的一道字符串的水題,不難,但就是細節問題我也wa了幾次
?
題意就是給你一個字典,再給你一些字符,首先如果字典中有這個字符串,則直接輸出,如果沒有的話,那就找字符串加一個字符或少一個字符或者換一個字符是否可以在字典中找到相應的字符串
?
解題思路:我是用string類型的,比較方便看兩個字符串是否相等,用char的話,就是strcmp函數也行。
如果找不到相等的,那么久分別在字典中找到與這個字符串的長度相差1的或者相等的。
然后匹配,如果匹配的結果相差一個則輸出
?
1 #include <stdio.h> 2 #include <string.h> 3 #include <string> 4 #include <iostream> 5 #include <stdlib.h> 6 7 using namespace std; 8 9 string str[10005],str1[10005]; 10 11 int main() 12 { 13 int dic=0,need=0; 14 while(cin>>str[dic]){ 15 if(str[dic]=="#") break; 16 dic++; 17 } 18 while(cin>>str1[need]){ 19 if(str1[need]=="#") break; 20 need++; 21 } 22 //qsort(str,dic,sizeof(str[0]),cmp); //沒用的,最開始我是以為要對字典排序輸出,其實并不用 23 for(int i=0;i<need;i++){ 24 int flog=0; //標記,如果找得到相同的字符串,則continue。 25 for(int j=0;j<dic;j++){ 26 if(str1[i]==str[j]) { 27 cout<<str1[i]<<" is correct"<<endl; 28 flog=1; 29 break; 30 } 31 } 32 if(flog==1) continue; 33 int len=str1[i].size(); 34 cout<<str1[i]<<":"; 35 for(int j=0;j<dic;j++){ 36 int strl=str[j].size(); 37 if(strl==len||strl==len+1||strl==len-1){ //字符串相差1的或者相等的,就用來匹配是否有可能相差一個字符,這是一種減枝的辦法。 38 int ans=0; 39 if(len>strl){ //吧那個較長的字符作為被匹配的,用短的來匹配長的字符串。 40 for(int m=0,d=0;m<len;m++){ 41 if(str1[i][m]==str[j][d]){ 42 ans++; 43 d++; 44 } 45 } 46 }else if(len<strl){ 47 for(int m=0,d=0;m<strl;m++){ 48 if(str[j][m]==str1[i][d]){ 49 ans++; 50 d++; 51 } 52 } 53 }else if(len==strl){ 54 for(int m=0,d=0;m<strl;m++,d++) 55 if(str[j][m]==str1[i][d]) ans++; 56 } 57 if(len>=strl&&ans==len-1) cout<<" "<<str[j]; 58 if(len<strl&&ans==len) cout<<" "<<str[j]; 59 } 60 } 61 cout<<endl; 62 } 63 return 0; 64 }?
轉載于:https://www.cnblogs.com/Tree-dream/p/5493771.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 安卓活动间的传值问题
- 下一篇: 详细解读神经网络十大误解,再也不会弄错它