[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 ...
随机推荐
- ES6初识- Class
{ //基本定义和生成实例 class Parent{ //构造函数 constructor(name='lisi'){ this.name=name; } //属性get,set get longN ...
- Smartforms 设置纸张打印格式
在sap做一个打印报表,要先设置一个纸张打印格式,下面以工厂中常用来打印的针孔纸为例,在sap设置该纸张的打印格式,以用于报表: 1.运行事务代码SPAD:选择工具栏上的[完全管理]按钮——>选 ...
- Integer和int使用==比较的总结
public static void main(String[] args) { int i1 = 128; Integer i2 = 128; Integer i3 = new Integer(12 ...
- .NET 客户IP地址捕捉
MVC模式下要获取客户IP可以在ActionFilterAttribute中进行拦截 filterContext.HttpContext.Request.UserHostAddress 同样,在Web ...
- 彻底搞定C指针--“函数名与函数指针”
函数名与函数指针 一 通常的函数调用 一个通常的函数调用的例子: //自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); 点击打开链 ...
- JDK学习---深入理解java中的HashMap、HashSet底层实现
本文参考资料: 1.<大话数据结构> 2.http://www.cnblogs.com/dassmeta/p/5338955.html 3.http://www.cnblogs.com/d ...
- C细节错误
2018.11.20 (1)有关优先级的细节 while (c = getchar() && c == '\n') { printf("c = %d\n", c); ...
- Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route
题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...
- [Bzoj3894]文理分科(最小割)
Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格子代表一个同学的座位.每位 ...
- [回文树][BZOJ2160][国家集训队]拉拉队排练
题面 Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮 ...