[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 ...
随机推荐
- Python3.6+pyinstaller+Django
方案(一)Python3.6+pyinstaller+windows服务 一.Python3.6(64位)环境清单 Django==1.11.7 django-windows-tools==0.2 P ...
- 裸机——DDR
1.DDR介绍 DDR,是SDRAM的改进,是双通道的SDRAM, SDRAM是同步动态随机访问存储器. SDRAM与SRAM相对于,二者的特点是: SDRAM 需要初始化,使用时许访问,价格便宜. ...
- TouTiao开源项目 分析笔记6
1.NewsChannelBean简单类笔记 1.1.Comparable接口的实现和使用 参考文章:Comparable接口的实现和使用. 因为NewsChannelBean实现了Comparabl ...
- Javascript Step by Step - 04
前言 本篇主要讨论jQuery的常用的若干操作.为了能直观的显示操作的结果,首先建立一个html文件,内容如下: <!DOCTYPE html> <html> <head ...
- 2 Mongodb基本操作
1.基本操作 MongoDB将数据存储为一个文档,数据结构由键值(key=>value)对组成 MongoDB文档类似于JSON对象,字段值可以包含其他文档.数组.文档数组 安装管理mongod ...
- HDU 5293 Tree chain problem 树形DP
题意: 给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值. 从中选出若干条链,两两不相交,并且使得权值之和最大. 分析: 题解 #include <cstdio> #incl ...
- app图标
1.http://www.iconfont.cn/ 在里面可以搜索你想要的图标: 比如关闭. 2.选择一个好看的下载png 3.打开ps,ctrl n新建一个图层. 把你下载的png弄到上面,然后关闭 ...
- b树的实现
花了蛮长时间实现的b树插入操作.有时间再实现其他操作. #include <stdio.h> #include <stdlib.h> #define M 5 enum KeyS ...
- 自动化测试(三)如何用python写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱,产生的邮箱不能重复。
写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱,产生的邮箱不能重复.邮箱前面的长度是6-12之间,产生的邮箱必须包含大写字母.小写字母.数字和特殊字符 和上一期一样 代码中间有段比较混沌 有 ...
- appium 多个设备同时执行
测试需要同时在多个android设备上运行,就需要启动多个appium 使用adb命令获取udid,命令:adb get-serialno 使用的是testng测试框架,代码使用java编写 第一台, ...