忘了当时怎么做的了,先把代码贴上,保存一下后缀数组模板。

#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 后缀数组的更多相关文章

  1. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  2. 【CF873F】Forbidden Indices 后缀自动机

    [CF873F]Forbidden Indices 题意:给你一个串s,其中一些位置是危险的.定义一个子串的出现次数为:它的所有出现位置中,不是危险位置的个数.求s的所有子串中,长度*出现次数的最大值 ...

  3. CF873F Forbidden Indices 后缀自动机+水题

    刷刷水~ Code: #include <cstdio> #include <cstring> #include <algorithm> #define N 200 ...

  4. codeforces 873F(后缀数组)

    题意 给一个长度不超过200000的字符串s,假定有一个字符串a,这个字符串在s中出现次数是f(a),你需要让$|a|f(a)$最大. 但是有一些位置是禁止的,即以该位置为结束位置的字符串不计数. 分 ...

  5. 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 ...

  6. Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)

    题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...

  7. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  8. 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 ...

  9. Codeforces #123D: 后缀数组+单调栈

    D. String     You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤  ...

随机推荐

  1. 利用html5的FormData对象实现多图上传

    <html> <head> <title>FormData多图上传演示</title> </head> <body> <a ...

  2. PCB仿真软件与电磁场求解器的算法

    1. 简介 目前商业化的PCB仿真软件主要有: Cadence公司的Sigrity.Ansys公司的SIwave/HFSS.CST公司的CST.Mentor公司的HyperLynx.Polor公司的S ...

  3. ubuntu linux查看cpu信息

    $ cat /proc/cpuinfo CPU核心数量 $ grep -c processor /proc/cpuinfo

  4. 初始化mysql数据库时提示字符编码错误的解决办法

    有时候在安装完数据库并初始化的时候会出现如下错误: root@localhost mysql-5.5.19]# bash scripts/mysql_install_db --user=mysql - ...

  5. 基于指令的移植方式的几个重要概念的理解(OpenHMPP, OpenACC)-转载

    引言: 什么是基于指令的移植方式呢?首先我这里说的移植可以理解为把原先在CPU上跑的程序放到像GPU一样的协处理器上跑的这个过程.在英文里可以叫Porting.移植有两种方式:一种是使用CUDA或者O ...

  6. Javascript Ajax 请求

    var XMLHttpReq; function createXMLHttpRequest() { try { XMLHttpReq = new ActiveXObject("Msxml2. ...

  7. Linux赋予普通用户root权限

    本文以新建用户admin来举例,请自行替换自己需要的用户 方法一: vi编辑 /etc/sudoers 文件,找到 root    ALL=(ALL)     ALL,在下面添加一行,如下所示: ## ...

  8. 在jdbc基础上进阶一小步的C3p0 连接池(DBCP 不能读xml配置文件,已淘汰) 和DBUtils 中两个主要类QueryRunner和ResultSetHandler的使用

    首先看C3p0这个连接池,最大优势可以自动读取默认的配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  9. Python 文件访问模式

    f = open('xxx文件', '访问模式') r    以只读方式打开文件(read).文件的指针将会放在文件的开头.默认模式. w   打开一个文件只用于写入(write).如果该文件已存在则 ...

  10. 关于Vue 兄弟组件通信

    最近项目中遇到希望在操作路由组件里面内容的时候可以影响共用组件Header组件(这个其实就是他的兄弟组件)的操作.  意思就是 router-view指向的router来影响Header组件的信息 首 ...