您的位置:首页 > 其它

CodeForces 25E Test KMP

2013-02-09 11:35 405 查看
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 s1, s2 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
;
char s[3][NN];
int p
;

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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: