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 ...
随机推荐
- Mac 解决 Sourcetree 同步代码总需要密码的问题
git config --global credential.helper osxkeychain
- 03-django模型(1)
一.内容回顾 1.路由层 a.简单使用 b.有名分组 c.路由分发 d.反向解析 2.视图层 a.HttpRequest对象 常用的属性 常用方法 b.HttpResponse对象 响应三剑客 3.模 ...
- php树形结构数组转化
/** * @param array $list 要转换的结果集 * @param string $pid parent标记字段 * @param string $level level标记字段 */ ...
- json server服务器
json文件可以理解为数据库 一.json-server快速搭建RESTAPI 安装: sudo cnpm install -g json-server 启动(使用): json-server指向js ...
- js-事件以及window操作
属性 当以下情况发生时,出现此事件 onblur 元素失去焦点 onchange 用户改变域的内容 onclick 鼠标点击某个对象 ondblclick 鼠标双击某个对象 onfocus 元素获得焦 ...
- LeetCode 929.Unique Email Addresses
Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...
- [LeetCode] Letter Case Permutation 字母大小写全排列
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- java控制台连接数据库
分四个步骤 *1.加载驱动程序*2.数据库连接字符串*3.数据库登录名和密码*4.最后关闭. 代码 package LinkMySQL; import java.sql.Connection; imp ...
- Hibernate-day02
OID 1,对象里面没有主键的概念,对象中对应主键的属性,称为OID(对象标识符);2,OID用来唯一标明一个对象实体(加上对象类型)3,OID在对象里面不见得只有一个属性;(映射复合主键)4,OID ...
- python 中的类
1.1 类里面包括:类的属性.方法 OO的特征(Object Oriented) 封装:信息隐蔽技术 继承:继承是子类自动共享父类之间数据和方法的机制 >>> class MyCla ...