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+;
char s[][];
int len[],sum,id[];
ull has[][],bpow[]; void init()
{
for (int i = ; i <= ; i++)
for (int j = ; j <= len[i]; j++)
has[i][j] = has[i][j - ] * 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 - ] * bpow[lenn + ];
} bool contain(int a,int b)
{
if (len[a] < len[b])
return false;
ull hasb = has[b][len[b]];
for (int i = ; i + len[b] - <= len[a]; i++)
if (get(a,i,len[b] - ) == 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 >= ; i--)
{
if (get(a,len[a] - i + ,i - ) == get(b,,i - ))
return i;
}
return ;
} int solve()
{
for (int i = ; i <= ; i++)
for (int j = i + ; j <= ; j++)
if (contain(i,j) || contain(j,i))
{
int x,y;
y = - 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[],id[]) - connect(id[],id[]));
}while (next_permutation(id + ,id + ));
return res;
} int main()
{
bpow[] = ;
for (int i = ; i <= ; i++)
bpow[i] = bpow[i - ] * mod;
id[] = ;
id[] = ;
id[] = ;
for (int i = ; i <= ; i++)
{
scanf("%s",s[i] + );
len[i] = strlen(s[i] + );
sum += len[i];
}
init();
printf("%d\n",solve()); return ;
}

Codeforces 25.E Test的更多相关文章

  1. Codeforces Round #486 (Div. 3) E. Divisibility by 25

    Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

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

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  3. codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...

  4. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

  5. Educational Codeforces Round 25 Five-In-a-Row(DFS)

    题目网址:http://codeforces.com/contest/825/problem/B 题目:   Alice and Bob play 5-in-a-row game. They have ...

  6. Divisibility by 25 CodeForces - 988E (技巧的暴力)

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  7. Educational Codeforces Round 25 A,B,C,D

    A:链接:http://codeforces.com/contest/825/problem/A 解题思路: 一开始以为是个进制转换后面发现是我想多了,就是统计有多少个1然后碰到0输出就行,没看清题意 ...

  8. Educational Codeforces Round 25 C. Multi-judge Solving

    题目链接:http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test 1 second ...

  9. Educational Codeforces Round 25 B. Five-In-a-Row

    题目链接:http://codeforces.com/contest/825/problem/B B. Five-In-a-Row time limit per test 1 second memor ...

随机推荐

  1. NO.02---聊聊Vue提升

    如果本篇有看不明白的地方,请翻阅上一篇文章 上一篇我们讲了如何通过一些简单的动作来改变 store.js 中的数据对象,在实际工作中,这是完全无法满足工作需求的,所以这篇我们来说说如何做一些简单的流程 ...

  2. Boss直聘邮件通知小脚本

    Boss 基于Python3的找工作利器--Boss直聘来消息邮件通知, 自动发送简历脚本,O(∩_∩)O~ 无聊写的,因为有时候觉得找工作心急如焚,想自动回复自动发简历啊有木有~~~ github地 ...

  3. (python)剑指Offer 面试题51:数组中重复的数字

    问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...

  4. 关闭会声会影2018提示UEIP.dll找不到指定模块

    最近有一些会声会影2018用户反映在关闭后弹出UEIP.dll错误,不知道该怎么办才好,针对这个问题,小编下面为大家介绍下解决方法. 原因分析 出现这个错误跟会声会影安装路径有中文字符是密切相关的,导 ...

  5. Linux系统网络安装——基于pxe+dhcp+nfs+tftp+kickstart

    原文发表于:2010-09-05 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...

  6. eclipse提示找不到dubbo.xsb报错

    需要下载一个dubbo.xsb文件到本地,并在eclipse中配置 下载路径:下载链接 下载方法: a).带开链接 b).点击[Raw]按钮 c). 右键->另存为 在eclipse中配置xsb ...

  7. Jenkins之Sonar 代码检查

    一.简介 SonarQube 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等 ...

  8. [T-ARA/筷子兄弟][Little Apple]

    歌词来源:http://music.163.com/#/song?id=29753511 作曲 : 筷子兄弟 [作曲 : 筷子兄弟] 作词 : K-Smith [作词 : KSmith] 编曲 : 新 ...

  9. Python 装饰器Decorator(一)

    (一) 装饰器基础知识 什么是Python装饰器?Python里装饰器是一个可调用的对象(函数),其参数是另一个函数(被装饰的函数) 假如有一个名字为somedecorator的装饰器,target是 ...

  10. Python Requests库入门——应用实例-京东商品页面爬取+模拟浏览器爬取信息

    京东商品页面爬取 选择了一款荣耀手机的页面(给华为打广告了,荣耀play真心不错) import requests url = "https://item.jd.com/7479912.ht ...