题目链接:洛谷


首先我们不考虑本质不同这个限制。

既然不能直接用栈乱搞,我们就可以用一个前缀和的套路了。

我们将(设为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的更多相关文章

  1. [CF653F] Paper task - 后缀数组,线段树,vector

    [CF653F] Paper task Description 给定一个括号序列,统计合法的本质不同子串的个数. Solution 很容易想到,只要在传统统计本质不同子串的基础上修改一下即可. 考虑经 ...

  2. Codeforces 653F Paper task SA

    Paper task 如果不要求本质不同直接st表二分找出最右端, 然后计数就好了. 要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦. #include<bits/stdc++.h& ...

  3. CF IndiaHacks 2016 F Paper task 后缀数组

    题目链接:http://codeforces.com/problemset/problem/653/F 大意是给出一个只包含'('和')'的括号串,求有多少不同的子串是合法的括号串 解法:对于每一个后 ...

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

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

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

  7. ### Paper about Event Detection

    Paper about Event Detection. #@author: gr #@date: 2014-03-15 #@email: forgerui@gmail.com 看一些相关的论文. 1 ...

  8. 1090-Rock, Paper, Scissors

    描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...

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

随机推荐

  1. Java对象模型规约

      下面是我根据工作中项目的经验,总结的一套自己觉得比较方便的对象模型规约   model(实体模型)      -vo(与前端交互的对象模型,前端泛指页面.移动端和远程服务调用等)         ...

  2. Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍

    如果查询表的某一列,Hive中默认会启用MapReduce job来完成这个任务,如下: hive>select id,name from m limit 10;--执行时hive会启用MapR ...

  3. PHP迭代器:Iterator和IteratorAggregate

    使用迭代模式遍历所有的对象的时候,都必须实现Traversable(遍历)接口.但是Traversable是一个内部的类,只有用c语言编写的类才可以实现Traversable实现.如果我们在自定义的 ...

  4. springboot配置详解

    springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...

  5. __name__的意义与作用

    首先定义了一个test.py的文件,然后再定义一个函数,并在函数定义后直接运行: test.py def HaveFun():  if __name__ == '__main__':  print(' ...

  6. Node.js API 学习笔记

    常用 API 学习笔记 url 函数 url.parse: 解析 url 地址 url.resolve: 向 url 地址添加或替换字段 url.format: 生成 url 地址 querystri ...

  7. 服务器资源监控插件(jmeter)

    零.引言 我们对被测应用进行性能测试时,除了关注吞吐量.响应时间等应用自身的表现外,对应用运行所涉及的服务器资源的使用情况,也是非常重要的方面,通过 实时监控,可以准确的把握不同测试场景下服务器资源消 ...

  8. Centos6.5部署vsftpd+mysql认证

    1.FTP传输原理 FTP,文件传输协议,是工作在应用层,基于TCP实现,依赖于互联网即可通讯. 1)连接模式 控制(命令)连接,用来通信,一直在线,客户端随机端口连接服务端TCP:21端口. 数据连 ...

  9. Data Science Project

    https://drivendata.github.io/cookiecutter-data-science/

  10. rabbitmq应用

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: zengchunyun ""& ...