Codeforces 25.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.

Examples
input
ab
bc
cd
output
4
input
abacaba
abaaba
x
output
11

 题目大意:给三个字符串,求一个字符串包含这3个字符串,输出满足要求的字符串的最小长度.

分析:思路很直观.先枚举两个字符串,看它们之间是否互相包含.如果是的,则看其中的大串与第三个串是否互相包含,如果是,则返回最大长度,否则分类讨论两种串的拼接情况.

          如果3个串两两都不包含,则枚举连接情况,用三个串的总长度-连接处的长度。关于怎么求相交的长度,可以枚举这个长度,再来判断hash是否相等.利用hash值的计算公式可以快速求出一个子串的hash值(类似于前缀和).

          犯了一个错:返回的hash值习惯性的用int来存储了,我的hash利用的是unsigned long long的自然溢出,所以在内存要求不是很紧的情况下尽量变量都用unsigned long long.

#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

const ull mod = 1e8+7;
char s[4][100010];
int len[4],sum,id[4];
ull has[4][100010],bpow[100010];

void init()
{
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= len[i]; j++)
            has[i][j] = has[i][j - 1] * mod + s[i][j];
}

ull get(int pos,int cur,int lenn)  //s[pos][cur......cur + len]
{
    return has[pos][cur + lenn] - has[pos][cur - 1] * bpow[lenn + 1];
}

bool contain(int a,int b)
{
    if (len[a] < len[b])
        return false;
    ull hasb = has[b][len[b]];
    for (int i = 1; i + len[b] - 1 <= len[a]; i++)
        if (get(a,i,len[b] - 1) == hasb)
            return true;
    return false;
}

int connect(int a,int b) //a在左,b在右
{
    int minn = min(len[a],len[b]);
    for (int i = minn; i >= 1; i--)
    {
        if (get(a,len[a] - i + 1,i - 1) == get(b,1,i - 1))
            return i;
    }
    return 0;
}

int solve()
{
    for (int i = 1; i <= 3; i++)
        for (int j = i + 1; j <= 3; j++)
            if (contain(i,j) || contain(j,i))
            {
                int x,y;
                y = 6 - i - j;
                if (len[i] > len[j])
                    x = i;
                else
                    x = j;
                if (contain(x,y) || contain(y,x))
                    return max(len[x],len[y]);
                else
                    return len[x] + len[y] - max(connect(x,y),connect(y,x));
            }
    int res = 0x7fffffff;
    do
    {
        res = min(res,sum - connect(id[1],id[2]) - connect(id[2],id[3]));
    }while (next_permutation(id + 1,id + 4));
    return res;
}

int main()
{
    bpow[0] = 1;
    for (int i = 1; i <= 100000; i++)
        bpow[i] = bpow[i - 1] * mod;
    id[1] = 1;
    id[2] = 2;
    id[3] = 3;
    for (int i = 1; i <= 3; i++)
    {
        scanf("%s",s[i] + 1);
        len[i] = strlen(s[i] + 1);
        sum += len[i];
    }
    init();
    printf("%d\n",solve());

    return 0;
}

 

posted @ 2017-12-31 23:47  zbtrs  阅读(279)  评论(0编辑  收藏  举报