[codeforces] 25E Test || hash
原题
给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度。
先暴力判断是否有包含,消减需要匹配的串的数量。因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希,然后判断后缀与前缀重合长度即可。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mo 99994711
typedef long long ll;
using namespace std;
int is1=1,is2=1,all=3,base=1314,ord[4],ans,l[4];
long long num[4][100010],bs[100010];
bool vis[4];
char s[4][100010];
ll gethash(int x,int st,int l)
{
ll ans=0;
ans=bs[100005-st]*(num[x][st+l-1]-(st==0? 0 : num[x][st-1]))%mo;
return (ans%mo+mo)%mo;
}
bool in(int x,int y)
{
ll u,v;
for (int i=0;i<l[y]-l[x];i++)
{
u=gethash(y,i,l[x]);
v=num[x][l[x]-1]*bs[100005]%mo;
if (u==v) return 1;
}
return 0;
}
int mini(int x,int y)
{
ll u,v;
int ans=0;
for (int i=1;i<=min(l[x],l[y]);i++)
{
u=gethash(x,l[x]-i,i);
v=gethash(y,0,i);
if (u==v) ans=i;
}
return ans;
}
void hsh(int x)
{
for (int i=0;i<l[x];i++)
if (i) num[x][i]=(num[x][i-1]+(ll)(s[x][i]-'a'+1)*bs[i])%mo;
else num[x][i]=s[x][i]-'a'+1;
}
void dfs(int x)
{
if (x>3)
{
ans=min(ans,l[1]+l[2]+l[3]-mini(ord[1],ord[2])-mini(ord[2],ord[3]));
return ;
}
for (int i=1;i<=3;i++)
if (!vis[i])
{
vis[i]=1;
ord[x]=i;
dfs(x+1);
vis[i]=0;
}
}
void st()
{
if (l[1]>l[2]) swap(l[1],l[2]),swap(s[1],s[2]);
if (l[2]>l[3]) swap(l[2],l[3]),swap(s[2],s[3]);
if (l[1]>l[2]) swap(l[1],l[2]),swap(s[1],s[2]);
}
int main()
{
ans=0x7fffffff;
bs[0]=1;
for (int i=1;i<=100005;i++) bs[i]=bs[i-1]*base%mo;
scanf("%s%s%s",s[1],s[2],s[3]);
l[1]=strlen(s[1]);
l[2]=strlen(s[2]);
l[3]=strlen(s[3]);
st();
hsh(1);
hsh(2);
hsh(3);
if (in(1,2)) is1=0,all--;
if (in(2,3)) is2=0,all--;
if (is1 && in(1,3)) is1=0,all--;
if (all==1)
{
printf("%d\n",l[3]);
return 0;
}
if (all==2)
{
if (!is2 && is1) swap(s[1],s[2]),swap(l[1],l[2]);
hsh(2);
hsh(3);
printf("%d\n",l[2]+l[3]-max(mini(2,3),mini(3,2)));
}
else
{
dfs(1);
printf("%d\n",ans);
}
return 0;
}
[codeforces] 25E Test || hash的更多相关文章
- Codeforces 25E Test 【Hash】
Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...
- Codeforces 1090J $kmp+hash+$二分
题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...
- Palindrome Degree(CodeForces 7D)—— hash求回文
学了kmp之后又学了hash来搞字符串.这东西很巧妙,且听娓娓道来. 这题的题意是:一个字符串如果是回文的,那么k值加1,如果前一半的串也是回文,k值再加1,以此类推,算出其k值.打个比方abaaba ...
- CodeForces 25E Test KMP
Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...
- CODEFORCES 25E Test
题意 三个字符串,找一个字符串(它的子串含有以上三个字符串)使它的长度最短,输出此字符串的长度. 题解 先枚举字符串排列,直接KMP两两匹配,拼接即可...答案取最小值.. 常数巨大的丑陋代码 # i ...
- Codeforces Round #109 (Div. 2) E. Double Profiles hash
题目链接: http://codeforces.com/problemset/problem/155/E E. Double Profiles time limit per test 3 second ...
- Codeforces Beta Round #4 (Div. 2 Only) C. Registration system hash
C. Registration system Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
随机推荐
- SpringMVC系列一
首先介绍一下SpringMVC的执行流程:如图 1.用户在客户端发送请求,经过前端控制器DispatcherServlet,请求处理器处理,返回ModelAndView返回结果到前端控制器: 2.前端 ...
- Thymeleaf显示Map集合数据
<select class="form-control zz-set-input-size" id="channel"> <option va ...
- MySQL 如何生成月份表
MySQL 如何生成月份表 如果遇到按照月份统计信息的时候,常用的统计方式就是用month表去连接order表,下面就是生成月份表的过程 1.首先是建表 CREATE TABLE `sn_month` ...
- 裸机——Nand
1.首先需要知道Nand的基础知识 从Nand的芯片手册可以获得 我使用的芯片手册是 K9F2G08 首先从芯片手册的名称可以获得信息: K9F:三星 2G : 2Gb (256MB) 08 ...
- POJ:3104-Drying(神奇的二分)
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20586 Accepted: 5186 Description I ...
- pyhton,数据类型
标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) Tuple(元组) List(列表) Sets(集合) Dictionary(字典) 重点是: 类 ...
- spark练习--统计xxx大学的各个少数名族的情况
最近,有一份数据,是关于学校的数据,这个里面有所有学生的信息,今天闲来没事,我就想用spark的方式来读取文件,并且来统计这个学校的各个民族的情况,以前我用hadoop中mapReduce来计算,不得 ...
- GPIO基础知识
STM32 GPIO入门知识 GPIO是什么? 通用输入输出端口,可以做输入,也可以做输出.GPIO端口可通过程序配置成输入或输出. 引脚和GPIO的区别和联系 STM32的引脚中,有部分是做GPIO ...
- 10,python开发之virtualenv与virtualenvwrapper
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程 ...
- Redis数据更新
技术交流群: 233513714