【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串
我们二分/分块枚举答案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]公共串的更多相关文章
- 【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串
二分答案,然后搞出hash值扔到哈希表里.期望复杂度O(n*log(n)). <法一>next数组版哈希表 #include<cstdio> #include<cstri ...
- [bzoj2946][Poi2000]公共串_后缀数组_二分
公共串 bzoj-2946 Poi-2000 题目大意:给定$n$个字符串,求他们的最长公共子串. 注释:$1\le n\le 5$,$1\le minlen<maxlen\le 2000$. ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【BZOJ2946】[Poi2000]公共串 后缀数组+二分
[BZOJ2946][Poi2000]公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单词 l 计 ...
- [POI2000] 公共串 - 后缀数组,二分
[POI2000] 公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. Solution 预处理出后缀数组和高度数组,二分答案 \(k\) ,对于每一个连续的 ...
- poj 2774 字符串哈希求最长公共子串
Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)
求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...
- 【二分答案】【字符串哈希】bzoj2084 [Poi2010]Antisymmetry
显然只有偶数长度的串符合题意,并且如果一个串符合题意,那么从其首尾各截掉一个字符也符合题意. 于是枚举中心,二分可以向左右扩展的最远距离,累计答案. #include<cstdio> #i ...
- 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
随机推荐
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- HDU4725:The Shortest Path in Nya Graph(最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Codeforces Round #524 (Div. 2) D. Olya and magical square
D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...
- typescript的入门
命令行使用tsc 1.安装typescript npm install -g typescript 2.新建一个index.ts 输入export hello class{} 3.编译 tsc ind ...
- codeforces 1015A
A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 记录一次Nginx跳转报错的问题
错误信息如下: An error occurred. Sorry, the page you are looking for is currently unavailable. Please try ...
- 从一段字符串中去除数字的shell方法
- 【Foreign】猜测 [费用流]
猜测 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 1 1 1 2 2 1 Samp ...
- 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...
- Linux内核官方文档atomic_ops.txt【摘自Linux 内核文档】
摘自Linux内核文档 Documentation/atomic_ops.txt,不是本人原创 Semantics and Behavior of Atomic and Bitmask Operati ...