码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1035

时间:2016-05-14 22:57:02      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

poj 1035

标签:

原文地址:http://www.cnblogs.com/Tree-dream/p/5493771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!