【二分答案】【哈希表】【字符串哈希】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 ...
随机推荐
- [bzoj 1208]STL水过
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 看网上的题解都用的手写数据结构……然而直接用set的lower_bound就水过去了 ...
- Saruman’s Level Up~(多校赛算组合数)
Description Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out o ...
- windows下maven打包eclipse工程
打包过程中,可能出现的2个问题: ①.[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build ...
- Spring securiuty 过滤器
1. HttpSessionContextIntegrationFilter 位于过滤器顶端,第一个起作用的过滤器. 用途一,在执行其他过滤器之前,率先判断用户的session中是否已经存在一个Sec ...
- [POJ1286&POJ2154&POJ2409]Polya定理
Polya定理 L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk)) G为置换群大小 m为颜色数量 c(pi)表示第i个置换的循环节数 如置换(123)(45)(6)其循环节数为 ...
- [POJ1082&POJ2348&POJ1067&POJ2505&POJ1960]简单博弈题总结
鉴于时间紧张...虽然知道博弈是个大课题但是花一个上午时间已经极限了... 希望省选过后再回过头来好好总结一遍吧. 接下来为了看着顺眼一点...还是按照难度顺序吧 POJ1082 一道最简单的博弈 ...
- Restful接口设计
URL设计规范:/模块/资源/{标示}/集合1/... eg: /user/{uid}/friends ->好友列表 例子:秒杀系统API设计 1.请求参数绑定:@PathVariable(&q ...
- redis.clients.jedis.HostAndPort - cant resolve localhost address
阿里云ECS部署spring-boot访问redis出现redis.clients.jedis.HostAndPort - cant resolve localhost address 摘要: 阿里云 ...
- Idea设置全白色 背景
IDEA设置全白色背景 标签(空格分隔): 工具使用 编辑框白色设置 菜单栏白色设置
- pandas求五日线并画图
import pandas as pd import numpy as np import matplotlib.pyplot as plt stock_data = pd.read_csv('000 ...