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 ...
随机推荐
- DeepLearning - Overview of Sequence model
I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...
- 支持向量机SVM 初识
虽然已经学习了神经网络和深度学习并在几个项目之中加以运用了,但在斯坦福公开课上听吴恩达老师说他(在当时)更喜欢使用SVM,而很少使用神经网络来解决问题,因此来学习一下SVM的种种. 先解释一些概念吧: ...
- CDQ分治_占坑
准备系统地学习一波CDQ分治,持续更新中... 首先,CDQ分治也还是分治的一种,只不过普通分治是独立的解决两个子问题,而CDQ分治还要计算第一个子问题对于第二个的影响. CDQ分治几乎都是用来解决多 ...
- Sublime Text 3高效实用快捷键
2017-11-27 16:18:48 Sublime Text 3 高效实用快捷键 Sublime Text 3 软件及注册码 官网下载链接在这里,有时候会很神奇的上不去,可能是因为被Q了,可能就是 ...
- Scrum立会报告+燃尽图(Beta阶段第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2387 项目地址:https://coding.net/u/wuyy694 ...
- Beta冲刺第二周王者荣耀交流协会第四次会议
1.例会照片: 成员:王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王玉玲 2.时间跨度: 2017年11月20日 18:00 — 18:13,总计13分钟. 3.地点: 一 ...
- 阿帕奇web服务器下载部署安装运行
链接: https://jingyan.baidu.com/album/d8072ac47baf0eec95cefdca.html?picindex=4 1.apache服务安装成功可是启动失败“wi ...
- 【线段树维护复杂状态】Ryuji doesn't want to study
https://nanti.jisuanke.com/t/31460 tree[rt].ans = tree[rt << 1].ans + tree[rt << 1 | 1]. ...
- RAR和ZIP:压缩大战真相 (挺赞值得了解)
前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万众期待下发布了9.0正式版.全世界自然一片沸腾,在世界各大知名下载网站中,WinZip9.0再次带起下载狂潮.然而此时国内并没有王者回 ...
- HDU 5875 Function 优先队列+离线
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...