记录编号 |
389202 |
评测结果 |
AAAAAAAAAA |
题目名称 |
papertask |
最终得分 |
100 |
用户昵称 |
AntiLeaf |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.505 s |
提交时间 |
2017-03-30 20:36:40 |
内存使用 |
62.29 MiB |
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=1000010;
const unsigned long long base=131ull;
void solve(int,int);
void insert(unsigned long long*,int);
int expand(unsigned long long,int);
unsigned long long gethash(int,int);
int root,cnt=0,val[maxn]={0},par[maxn]={0};
map<unsigned long long,int>go[maxn];
unsigned long long h[maxn],pw[maxn],t[maxn];
char str[maxn];
long long ans=0;
int n,a[maxn],s[maxn],top,last=0;
int main(){
freopen("papertask.in","r",stdin);
freopen("papertask.out","w",stdout);
int __size__=128<<20;
char *__p__=(char*)malloc(__size__)+__size__;
__asm__("movl %0, %%esp\n"::"r"(__p__));
root=++cnt;
scanf("%d",&n);
scanf("%s",str+1);
pw[0]=1;
for(int i=1;i<=n;i++)pw[i]=pw[i-1]*base;
for(int i=n;i;i--)h[i]=h[i+1]*base+str[i];
for(int i=1;i<=n;i++){
if(str[i]=='(')s[++top]=i;
else{
if(!top)continue;
a[i]=s[top--];
a[a[i]]=i;
}
}
int x=1,last=0;
while(x<=n){
if(!a[x]){
x++;
continue;
}
last=x;
while(a[a[x]+1])x=a[x]+1;
solve(last,a[x]);
x=a[x]+1;
}
printf("%lld\n",ans);
return 0;
}
void solve(int l,int r){
if(l>r)return;
int cnt=0,x=l;
while(x<=r){
t[cnt++]=gethash(x,a[x]-x+1);
x=a[x]+1;
}
insert(t,cnt);
x=l;
while(x<=r){
solve(x+1,a[x]-1);
x=a[x]+1;
}
}
void insert(unsigned long long *a,int n){
int x=root,i=0;
for(;i<n;i++)x=expand(a[i],x);
}
int expand(unsigned long long c,int p){
int np=++cnt;
val[np]=val[p]+1;
while(p&&!go[p].count(c)){
go[p][c]=np;
p=par[p];
}
if(!p)par[np]=root;
else{
int q=go[p][c];
if(val[q]==val[p]+1)par[np]=q;
else{
int nq=++cnt;
val[nq]=val[p]+1;
go[nq]=go[q];
par[nq]=par[q];
par[np]=par[q]=nq;
while(p&&go[p][c]==q){
go[p][c]=nq;
p=par[p];
}
}
}
ans+=val[np]-val[par[np]];
return np;
}
inline unsigned long long gethash(int x,int l){return h[x]-h[x+l]*pw[l];}