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 ≤ ...
随机推荐
- maven struts2工程StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter
maven搭建struts2工程时报错 严重: Exception starting filter struts2java.lang.ClassCastException: org.apache.st ...
- 老笔记本装xubuntu+win7
https://www.freezhongzi.info/?p=167 1 install xubuntu 分区如下 sda1 20g ext4 / sda2 80g ext4 /data sda3 ...
- WebRTC协议
webrtc协议介绍 MDN webrtc协议 ICE 交互式连接建立Interactive Connectivity Establishment (ICE) 是一个允许你的浏览器和对端浏览器建立连接 ...
- Altium_Designer17-PCB-如何重新定义板子外形
当我们绘制完电路图.完成PCB布局和布线后,我们会遇到下面这种情况: 发现什么了吗?那么大的黑框框,我们只用到了很少的一部分.那有啥?告诉你黑框框的大小就是你最后将要印刷的PCB板的实际大小!大家都知 ...
- Jmeter入门5 关联 响应数据传递-正则表达式提取器
在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...
- jade文档声明和头尾标签
作为一个页面,首先需要一个doctype的声明,它位于文档最上面的位置,放置html标签以前,用来告知浏览器当前这个页面用哪种html,或者xml的规范并解析页面 doctype html htm ...
- js日期转换Fri Oct 31 18:00:00 UTC+0800 2008转换为yyyy-mm-dd
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...
- How good are detection proposals, really?
How good are detection proposals, really? J. Hosang, R. Benenson, B. Schiele Oral at BMVC 2014 http: ...
- Data Warehouse 业务系统不入仓表
根据数据仓库的实施经验,凡符合如下特征的表,建议不入仓. ① 备份数据表 此类表是对现有表中某个时点数据的一份拷贝,根据需要进行数据恢复使用.因此,只需取当前表中的数据即可. ② 冗余数据表 同一类数 ...
- iso十款常用类库
iso十款常用类库 MBProgressHUD(进展指示符库) 地址:https://github.com/jdg/MBProgressHUD 苹果的应用程序一般都会用一种优雅的,半透明的进度 ...