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;
}

Codeforces 25E Test 【Hash】的更多相关文章

  1. [CodeForces - 1225C]p-binary 【数论】【二进制】

    [CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...

  2. 【hash】BZOJ3751-[NOIP2014]解方程

    [题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...

  3. 【hash】Power Strings

    [题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...

  4. 【hash】Similarity of Subtrees

    图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...

  5. 【hash】Seek the Name, Seek the Fame

    [哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...

  6. 【hash】A Horrible Poem

    [题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...

  7. 【hash】Three friends

    [来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...

  8. 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】

    [链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...

  9. 【hash】珍珠

    [来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...

随机推荐

  1. 网络流learning

    上次学习网络流还是大一的下学期,之后就被从图论分出来交给队友了 然而吉林一战,队友在深圳读研而不能来,于是需要自己学习一下,争取在比赛前看完网络流建模汇总和一些总结,升华一下. 同时记录一下自己做过的 ...

  2. python 编程测试练习2

    1.将A.txt(多行)文件的内容读取出来写入到B.txt中 2.总结 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚 ...

  3. JavaScript高级与面向对象

    对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...

  4. MongoDB 查看所有用户账号信息

    在 MongoDB 中创建了很多帐号,怎么查看所有帐号信息? 1. 查看全局所有账户 2. 查看当前库下的账户 查看全局所有账户 : > use admin switched to db adm ...

  5. webBrowser.DocumentText重新赋值无效解决方法

    因为webBrowser这个控件的webBrowser.DocumentText是异步的,所以要自己调用刷新: webBrowser.Navigate("about:blank") ...

  6. pyhon SyntaxError: Non-ASCII character '\xe8' in file xxx on line xx, but no encoding

    import math if __name__ == '__main__':    name1 = raw_input("请输入您的编号:")    print name1 完整的 ...

  7. Centos7 使用Dockerfile 制作自己的Dotnetcore程序镜像

    准备Centos7环境及Docker环境 从Docker hub拉取 Microsoft/dotnet 基础镜像(可以使用国内加速) 向Centos7指定目录上传Dotnet Core程序,目录: / ...

  8. Java开发微信公众号模板消息【同步|异步】

    第一步:申请模板消息功能并添加模板 在微信公众平台找到你需要的模板,并添加上即可: 第二步:添加功能模块后开始开发 功能中使用的类及代码: 发送数据主实体类: Template.java packag ...

  9. Redis数据结构:跳跃表

    1. 跳跃表是有序集合(zset)的底层实现之一: 2. 由zskiplist和zskiplistNode组成: 3. 每个跳跃表节点的层数都是1-32之间的随机数(每创建一个节点的时候,程序会随机生 ...

  10. Python中列表生成式和字典生成式练习

    (一)列表生成式 练习一:编写名为collatz(number)的函数:实现的功能:参数为偶数时,打印number// 2;参数为奇数时,打印3*number + 1 解析: number = int ...