[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 ...
随机推荐
- 7-3 python操作excel
1.写excel 写入特定单元格数据 # .导入xlwt模块 # .新建一个excel # .添加一个sheet页 # .往指定的单元格中写入数据 # .保存excel import xlwt boo ...
- Redis数据库 : python与java操作redis
redis 包 from redis import * 连接: r = StrictRedis(host='localhost', port='6379') 读写:r.set('key','value ...
- git push 时 fatal: Unable to create 'D:/phpStudy/WWW/green_tree/.git/index.lock': File exists.解决办法
找到自己的项目,找到.git文件夹,进去把目标文件删除即可 或者使用rm -rf 命令(如果没有那个文件件或者文件,将隐藏文件打开就可以看到了)
- PLC状态机编程-如何在STL中使用状态机
搞PLC编程多年,一直不知道状态机,学习matlab后,发现状态机编程异常方便,过去很多编程时的疑惑豁然开朗起来.今天跟大家分享一下如何在STL中使用状态机. 下面是用状态机描述的控制任务. 这个状态 ...
- 学习python第十三天,函数5 装饰器decorator
定义:装饰器本质是函数,(装饰其他函数)就是为其他函数添加附加功能原则:1.不能修改被装饰的函数的源代码 2.不能修改装饰的函数的调用方式 实现装饰器知识储备1函数即变量2.高阶函数,满足2个条件之一 ...
- 点击EditText可编辑,点击其他地方不可编辑
我是在Fragment中实现的,在网上查了,有的说要回调Activity的onTouch事件,通过实验直接在Fragment中即可.如下: 我的EditText在ScrollView,因为Scroll ...
- requestLayout 无效
今天,listview 的requestLayout 无效. 最后,我用了 getWindow().getDecorView().requestLayout(); 可以了.
- java前台传参json,后台用map或者实体对象接收
(一)前台传js对象,里面包含数组,后台用map接收 (1)第一种情况:数组里不包含js对象 var param ={}: param.id=id; param.name=name; var scor ...
- hbase shell出现ERROR:Can't get master address from Zookeeper;znode data==null
hbase shell出现ERROR:Can't get master address from Zookeeper;znode data==null(ERROR:org.apache.hadoop. ...
- 机器学习tensorflow框架初试
本文来自网易云社区 作者:汪洋 前言 新手学习可以点击参考Google的教程.开始前,我们先在本地安装好 TensorFlow机器学习框架. 首先我们在本地window下安装好python环境,约定安 ...