Codeforces 25E Test 【Hash】

15 篇文章 0 订阅
10 篇文章 0 订阅

Codeforces 25E Test


E. Test

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


hash题,恶心了一个小时
我的奇葩思路:预处理a和b数组,分别是从前到后和从后往前的hash值前缀和
然后检查一下有没有包含关系
做完了细节比较多


#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define LL long long
string s[3];
LL a[3][N],b[3][N],ans=0x3f3f3f3f;
LL base=131,Mod=100000007;
LL bas[N],vis[3]={0};
void solve(LL id){
    LL len=s[id].size();
    for(LL i=0;i<len;i++)a[id][i]=((i>0)*a[id][i-1]+s[id][i]*bas[i])%Mod;
    for(LL i=len-1;i>=0;i--)b[id][i]=(b[id][i+1]+s[id][i]*bas[i])%Mod;
}
bool check_in(LL id1,LL id2){
    LL len1=s[id1].size(),len2=s[id2].size();
    LL h1=a[id1][len1-1],h2=a[id2][len1-1];
    if(h1==h2)return 1;
    for(LL i=len1;i<len2;i++){
        h2=(a[id2][i]-a[id2][i-len1]+Mod)%Mod;
        h1=h1*base%Mod;
        if(h1==h2)return 1;
    }
    return 0;
}
LL getlen(LL id1,LL id2){
    LL ans=0,len1=s[id1].size(),len2=s[id2].size();
    for(LL i=1;i<min(len1,len2);i++)
        if(b[id1][len1-i]==(a[id2][i-1]*bas[len1-i]%Mod))ans=max(ans,i);
    return ans;
}
LL work(LL id1,LL id2,LL id3){
    if(vis[id1]){
        if(vis[id2])return s[id3].size();
        if(vis[id3])return s[id2].size();
        return s[id2].size()+s[id3].size()-getlen(id2,id3);
    }
    if(vis[id2]){
        if(vis[id1])return s[id3].size();
        if(vis[id3])return s[id1].size();
        return s[id1].size()+s[id3].size()-getlen(id1,id3);
    }
    if(vis[id3]){
        if(vis[id1])return s[id2].size();
        if(vis[id2])return s[id1].size();
        return s[id1].size()+s[id2].size()-getlen(id1,id2);
    }
    return s[id1].size()+s[id2].size()+s[id3].size()-getlen(id1,id2)-getlen(id2,id3);
}
int main(){
    cin>>s[0]>>s[1]>>s[2];
    if(s[1].size()<s[0].size())swap(s[0],s[1]);
    if(s[2].size()<s[1].size())swap(s[1],s[2]);
    if(s[1].size()<s[0].size())swap(s[0],s[1]);
    LL maxlen=s[2].size();
    bas[0]=1;
    for(LL i=1;i<=maxlen;i++)bas[i]=(bas[i-1]*base)%Mod;
    for(LL i=0;i<3;i++)solve(i);
    if(check_in(0,1))vis[0]=1;
    if(check_in(0,2))vis[0]=1;
    if(check_in(1,2))vis[1]=1;
    for(LL x=0;x<3;x++)
        for(LL y=0;y<3;y++)
            for(LL z=0;z<3;z++)
                if(x!=y&&y!=z&&x!=z)
                    ans=min(ans,work(x,y,z));
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值