Codeforces 25.E Test
2 seconds
256 megabytes
standard input
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?
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 one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
ab
bc
cd
4
abacaba
abaaba
x
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的更多相关文章
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Beta Round #25 (Div. 2 Only)
Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...
- 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 ...
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...
- 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 ...
- 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 ...
- Educational Codeforces Round 25 A,B,C,D
A:链接:http://codeforces.com/contest/825/problem/A 解题思路: 一开始以为是个进制转换后面发现是我想多了,就是统计有多少个1然后碰到0输出就行,没看清题意 ...
- 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 ...
- 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 ...
随机推荐
- 3星|《科技投资新时代》:TMT行业资讯汇编
科技投资新时代:TMT投资方法.趋势与热点聚焦 全书共6章,前4章是一些投资与分析的基本方法与技巧,第5章集中讲通信行业的现状与趋势,第6章讲大数据.物联网.全面屏等TMT行业热点. 总体来说数据.信 ...
- 『ACM C++』PTA浙大 | 基础题 - Have Fun with Numbers
连着这两道都是开学前数构老师的“爱心作业”,还没上课开学就给我们布置作业了,这道题有点小坑,也经常遇到类似的问题,特地拿出来记录一下. -------------------------------- ...
- django项目中关于跨域CORS
1.使用django-cors-headers扩展,但首先进行安装 2.在配置中添加应用 3.在中间层中设置:“corsheaders.middleware.CorsMiddleware” 4.添加C ...
- DevOps之六 shell以及pipeline 命令部署
一 使用shell命启动spring boot 项目 1. 使用shell停止当前项目 #!/bin/sh main() { clear pid=`ps -ef|grep xx.jar|grep -v ...
- Hibernate查询的六种方式
Hibernate查询的六种方式 分别是HQL查询,对象化查询Criteria方法,动态查询DetachedCriteria,例子查询,sql查询,命名查询. 如果单纯的使用hibernate ...
- eclipse安装反编译器jad
1.下载net.sf.jadclipse_3.3.0.jar.jadclipse_3.3.0.jar.jad.exe 2.将net.sf.jadclipse_3.3.0.jar放在eclipse的安装 ...
- springboot 集成 swagger
1. 首先配置swaggerConfigpackage com.lixcx.lismservice.config; import com.lixcx.lismservice.format.Custom ...
- Longge's problem(欧拉函数应用)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- Mac10.11.2 Apache 服务配置
系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...
- PreparedStatement的execute误解
boolean execute() throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句可以是任何种类的 SQL 语句.一些特别处理过的语 ...