• 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;
    }
    

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

  • 相关阅读:
    Linux常用指令全集
    js判断ie11浏览器
    javascript事件触发器fireEvent和dispatchEvent
    HTML5自定义属性对象Dataset简介
    CommonJS规范
    sql基本语法大全
    (七)make menuconfig
    (六)buildroot使用详解
    (二十一)Makefile例子
    (二十)ubuntu的recovery mode解决用户一些实际问题
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5552088.html
Copyright © 2020-2023  润新知