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


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

    一段 暴力 TLE --

    一段 AC ---

    暴力 : TLE

     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
    ",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
    ");
    41         else
    42         {
    43             for( j = 0 ; j<num ; j++ )
    44                 printf( "%c",str[j] );
    45             printf( " %d
    ",num );
    46         }        
    47     }
    48     return 0;
    49 }
    View Code

    稍微优化 剪枝: AC

     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
    ",i+1 );
    42                     temp = true; //标记 跳出循环 
    43                 }
    44             }
    45             flag = true; //重置标记
    46             i--;  //继续往前移动
    47             if( temp )
    48                 break;
    49         }
    50         if( !temp )
    51             printf("0
    ");    
    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.

        

    just follow your heart
  • 相关阅读:
    Java设计模式—状态模式
    Java设计模式—备忘录模式
    android AsyncTask介绍
    Android UI线程和非UI线程
    Java设计模式—代理模式
    Java设计模式—命令模式
    <Android 应用 之路> MPAndroidChart~PieChart
    FPGA的EPCS 配置的2种方法 FPGA下载程序的方法(EPCS)
    如何将.sof转换成.jic
    quartus ii工程文件的分析
  • 原文地址:https://www.cnblogs.com/radical/p/3766438.html
Copyright © 2020-2023  润新知