zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    vue简单分屏(1,4,9,16),全屏,还需要调整
    vue 中的Vuex实践
    常用工具类
    java Log日志规范
    spring中对象的注入方式
    不恰当的update语句使用主键和索引导致mysql死锁
    JPA规范及其它持久层框架
    webservice基本功能介绍
    Activiti工作流(二)之常用操作
    Activiti工作流(一)之基本操作介绍
  • 原文地址:https://www.cnblogs.com/radical/p/3766438.html
Copyright © 2011-2022 走看看