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

#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. 运行python文件报SyntaxError:Non-ASCII character '\xe7'

    以下是报错内容: 在文件页头加上: #coding=uft-8 ~解决了~ 记录一下(捂脸)

  2. 压力测试工具ab的使用

    ab是Apache自带的HTTP压力测试工具,全称是ApacheBench 路径为\Apache\bin\ab.exe 参数文档: http://httpd.apache.org/docs/2.2/p ...

  3. JavaScript(一) 对象基础

    1.定义类或对象 1.1 混合的构造函数/原型方法 用构造函数定义对象的所有非函数属性,类似于Java的构造方法.用原型方法定义对象的函数属性(方法).这种方法是使用比较广泛的定义类或对象的方法. / ...

  4. 笨办法学Python(三十一)

    习题 31: 作出决定 这本书的上半部分你打印了一些东西,而且调用了函数,不过一切都是直线式进行的.你的脚本从最上面一行开始,一路运行到结束,但其中并没有决定程序流向的分支点.现在你已经学了 if,  ...

  5. ubuntu修改字体大小

    字体大小可以整体修改 System Settings -> Displays -> Scale for menu and title bars

  6. 在你的andorid设备上运行netcore (Linux Deploy)

    最近注意到.net core 的新版本已经开始支持ARM 平台的CPU, 特意去Linux Deploy 中尝试了一下,真的可以运行 Welcome to Ubuntu 16.04 LTS (GNU/ ...

  7. April 30 2017 Week 18 Sunday

    Our lives stretched out ahead of us, like a perpetual sunrise. 生命如永恒的日出,生生不息. Please respect yoursel ...

  8. 模拟猜数(POJ2328)

    题目链接:http://poj.org/problem?id=2328 解题报告: 缩短区间,soeasy, #include <stdio.h> #include <stdlib. ...

  9. 2017.10.6 Java命名规范及使用情况

    Package 的命名 Package 的名字应该都是由一个小写单词组成. Class 的命名 Class 的名字必须由大写字母开头而其他字母都小写的单词组成 Class 变量的命名 变量的名字必须用 ...

  10. PHP获取当前页面完整路径URL

    //PHP获取当前页面完整路径URL 1 <?php function getFullUrl(){ # 解决通用问题 $requestUri = ''; if (isset($_SERVER[' ...