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 ...
随机推荐
- Mysql --数据的增删改
插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 一. 在mysql管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用insert实现数据的插入 2.u ...
- 【原创】大数据基础之Flink(1)简介、安装、使用
Flink 1.7 官方:https://flink.apache.org/ 一 简介 Apache Flink is an open source platform for distributed ...
- Vofuria ARCamera相机问题
想要发射 射线 Camera.allCameras[0] 该语句来选择相机:Camera.main 此语句只能找到Tag为MainCamera的相机:
- laravel 汇总数据
public function userInfluenceCollect(Request $request) { $types = ['logins', "checkins", & ...
- Bootstrap-datepicker3官方文档中文翻译---概述(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
bootstrap-datepicker Bootstrap-datepicker 提供了一个拥有Bootstrap样式的弹性Datepicker控件 Requirements/使用要求 Bootst ...
- day19模块1
http://www.cnblogs.com/Eva-J/articles/7228075.html#_label10 collections模块: 在内置数据类型(dict.list.set.tup ...
- vue中html模板使用绑定的全局函数
我们知道在script中使用vue绑定的全局函数时, 我们需要用这种方式使用: this.Util.Fun(e) 那在模板中, 比如v-if中想使用Fun函数怎么办呢?你应该这样做 <i v-i ...
- webpack的devServer配置错误
首先声明,之前vue项目没有报错,做react项目这样配置就报错了. 一.问题描述 [HMR] Hot Module Replacement is disabled. 二.问题分析 不太理解控制台为什 ...
- C#中new的三种用法
在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数. 2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员. 3)new ...
- MyBatis:SQL语句中的foreach的详细介绍
foreach 也就是遍历迭代,在SQL中通常用在 in 这个关键词的后面foreach元素的属性主要有 item,index,collection,open,separator,close. 分别代 ...