【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串
二分答案,然后搞出hash值扔到哈希表里。期望复杂度O(n*log(n))。
<法一>next数组版哈希表
#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
const ull seed=29;
#define MOD 2007
ull seeds[2001],ord[301],v[6][2000];
int n,ls[6],minv=2147483647,mini,first[6][MOD],next[6][2000],en[6];
char s[6][2001];
void AddEdge(const int &o,const int &U,const ull &V)
{
v[o][en[o]]=V;
next[o][en[o]]=first[o][U];
first[o][U]=en[o]++;
}
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(const int &o,const ull &x)
{
int y=(int)(x%MOD);
for(int i=first[o][y];i!=-1;i=next[o][i]) if(v[o][i]==x) return 1;
return 0;
}
bool Find_In_All(const ull &x)
{
for(int i=1;i<=n;++i)
if(i!=mini&&(!Find(i,x)))
return 0;
return 1;
}
bool check(const int &x)
{
memset(first,-1,sizeof(first));
memset(en,0,sizeof(en));
for(int i=1;i<=n;++i) if(i!=mini)
{
ull hs=BKDRhash(s[i],0,x);
AddEdge(i,hs%MOD,hs);
for(int j=x;j<ls[i];++j)
{
hs=hs*seed+ord[s[i][j]];
hs-=seeds[x]*ord[s[i][j-x]];
AddEdge(i,hs%MOD,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 l=0,r=minv+1;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) l=mid+1;
else r=mid;
}
printf("%d\n",l-1);
return 0;
}
<法二>vector版哈希表
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef unsigned long long ull;
const ull seed=29;
#define MOD 2007
typedef vector<ull>::iterator VER;
vector<ull>List[6][2007];
ull seeds[2001],ord[301];
int n,ls[6],minv=2147483647,mini;
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(const int &o,const ull &x)
{
int y=(int)(x%MOD);
for(VER it=List[o][y].begin();it!=List[o][y].end();++it)
if(*it==x)
return 1;
return 0;
}
bool Find_In_All(const ull &x)
{
for(int i=1;i<=n;++i)
if(i!=mini&&(!Find(i,x)))
return 0;
return 1;
}
bool check(const int &x)
{
for(int i=1;i<=n;++i) if(i!=mini)
{
ull hs=BKDRhash(s[i],0,x);
List[i][hs%MOD].push_back(hs);
for(int j=x;j<ls[i];++j)
{
hs=hs*seed+ord[s[i][j]];
hs-=seeds[x]*ord[s[i][j-x]];
List[i][hs%MOD].push_back(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 l=0,r=minv+1;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) l=mid+1;
else r=mid;
}
printf("%d\n",l-1);
return 0;
}
【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串的更多相关文章
- 【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串
我们二分/分块枚举答案x,暴力把除了最短的字符串以外的其他字符串的x长度子串哈希搞出来,分别扔到set里. 然后暴力枚举最短的字符串的x长度字串,查看是否在全部的set里出现过. #include&l ...
- [bzoj2946][Poi2000]公共串_后缀数组_二分
公共串 bzoj-2946 Poi-2000 题目大意:给定$n$个字符串,求他们的最长公共子串. 注释:$1\le n\le 5$,$1\le minlen<maxlen\le 2000$. ...
- 哈希,哈希表,哈希Map
数组: 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1):数组的特点是:寻址容易,插入和删除困难: 链表: 链表存储区间离散,占用内存比较宽松,故空间复杂 ...
- Java集合(八)哈希表及哈希函数的实现方式
Java集合(八)哈希表及哈希函数的实现方式 一.哈希表 非哈希表的特点:关键字在表中的位置和它之间不存在一个确定的关系,查找的过程为给定值一次和各个关键字进行比较,查找的效率取决于和给定值进行比较的 ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【BZOJ2946】[Poi2000]公共串 后缀数组+二分
[BZOJ2946][Poi2000]公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单词 l 计 ...
- [POI2000] 公共串 - 后缀数组,二分
[POI2000] 公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. Solution 预处理出后缀数组和高度数组,二分答案 \(k\) ,对于每一个连续的 ...
- 剑指 Offer 50. 第一个只出现一次的字符 + 哈希表 + 有序哈希表
剑指 Offer 50. 第一个只出现一次的字符 Offer_50 题目详情 方法一:使用无序哈希表 package com.walegarrett.offer; /** * @Author Wale ...
- POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串
Life Forms Time Limit: 500 ...
随机推荐
- codeforces 111D
题目链接 D. Petya and Coloring time limit per test 5 seconds memory limit per test 256 megabytes input s ...
- SHOI 2007 仙人掌图(BZOJ 1023)
1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2564 Solved: 1062 Descrip ...
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...
- Linux下程序对拍_C++ (付费编号1001)
本博客为精品博客,涉及利益问题,严禁转载,违者追究法律责任 一.对拍背景 对拍是一种十分实用的检查程序正确性的手段,在比赛时广泛使用 我们一般对拍两个程序,一个是自己不确定正确性的高级算法,另一个一般 ...
- python3 线程_threading模块
'''并发:同一个时间段内运行多个程序的能力 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成 程序:食谱数据集:鸡蛋.牛奶.糖等进程控制块:记下食谱做到 ...
- 这个命令可以看到你的cpu到底集合
cat /proc/cpuinfo |grep processor|awk '{print $3}'|wc -l 改变虚拟机分辨率 xrandr -s 1024x768 -r 60 或 ...
- HDU2594(简单KMP)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- apparmor开启
- 【反演复习计划】【bzoj3994】DZY loves maths
这题大概就是提取一下d,然后就跟前面的题目差不多了. #include<bits/stdc++.h> #define N 10000005 using namespace std; typ ...
- 【bzoj4810】由乃的玉米田
lxl丧心病狂-- 首先允许离线的区间询问一看就是莫队.那么我们看下怎么莫队? 不会. "由乃题多半是不可做的."于是我看了下题解--好吧果然是bitset 用bitset维护当前 ...