CodeForces 25E Test KMP

E. Test
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
ab
bc
cd
output
4
input
abacaba
abaaba
x
output
11
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<string.h>
using namespace std;
#define N 200020
#define NN 100010
char str[N];
char s[3][NN];
int p[N];

int kmp(char *s1,char *s2)//返回s2中剩下的字符个数
{
    int n=strlen(s1);
    int m=strlen(s2);
    int j=-1;
    p[0]=-1;
    for(int k=1;k<m;k++)
    {
        while(j>=0&&s2[k]!=s2[j+1]) j=p[j];
        if(s2[k]==s2[j+1]) j++;
        p[k]=j;
    }

    j=-1;
    for(int k=0;k<n;k++)
    {
        if(j==m-1) return 0;
        while(j>=0&&s1[k]!=s2[j+1]) j=p[j];
        if(s1[k]==s2[j+1]) j++;
    }
    return (m-1-j);
}
int compair(char *s1,char *s2,char *s3)
{
    int len=kmp(s1,s2);
    str[0]='\0';
    strcat(str,s1);
    strcat(str,s2+strlen(s2)-len);
    len=kmp(str,s3);
    return strlen(str)+len;
}
int main()
{
    while(scanf("%s%s%s",s[0],s[1],s[2])!=EOF)
    {
        int minn;
        int l;
        minn=compair(s[0],s[1],s[2]);
       // if(l<minn)   minn=l;

        l=compair(s[0],s[2],s[1]);
        if(l<minn)   minn=l;

        l=compair(s[1],s[2],s[0]);
        if(l<minn)   minn=l;

        l=compair(s[1],s[0],s[2]);
        if(l<minn)   minn=l;

        l=compair(s[2],s[0],s[1]);
        if(l<minn)   minn=l;

        l=compair(s[2],s[1],s[0]);
        if(l<minn)   minn=l;

        cout<<minn<<endl;
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值