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. PLSQL事务

    1 使用set transaction设置事务属性 2 只读事务 set transaction read only 3 读写事务 set transaction write; 4 在进行数据统计分析 ...

  2. Loadrunner教程--常用操做流程

    1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...

  3. 记录一下自己申请并使用VPS的全过程

    在学习REST API的时候,想要阅读一下谷歌爸爸的api design guide,无奈无情被墙,正好在学习云相关的技术,就想到申请一个VPS来用用. 这次我选择的是hostmybytes,原因有两 ...

  4. 微软职位内部推荐-Senior Software Lead-Index Gen

    微软近期Open的职位: Position: Senior Software Development Lead Bing Index Generation team is hiring! As one ...

  5. Qt 编程指南

    Qt 编程指南 持续关注一本正在编写的Qt编程指南,期待作者早日完成创作.

  6. c# 简单日志记录

    FileStream fs = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "log.txt",Fi ...

  7. P4tutorial实战

    Tutorial样例实战 GitHub仓库地址 参考博客 实验一:SIGCOMM_2015/Sourse_Routing 实验环境: OS:Ubuntu16.04 bmv2:behavioral-mo ...

  8. MOOK学习

    课程选择及其理由 课程:c++程序设计 教师:魏英 学校:西北工业大学 总共:48讲 选择理由:我其实之前找了好几个,但由于小白,思考了下(迷茫,感觉好像都不错),然后看了一下大家都选择了西北工业大学 ...

  9. 软工实践-Alpha 冲刺 (9/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成非功能的主界面制作 ...

  10. 第四周作业——C语言自评

    1.你对自己的未来有什么规划?做了哪些准备?以目前的现状来说,希望至少能够掌握专业所要求的基本操作,然后一步步去深入.提升,毕业之后不会灰溜溜的一次次求职失败.目前更多的是利用闲暇时间补回过去老师同学 ...