基因切割
题目大意
给你一个字符串,然后有 m 个处理器从左到右,然后你要把字符串从左到右放入每个处理器,每个处理器有一个串,会储存这个串的长度的字符,然后当储存的字符就是它的串的时候就会清除这个字符。 然后问你放进去之后这个字符串变成了什么。
思路
无语的一道题,写了半天,跳了一天半。。。
首先你会发现它其实就是依次对每个串做。 然后做一次就是可以用 KMP 找,然后删掉。 那这样是
n
m
nm
nm 的。
然后你发现有个部分分是每个处理器的串都很小,那你其实可以想到我们用哈希表存下每个这些串,然后我们找原串中长度不超过这些串的子串,然后暴力删。 那这样是
n
A
2
nA^2
nA2(
A
A
A 就是串的长度)
然后你考虑把它们两个结合起来: 考虑大小小于等于
A
A
A 的用哈希做,大于
A
A
A 的用 KMP 做。 那哈希的复杂度就是
n
A
2
nA^2
nA2,那大于
A
A
A 的至多
m
A
\dfrac{m}{A}
Am? 个串,所以是
n
m
A
\dfrac{nm}{A}
Anm?。
然后平衡规划一下:
n
A
2
+
n
m
A
?
n
m
2
3
nA^2+\dfrac{nm}{A}\geqslant nm^{\frac{2}{3}}
nA2+Anm??nm32? 当且仅当
A
=
m
1
3
A=m^{\frac{1}{3}}
A=m31?
看起来很简单吧,但你要怎么维护两个呢?
然后我们可以用链表维护。 然后难度乘了几倍。
然后你会发现好像哈希会被卡?双哈希。 然后更麻烦了!
对就是这样。
代码
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define ull unsigned long long
#define di1 13331
#define di2 19491001
#define mo1 998244853
#define mo2 999324367
#define mo3 16777213
using namespace std;
const int N = 1e5 + 100;
const int AA = 100 + 1;
int n, A, m, msum, mm[N];
int fail[N], tot;
char s[N], ss[N];
string a[N];
vector <int> vec[N], tmp;
ull jia1(ull x, ull y) {
return x + y >= mo1 ? x + y - mo1 : x + y;
}
ull jian1(ull x, ull y) {
return x + mo1 - y >= mo1 ? x - y : x + mo1 - y;
}
ull mul1(ull x, ull y) {
return x * y % mo1;
}
ull jia2(ull x, ull y) {
return x + y >= mo2 ? x + y - mo2 : x + y;
}
ull jian2(ull x, ull y) {
return x + mo1 - y >= mo2 ? x - y : x + mo2 - y;
}
ull mul2(ull x, ull y) {
return x * y % mo2;
}
struct Hash {
ull val1, val2;
Hash(int val1_ = 0, int val2_ = 0) {val1 = val1_; val2 = val2_;}
ull val() {
return val1 << 32 | val2;
}
}di, dis[N], hs[N];
Hash operator +(Hash x, Hash y) {
return Hash(jia1(x.val1, y.val1), jia2(x.val2, y.val2));
}
Hash operator +(Hash x, int y) {
return Hash(jia1(x.val1, y), jia2(x.val2, y));
}
Hash operator -(Hash x, Hash y) {
return Hash(jian1(x.val1, y.val1), jian2(x.val2, y.val2));
}
Hash operator -(Hash x, int y) {
return Hash(jian1(x.val1, y), jian2(x.val2, y));
}
Hash operator *(Hash x, Hash y) {
return Hash(mul1(x.val1, y.val1), mul2(x.val2, y.val2));
}
Hash operator *(Hash x, int y) {
return Hash(mul1(x.val1, y), mul2(x.val2, y));
}
struct HASH_TABLE{
struct node {
ull x;
int to, nxt;
}e[N];
int le[mo3], KK;
void Insert(ull x, int y){
int now = x % mo3;
e[++KK] = (node){x, y, le[now]}; le[now] = KK;
}
int Getval(ull x){
int now = x % mo3;
for (int i = le[now]; i; i = e[i].nxt)
if(e[i].x == x) return e[i].to;
return -1;
}
}mp;
struct node {
int pre, nxt, va;
bool out;
Hash val[AA];
}P[N];
void Insert(int i) {
for (int j = 1; j <= A; j++) {
P[i].val[j] = dis[j - 1] * P[i].va + P[P[i].nxt].val[j - 1];
int id = mp.Getval(P[i].val[j].val());
if (id != -1) vec[id].push_back(i);
}
}
void delete_(int x) {
if (P[x].out) return ;
int pre = P[x].pre, nxt = P[x].nxt;
P[x].out = 1;
P[pre].nxt = nxt; P[nxt].pre = pre;
}
int Nxt_delete(int x, int len) {
while (len--) {
int nxt = P[x].nxt;
delete_(x); x = nxt;
}
return x;
}
int Pre_delete(int x, int len) {
while (len--) {
int pre = P[x].pre;
delete_(x); x = pre;
}
return x;
}
void work_HASH(int x, int len) {
int id = mp.Getval(hs[x].val());
vector <int> Tmp = vec[id];
sort(Tmp.begin(), Tmp.end());
vec[id] = vector <int> ();
int R = 1; tmp.clear();
for (int i = 0; i < Tmp.size(); i++) {
int nw = Tmp[i];
if (nw < R || P[nw].out) continue;
if (P[nw].val[len].val() == hs[x].val()) {
tmp.push_back(P[nw].pre);
R = Nxt_delete(nw, len);
}
}
R = 1e9;
for (int i = tmp.size() - 1; i >= 0; i--) {
for (int j = 0; j < A; j++) {
if (!tmp[i]) break;
if (tmp[i] < R) Insert(tmp[i]), R = tmp[i];
tmp[i] = P[tmp[i]].pre;
}
}
}
void work_KMP(int x, int len) {
tmp.clear();
fail[0] = -1; int j = 0;
for (int i = 2; i <= len; i++) {
while (j != -1 && a[x][i - 1] != a[x][j]) j = fail[j];
j++; fail[i] = j;
}
j = 0;
for (int i = P[0].nxt; i != n + 1; i = P[i].nxt) {
while (j != -1 && P[i].va != a[x][j]) j = fail[j];
j++;
if (j == len) {
j = 0; tmp.push_back(Pre_delete(i, len));
}
}
for (int i = tmp.size() - 1; i >= 0; i--) {
for (int j = 0; j < A; j++) {
if (!tmp[i]) break;
Insert(tmp[i]);
tmp[i] = P[tmp[i]].pre;
}
}
}
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%s", ss + 1); a[i] = string(ss + 1);
mm[i] = a[i].length(); msum += mm[i];
}
A = pow(1.0 * msum, 1.0 / 3);
di = Hash(di1, di2);
dis[0] = Hash(1, 1); for (int i = 1; i <= n; i++) dis[i] = dis[i - 1] * di;
for (int i = 1; i <= m; i++) {
if (mm[i] <= A) {
hs[i] = Hash(0, 0); for (int j = 0; j < mm[i]; j++) hs[i] = hs[i] * di + a[i][j];
if (mp.Getval(hs[i].val()) == -1) mp.Insert(hs[i].val(), ++tot);
}
}
for (int i = 0; i <= n + 1; i++) {
P[i].pre = i - 1; P[i].nxt = i + 1; P[i].va = s[i];
}
for (int i = n; i >= 1; i--) Insert(i);
for (int i = 1; i <= m; i++) {
if (mm[i] <= A) work_HASH(i, mm[i]);
else work_KMP(i, mm[i]);
}
for (int i = P[0].nxt; i != n + 1; i = P[i].nxt) {
putchar(P[i].va);
}
return 0;
}
|