zoukankan      html  css  js  c++  java
  • CF 25 E 三个字符串 KMP模板

    Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

     Status

    Description

    Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?

    Input

    There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.

    Output

    Output one number — what is minimal length of the string, containing s1s2 and s3 as substrings.

    Sample Input

    Input
    ab
    bc
    cd
    Output
    4
    Input
    abacaba
    abaaba
    x
    Output
    11

    Source

    题意:将3个字符串连接起来,重复的可以重叠不用计算,输出最短的长度
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 100000+20;
    char s[4][maxn];
    int next[4][maxn],len[5];
    
    void get_next(int i)
    {
        int k=-1,j=0;
        next[i][0]=-1;
        while(j<len[i])
           if(k==-1||s[i][j]==s[i][k])
             {
                k++;
                j++;
                next[i][j]=k;
             }
           else k=next[i][k];
    }
    
    int kmp(int a,int b)
    {
         int  i=0,j=0;
         while(j<len[b]&&i<len[a])
         if(i==-1||s[a][i]==s[b][j])
            {
                i++;j++;
            }
         else i=next[a][i];
         return i;
    }
    
    int main()
    {
        while(~scanf("%s %s %s",s[0],s[1],s[2]))
        {
            int ans=0;
            len[0]=strlen(s[0]);
            len[1]=strlen(s[1]);
            len[2]=strlen(s[2]);
            for(int i=0;i<3;i++) get_next(i);
    
            for(int i=0;i<3;i++)
              for(int j=0;j<3;j++)
                  {
                      if(i==j) continue;
                      for(int k=0;k<3;k++)
                      {
                          if(k==i||k==j) continue;
                          int x=kmp(i,j);
                          int y1=kmp(k,i);
                          int y2=kmp(k,j);
                          ans=max(ans,max(x+y1,x+y2));
                      }
                  }
            printf("%d
    ",len[0]+len[1]+len[2]-ans);
        }
        return 0;
    }
    

      分析:只要求出三个字符串所能得到的最大匹配数就好,

  • 相关阅读:
    vue中$refs、$slot、$nextTick相关的语法
    js中hash、hashchange事件
    js中filter的用法
    ES6新特性-函数的简写(箭头函数)
    js中把ajax获取的数据转化成树状结构(并做成多级联动效果)
    jq中get()和eq()的区别
    new Date() 日期格式处理
    微信小程序 加载图片时,先拉长,再恢复正常
    一个例子理解ES6的yield关键字
    eclipse在光标停留在同一对象的背景色提示,开启与关闭
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5552088.html
Copyright © 2011-2022 走看看