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

#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. 关于在Share point 2010 中保存SPFieldLookupValue类型到一个List中的问题

    在share point 中,有时字段的类型是lookup的,那么将会从另外的一个list中进行相应的连接,这是如果保存string等类型,将会报一个错, Invalid data has been ...

  2. word中已删除设置是否显示

        我们打开别人发过来的文档时经常会出现如下图所示,其实这个不是文档的异常,只是word设置显示了修订版标记,如果我们需要去除这个,只需要在审阅-修订功能下面选择显示最终版即可(测试用的10版,其 ...

  3. c++中explicit关键字用法

    C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类 ...

  4. 命令行输入Jmeter提示不是内部或外部命令,处理方式:添加环境变量

    命令行输入Jmeter提示不是内部或外部命令,需要在环境变量path中添加jmeter的bin目录绝对路径 我的电脑 > 右击 >属性 > 高级系统变量 > 环境变量 > ...

  5. Hash模板

    ;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hasht ...

  6. CentOS 6下PXE+Kickstart无人值守安装操作系统

    一.简介1.1 什么是PXEPXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作 ...

  7. Tomcat8.0.36安装配置

    1.下载tomcat8.0.36 下载地址:http://tomcat.apache.org/download-80.cgi 2.解压 至C:\Program Files\tomcat8下 3.添加系 ...

  8. CUDA Texture纹理存储器 示例程序

    原文链接 /* * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved. * Data: 2012-4-20 */ // // 此程序是演示了1D和 ...

  9. php开启短标签支持

    打开php.ini,找到 short_open_tag = Off ,将 Off 改为 On

  10. 开发必看 | iOS开发常用设计模式!

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~(一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则: ...