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. Spring学习(3):Spring概述(转载)

    1. Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 在面向对象思想中 ...

  2. JAVA学习笔记--迭代器

    迭代器(Iterator)是一种设计模式.它是一个对象,它的工作是遍历并选择序列中的对象,而客户端程序员不必知道或关心该序列底层的结构.创建迭代器的代价小,因而迭代器通常被称为轻量级对象. 一.Ite ...

  3. centos 7.2 安装apache,mysql,php5.6

    安装Apache.PHP.Mysql.连接Mysql数据库的包: yum -y install httpd yum -y install php yum -y install php-fpm yum  ...

  4. nginx upstream 名称下划线问题

    原始配置: user  nobody;worker_processes  1; #pid        logs/nginx.pid; worker_connections  1024;} http ...

  5. 王者荣耀交流协会第一次Scrum立会

    工作照片: scrum master:高远博 时间跨度;2017/10/13 6:04-6:34 地点:一食堂二楼两张桌子旁 立会内容; 昨天的成绩;昨天商议了今天的开会的时间.地点 今天的计划;讨论 ...

  6. “Hello World!”团队第九次会议

    今天是我们团队“Hello World!”团队召开的第九次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 20 ...

  7. YQCB冲刺周第二天

    YQCB冲刺周第二天 1.实现用户记账的功能 2.实现用户头像的设置 3.实现个人设置的功能 遇到的问题: 记账的分类,数据库存取图片,页面跳转+超链接的使用 团队讨论的照片:             ...

  8. net项目调试时,读取主干或其他项目代码问题

    最近调试项目的时候出了一个很奇怪的问题. 项目总是去读取另外一个项目的配置文件,甚至执行的代码都是另外一个项目的. 我用vs修改代码时候,甚至修改到了其他项目的代码,生成不报错,很奇怪. 本来怀疑是v ...

  9. 设计模式PHP篇(二)————工厂模式

    一个很简单的工厂模式.代码如下: <?php interface Person { public function sex(); } class Man implements Person { ...

  10. centos中apache自用常用额外配置记录(xwamp)

    xwamp套件中apache配置,记录下,以免忘记. 配置路径 ${wwwroot_dir}/conf/httpd.conf 配置内容 <ifmodule mod_deflate.c> D ...