CodeForces 873F Forbidden Indices 后缀数组
忘了当时怎么做的了,先把代码贴上,保存一下后缀数组模板。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
typedef long long LL;
const int maxn = 200000 + 10;
int n, sum[maxn];
char s[maxn], t[maxn];
int fi[maxn], se[maxn], c[maxn], sa[maxn];
//比较的过程中是有数组访问越界的可能的,注意检查
bool equal(int *a, int i, int j, int k) {
if(a[i] != a[j]) return false;
if(i + k < n && j + k < n) return a[i + k] == a[j + k];
if(i + k >= n && j + k >= n) return true;
return false;
}
void build_sa(int m = 27) {
int *x = fi, *y = se;
REP(i, 0, m) c[i] = 0;
REP(i, 0, n) c[x[i] = s[i]]++;
REP(i, 1, m) c[i] += c[i - 1];
PER(i, 0, n) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1) {
int p = 0;
REP(i, n - k, n) y[p++] = i;
REP(i, 0, n) if(sa[i] >= k) y[p++] = sa[i] - k;
REP(i, 0, m) c[i] = 0;
REP(i, 0, n) c[x[y[i]]]++;
REP(i, 1, m) c[i] += c[i - 1];
PER(i, 0, n) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
REP(i, 1, n)
x[sa[i]] = equal(y, sa[i], sa[i-1], k) ? p-1 : p++;
if(p >= n) break;
m = p;
}
}
int rank[maxn], height[maxn];
void get_height() {
REP(i, 0, n) rank[sa[i]] = i;
int k = 0;
REP(i, 0, n) {
if(k) k--;
if(!rank[i]) { k = 0; continue; }
int j = sa[rank[i] - 1];
while(s[i+k] == s[j+k]) k++;
height[rank[i]] = k;
}
}
int C[maxn];
#define lowbit(x) (x&(-x))
void init() { memset(C, -1, sizeof(C)); }
void upd(int x, int v) {
while(x <= n) {
C[x] = max(C[x], v);
x += lowbit(x);
}
}
int query(int x) {
int ans = -1;
while(x) {
ans = max(ans, C[x]);
x -= lowbit(x);
}
return ans;
}
int pre[maxn], suf[maxn];
int main() {
scanf("%d\n%s\n%s", &n, s, t);
reverse(s, s + n);
reverse(t, t + n);
LL ans = 0;
REP(i, 0, n) if(t[i] == '0') { ans = n - i; break; }
REP(i, 0, n) { s[i] -= 'a' - 1; }
build_sa();
get_height();
REP(i, 0, n) sum[i] = t[sa[i]] == '1';
REP(i, 1, n) sum[i] += sum[i - 1];
REP(i, 0, n) { upd(height[i] + 1, i); pre[i] = query(height[i]); }
init();
PER(i, 0, n) { upd(height[i] + 1, n - i - 1); suf[i] = n - query(height[i]) - 1; }
REP(i, 1, n) {
if(!height[i]) continue;
LL len = suf[i] - pre[i];
LL ban = sum[suf[i] - 1];
if(pre[i] > 0) ban -= sum[pre[i] - 1];
ans = max(ans, (LL)(len - ban) * height[i]);
}
printf("%I64d\n", ans);
return 0;
}
CodeForces 873F Forbidden Indices 后缀数组的更多相关文章
- Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...
- 【CF873F】Forbidden Indices 后缀自动机
[CF873F]Forbidden Indices 题意:给你一个串s,其中一些位置是危险的.定义一个子串的出现次数为:它的所有出现位置中,不是危险位置的个数.求s的所有子串中,长度*出现次数的最大值 ...
- CF873F Forbidden Indices 后缀自动机+水题
刷刷水~ Code: #include <cstdio> #include <cstring> #include <algorithm> #define N 200 ...
- codeforces 873F(后缀数组)
题意 给一个长度不超过200000的字符串s,假定有一个字符串a,这个字符串在s中出现次数是f(a),你需要让$|a|f(a)$最大. 但是有一些位置是禁止的,即以该位置为结束位置的字符串不计数. 分 ...
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces #123D: 后缀数组+单调栈
D. String You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤ ...
随机推荐
- UIWindow及程序启动的过程
1. UIWindow才有自发显示的功能, 一个程序之所以能显示东西,是因为有window !// [self.window makeKeyAndVisible]; 2. 任何view的显示 ...
- http:origin,referer和host区别
发起一个ajax请求时,request header里面有三个属性会涉及请求源信息.前端可能用不到这些值,但是,后台业务系统会比较关心它们,场景可能有: 处理跨域请求时,必须判断来源请求方是否合法:后 ...
- JavaScript(一) 对象基础
1.定义类或对象 1.1 混合的构造函数/原型方法 用构造函数定义对象的所有非函数属性,类似于Java的构造方法.用原型方法定义对象的函数属性(方法).这种方法是使用比较广泛的定义类或对象的方法. / ...
- CentOS6下DHCP服务(二)简单配置案例及故障排查
1.预分配网络参数如下:linux服务器:eth0 IP为192.168.8.250 做为局域网DHCP服务器局域网网段设置为192.168.8.0/24:内部计算机的router为192.168. ...
- BZOJ 3090: Coci2009 [podjela]
3090: Coci2009 [podjela] Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 23 Solved: 17[Submit][Statu ...
- C4C Product Price List的模型中和有效期相关的两个字段
SAP C4C的price list实例可以在工作中心Products,视图Price Lists里看到. 我们点开第二个名为TEST的实例: 我写这篇文章的日期是2018年10月27日, 我现在把这 ...
- Selenium入门12 鼠标和键盘事件
1 鼠标 集成在webdriver.ActionChains.单击.双击.右击.拖放等等. 2 键盘 引入包from selenium.webdriver.common.keys import K ...
- MHA启动及关闭
MHA启动及关闭 #masterha_manager --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/ap ...
- Next K Permutation
3457: Next K Permutation 时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 n 个数有 ...
- 机器学习实战之Logistic回归
Logistic回归一.概述 1. Logistic Regression 1.1 线性回归 1.2 Sigmoid函数 1.3 逻辑回归 1.4 LR 与线性回归的区别 2. LR的损失函数 3. ...