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

TOJ---2754---类似于字符串匹配

时间:2014-06-06 16:22:49      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

先去开撸了 把代码 贴上  ^ ^

一段 暴力 TLE --

一段 AC ---

 

暴力 : TLE

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 const int size = 50010;
 7 char str[size]; // 开头
 8 char ch[size];  // 结尾
 9 
10 int main()
11 {
12     int i , j;
13     int len1 , len2;
14     int num;
15     bool flag;
16     while( ~scanf("%s %s",str,ch) )
17     {
18         flag = false;
19         i = j = num = 0;
20         len1 = strlen(str); // 提供开头字符串
21         len2 = strlen(ch);  // 提供结尾字符串
22         while( i<len2 )
23         {
24             if( ch[i]==str[j] )
25             {
26                 //printf( "%c %c\n",ch[i],str[j] );
27                 j++;
28                 i++;
29                 num++;
30                 if( i==len2-1 )
31                     flag = true;
32             }
33             else
34             {
35                 num = 0;
36                 j = 0;
37             }
38         }
39         if( !flag )
40             printf("0\n");
41         else
42         {
43             for( j = 0 ; j<num ; j++ )
44                 printf( "%c",str[j] );
45             printf( " %d\n",num );
46         }        
47     }
48     return 0;
49 }
View Code

 

稍微优化 剪枝: AC

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 const int size = 50010;
 7 char str[size]; // 开头
 8 char ch[size];  // 结尾
 9 
10 int main()
11 {
12     int i;
13     int len1 , len2;
14     bool flag , temp;
15     while( ~scanf("%s %s",str,ch) )
16     {
17         flag = true;
18         temp = false;
19         len1 = strlen(str); // 提供开头字符串              
20         len2 = strlen(ch);  // 提供结尾字符串 
21         i = len1-1; // 第一个字符串的末坐标
22         while( i>=0 )
23         {
24             if( i<=len2-1 && str[i] == ch[len2-1] && str[0] == ch[len2-1-i] ) //要想接龙成功 那么接龙的字符串长度就是i+1 分别比较2个字符串的开头与末尾
25             {
26                 int t = len2-1-i;
27                 for( int k = 0 ; k<=i ; k++ , t++ )
28                 {
29                     if( str[k]!=ch[t] )
30                     {
31                         flag = false; //标记 失败 break终止比较
32                         break;
33                     }
34                 }
35                 if( flag )  //这里就是 接龙成功输出字符串
36                 {
37                     for( int k = 0 ; k<=i ; k++ , t++ )
38                     {
39                         printf( "%c",str[k] );
40                     }
41                     printf( " %d\n",i+1 );
42                     temp = true; //标记 跳出循环 
43                 }
44             }
45             flag = true; //重置标记
46             i--;  //继续往前移动
47             if( temp )
48                 break;
49         }
50         if( !temp )
51             printf("0\n");    
52     }
53     return 0;
54 }
View Code

 

先去撸了 ~~~

好吧 我的 大锐雯 die ....

回过来  看这题 发现也没什么好写的 。。。。

我将一些细节 在注释里也写了

 

today:  //  今天来句 洋气的

      One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.

    

 

 

TOJ---2754---类似于字符串匹配,布布扣,bubuko.com

TOJ---2754---类似于字符串匹配

标签:c   style   class   blog   code   a   

原文地址:http://www.cnblogs.com/radical/p/3766438.html

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