CF653F Paper task
题目链接:洛谷
首先我们不考虑本质不同这个限制。
既然不能直接用栈乱搞,我们就可以用一个前缀和的套路了。
我们将(设为1,将)设为-1,记前缀和为$s_i$,则$[i,j]$这一段是回文子串当且仅当
1.$s_j=s_{i-1}$
2.$\forall k\in [i,j],s_k\geq s_{i-1}$
于是我们枚举$i$,显然$j$要满足第二个性质就肯定不能超过一个上界,这个上界是可以二分的。每次check的时候就判断一下区间最小值,可以用ST表维护。
然后看看本质不同如何做。
这时候我们就要请出SA,求出$sa[]$和$ht[]$之后,枚举$sa[i]$作为左端点,此时必须$j\geq sa[i]+ht[i]$,其中$ht[]$是高度数组,否则就会与前面的字符串重复。
改一改二分的区间就可以了。
#include<bits/stdc++.h>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = ;
int n, a[N], m, sa[N], rak[N], tmp[N], c[N], *x = rak, *y = tmp, ht[N];
LL ans;
inline void Qsort(){
for(Rint i = ;i <= m;i ++) c[i] = ;
for(Rint i = ;i <= n;i ++) ++ c[x[y[i]]];
for(Rint i = ;i <= m;i ++) c[i] += c[i - ];
for(Rint i = n;i;i --) sa[c[x[y[i]]] --] = y[i];
}
inline void Ssort(){
m = ;
for(Rint i = ;i <= n;i ++){
x[i] = a[i]; y[i] = i;
}
Qsort();
for(Rint w = , p;w < n;w <<= , m = p){
p = ;
for(Rint i = n - w + ;i <= n;i ++) y[++ p] = i;
for(Rint i = ;i <= n;i ++) if(sa[i] > w) y[++ p] = sa[i] - w;
Qsort();
swap(x, y);
x[sa[]] = p = ;
for(Rint i = ;i <= n;i ++)
x[sa[i]] = (y[sa[i]] == y[sa[i - ]] && y[sa[i] + w] == y[sa[i - ] + w]) ? p : ++ p;
if(p >= n) break;
}
for(Rint i = ;i <= n;i ++) rak[sa[i]] = i;
int k = ;
for(Rint i = ;i <= n;i ++){
if(rak[i] == ) continue;
int j = sa[rak[i] - ];
if(k) -- k;
while(a[j + k] == a[i + k]) ++ k;
ht[rak[i]] = k;
}
}
int st[][N], lg2[N];
inline int query(int l, int r){
int k = lg2[r - l + ];
return min(st[k][l], st[k][r - ( << k) + ]);
}
vector<int> pos[N << ];
int main(){
scanf("%d", &n);
for(Rint i = ;i <= n;i ++){
int ch = getchar();
while(ch != '(' && ch != ')') ch = getchar();
a[i] = (ch == ')') + ;
}
Ssort();
st[][] = n;
for(Rint i = ;i <= n;i ++) st[][i] = st[][i - ] - a[i] * + ;
for(Rint i = ;i <= n;i ++) pos[st[][i]].push_back(i);
for(Rint i = ;i <= ;i ++)
for(Rint j = ;j <= n;j ++)
st[i][j] = min(st[i - ][j], st[i - ][j + ( << i - )]);
lg2[] = ;
for(Rint i = ;i <= n;i ++) lg2[i] = lg2[i >> ] + ;
for(Rint i = ;i <= n;i ++){
if(a[sa[i]] == ) continue;
int l = sa[i] + ht[i], r = n, mid, res = l - ;
while(l <= r){
mid = l + r >> ;
if(query(sa[i], mid) >= st[][sa[i] - ]){l = mid + , res = mid;}
else r = mid - ;
}
int t = st[][sa[i] - ];
ans += upper_bound(pos[t].begin(), pos[t].end(), res) - lower_bound(pos[t].begin(), pos[t].end(), sa[i] + ht[i]);
}
printf("%I64d\n", ans);
}
// nantf tai qiang le
CF653F
CF653F Paper task的更多相关文章
- [CF653F] Paper task - 后缀数组,线段树,vector
[CF653F] Paper task Description 给定一个括号序列,统计合法的本质不同子串的个数. Solution 很容易想到,只要在传统统计本质不同子串的基础上修改一下即可. 考虑经 ...
- Codeforces 653F Paper task SA
Paper task 如果不要求本质不同直接st表二分找出最右端, 然后计数就好了. 要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦. #include<bits/stdc++.h& ...
- CF IndiaHacks 2016 F Paper task 后缀数组
题目链接:http://codeforces.com/problemset/problem/653/F 大意是给出一个只包含'('和')'的括号串,求有多少不同的子串是合法的括号串 解法:对于每一个后 ...
- Tips for writing a paper
Tips for writing a paper 1. Tips for Paper Writing 2.• Before you write a paper • When you are writi ...
- How to (seriously) read a scientific paper
How to (seriously) read a scientific paper Adam Ruben’s tongue-in-cheek column about the common diff ...
- How to implement an algorithm from a scientific paper
Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scie ...
- ### Paper about Event Detection
Paper about Event Detection. #@author: gr #@date: 2014-03-15 #@email: forgerui@gmail.com 看一些相关的论文. 1 ...
- 1090-Rock, Paper, Scissors
描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
随机推荐
- 【原创】大叔问题定位分享(16)spark写数据到hive外部表报错ClassCastException: org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat cannot be cast to org.apache.hadoop.hive.ql.io.HiveOutputFormat
spark 2.1.1 spark在写数据到hive外部表(底层数据在hbase中)时会报错 Caused by: java.lang.ClassCastException: org.apache.h ...
- Python-web应用 +HTTP协议 +web框架
web架构 # web应用 架构# C/S 架构 | B/S 架构# client server: 客户端服务器架构,C++# browser server:浏览器服务器架构,Java.Python ...
- 26)django-form字段和插件widgets
创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML 一:常用字段 1.Django中Form类内置字段如下: 用于保存正则表达式 Choice ...
- 洛谷P5280 [ZJOI2019]线段树 [线段树,DP]
传送门 无限Orz \(\color{black}S\color{red}{ooke}\)-- 思路 显然我们不能按照题意来每次复制一遍,而多半是在一棵线段树上瞎搞. 然后我们可以从\(modify\ ...
- C# 解压与压缩文件
解压文件 ,引用 SharpZipLib.dll类库 方法一: public void UnGzipFile(string zipfilename) { //同压缩文件同级同名的非压缩文件路径 var ...
- CentOS 7 yum方式快速安装MongoDB
一.安装环境及配置yum # more /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # vi /etc/yum.repos.d/m ...
- Microsoft Office Word 中的公式自动编号
先插入公式,#,插入题注(交叉引用),生成了标号.此时整个公式是题注样式.在公式和标号之间插入一个样式分隔符. ____________________________________________ ...
- CLR Via 第一 章 知识点整理(2)程序集和CLR的启动
这一节先简单的讨论一下程序集以及CLR的初始化 虽然对应的编译器会生成托管模块,但实际上CLR不与托管模块工作,编译器除了编译还有将生成的托管模块转换为程序集的功能,微软还提供了工具AL.exe(程序 ...
- HttpConnection
1.HttpConnection 用于接受和发送网络数据 网络操作必须新开个子线程执行,否则会出现 ANR(Application Not Response) 应用无响应异常 Get: /** * 通 ...
- NOIP2017 d1t2 时间复杂度
题目传送门:洛谷P3952 大模拟不解释 #include<iostream> #include<cstdio> #include<cmath> #include& ...