Codeforces Beta Round #25 (Div. 2 Only)E. Test

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

 KMP

给你3个子串,求最短的原串。

例如

abc

ab

c

最短原串就是abc

/* ***********************************************
Author        :pk29
Created Time  :2015/8/23 18:41:02
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

int f[maxn];
void getFail(string p,int *f){
    int m=p.size();
    f[0]=0;
    f[1]=0;
    for(int i=1;i<m;i++){
        int j=f[i];
        while(j&&p[i]!=p[j])j=f[j];
        f[i+1]=p[i]==p[j]?j+1:0;
    }
}
int find(string t,string p,int *f){
    int n=t.size(),m=p.size();
    getFail(p,f);
    int j=0;
    for(int i=0;i<n;i++){
        while(j&&p[j]!=t[i])j=f[j];
        if(p[j]==t[i])j++;
        if(j==m)return 0;//一开始没考率到主串包含子串,wa成狗
    }
    return m-j;
}
 
string s[3];
int arr[3]={0,1,2};
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    while(cin>>s[0]>>s[1]>>s[2]){
        if(s[0]>s[1])swap(s[0],s[1]);
        if(s[1]>s[2])swap(s[1],s[2]);
        if(s[0]>s[1])swap(s[0],s[1]);
        //next_permutation()使用前得先排序
        int ans=INF;
        int num=1;
        do{
            string t="";
            t+=s[0];
            int a=find(t,s[1],f);
            t+=s[1].substr(s[1].size()-a,a);
            int b=find(t,s[2],f);
            int x=t.size()+b;
            ans=min(ans,x);
        }while(next_permutation(s,s+3));
        cout<<ans<<endl;
    }
    return 0;
}