我们二分/分块枚举答案x,暴力把除了最短的字符串以外的其他字符串的x长度子串哈希搞出来,分别扔到set里。

然后暴力枚举最短的字符串的x长度字串,查看是否在全部的set里出现过。

#include<cstdio>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;
typedef unsigned long long ull;
set<ull>T[6];
const ull seed=29;
ull seeds[2001],ord[301];
int n,ls[6],minv=2147483647,mini,ans;
char s[6][2001];
ull BKDRhash(const char str[],const int &L,const int &R)
{
ull res=0;
for(int i=L;i<R;++i)
res=res*seed+ord[str[i]];
return res;
}
void init()
{
for(int i='a',j=1;i<='z';++i,++j)
ord[i]=(ull)j;
seeds[0]=1;
for(int i=1;i<=minv;++i)
seeds[i]=seeds[i-1]*seed;
}
bool Find_In_All(const ull &x)
{
for(int i=1;i<=n;++i)
if(i!=mini)
if(T[i].find(x)==T[i].end())
return 0;
return 1;
}
bool check(const int &x)
{
for(int i=1;i<=n;++i) if(i!=mini)
{
T[i].clear();
ull hs=BKDRhash(s[i],0,x);
T[i].insert(hs);
for(int j=x;j<ls[i];++j)
{
hs=hs*seed+ord[s[i][j]];
hs-=seeds[x]*ord[s[i][j-x]];
T[i].insert(hs);
}
}
ull hs=BKDRhash(s[mini],0,x);
if(Find_In_All(hs)) return 1;
for(int i=x;i<minv;++i)
{
hs=hs*seed+ord[s[mini][i]];
hs-=seeds[x]*ord[s[mini][i-x]];
if(Find_In_All(hs)) return 1;
}
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",s[i]);
ls[i]=strlen(s[i]);
if(ls[i]<minv)
{
minv=ls[i];
mini=i;
}
}
init();
int sz=sqrt(minv),last=minv+1;
for(int i=minv;last;i-=sz)
{
if(check(i))
for(int j=last-1;j>=i;--j)
if(check(j))
{
printf("%d\n",j);
return 0;
}
last=i;
}
}

【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串的更多相关文章

  1. 【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串

    二分答案,然后搞出hash值扔到哈希表里.期望复杂度O(n*log(n)). <法一>next数组版哈希表 #include<cstdio> #include<cstri ...

  2. [bzoj2946][Poi2000]公共串_后缀数组_二分

    公共串 bzoj-2946 Poi-2000 题目大意:给定$n$个字符串,求他们的最长公共子串. 注释:$1\le n\le 5$,$1\le minlen<maxlen\le 2000$. ...

  3. BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案

    BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description          给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单 ...

  4. 【BZOJ2946】[Poi2000]公共串 后缀数组+二分

    [BZOJ2946][Poi2000]公共串 Description        给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单词 l        计 ...

  5. [POI2000] 公共串 - 后缀数组,二分

    [POI2000] 公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. Solution 预处理出后缀数组和高度数组,二分答案 \(k\) ,对于每一个连续的 ...

  6. poj 2774 字符串哈希求最长公共子串

    Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  7. BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)

    求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...

  8. 【二分答案】【字符串哈希】bzoj2084 [Poi2010]Antisymmetry

    显然只有偶数长度的串符合题意,并且如果一个串符合题意,那么从其首尾各截掉一个字符也符合题意. 于是枚举中心,二分可以向左右扩展的最远距离,累计答案. #include<cstdio> #i ...

  9. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

随机推荐

  1. HDU 5670

    Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. leetcode-501. Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  3. jzoj2700 【GDKOI2012模拟02.01】数字

    传送门:https://jzoj.net/senior/#main/show/2700 [题目大意] 令n为正整数,S(n)为n的各位数字之和,令

  4. 【STSRM12】夏令营

    [题意]n个数划分成k段,每段的价值为段内不同数字的数量,求最大总价值 [算法]DP+线段树 [题解] f[i][j]表示前i个数字划分成j段的最大价值. f[i][j]=max(f[k][j-1]+ ...

  5. python 读 excel 模块: xlrd

    主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...

  6. mysql 表的类型

    MySQL 数据表主要支持六种类型 ,分别是:BDB.HEAP.ISAM.MERGE.MYISAM.InnoBDB. 这六种又分为两类,一类是”事务安全型”(transaction-safe),包括B ...

  7. linux下su和su - 的区别

    linux使用中常会使用su来切换用户 使用su切换为tom用户 [root@bogon ~]# su tom[tom@bogon root]$ [tom@bogon root]$ pwd/root ...

  8. Swift, Playgrounds, and XCPlayground

    http://www.codeschool.com/blog/2014/12/12/swift-playgrounds-xcplayground/ Swift, Playgrounds, and XC ...

  9. postman发送post请求,报405 Method Not Allowed

    postman发送post请求,报405 Method Not Allowed: 亲测有效的方案一: 检查请求header是否缺少必要信息.如果不清可以把所有的头部信息全部粘贴到header中,尝试是 ...

  10. 安装VMware Tools的步骤和那些坑

    背景环境:VMware workstation 12.5+Ubuntu16.04 首先VMware Tools在ubuntu中是及其不稳定的,也就是说,当你点击菜单栏中的install vmware ...