bzoj 3238: [Ahoi2013]差异
来源:互联网 发布:如何设计一个软件 编辑:程序博客网 时间:2024/04/29 20:50
Description
Solution:
Code:
#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <algorithm>#include <iostream>using namespace std;char str[500010]="\0";struct sam{ int ch[26]; int lenth; int fail;}pot[1000010]={{{0},0,0}};int New[500010]={0};int pp=0;int First[1000010]={0};int N;struct bian_{ int to; int next; int dist;}bian[1000010]={{0,0,0}};int Size[1000010]={0};long long ans=0;void Addbian(int p,int q,int r,int k){ bian[k].to=q; bian[k].dist=r; bian[k].next=First[p]; First[p]=k; return;}void Add(int t,int c){ int cnt=New[t-1];New[t]=++pp;pot[New[t]].lenth=pot[New[t-1]].lenth+1; for(;cnt!=0 && pot[cnt].ch[c]==0;cnt=pot[cnt].fail) pot[cnt].ch[c]=New[t]; if(cnt==0) pot[New[t]].fail=1; else { int q=pot[cnt].ch[c]; if(pot[cnt].lenth+1==pot[q].lenth) pot[New[t]].fail=q; else { pp++;pot[pp]=pot[q];pot[pp].lenth=pot[cnt].lenth+1; pot[q].fail=pot[New[t]].fail=pp; for(;cnt!=0 && pot[cnt].ch[c]==q;cnt=pot[cnt].fail) pot[cnt].ch[c]=pp; } } return;}void dfs(int cnt,int depth){ for(int i=First[cnt];i!=0;i=bian[i].next) { int u=bian[i].to; dfs(u,depth+bian[i].dist); ans-=(long long)depth*Size[u]*Size[cnt]*2; Size[cnt]+=Size[u]; } if(New[pot[cnt].lenth]==cnt) { ans-=(long long)depth*Size[cnt]*2; Size[cnt]++; } return;}int main(){ scanf("%s",str+1); N=strlen(str+1); New[0]=++pp; for(int i=1;i<=N;i++) { Add(i,str[N-i+1]-'a'); ans+=(long long)i*(N-1); } for(int i=2;i<=pp;i++) Addbian(pot[i].fail,i,pot[i].lenth-pot[pot[i].fail].lenth,i-1); dfs(1,0); cout<<ans<<endl; return 0;}
0 0
- 【BZOJ 3238】 [Ahoi2013]差异
- bzoj 3238: [Ahoi2013]差异
- BZOJ 3238 [Ahoi2013]差异
- bzoj 3238 [Ahoi2013]差异
- BZOJ 3238 AHOI2013 差异 后缀自动机
- bzoj 3238: [Ahoi2013]差异 后缀数组
- BZOJ 3238: [Ahoi2013]差异 后缀数组
- bzoj 3238: [Ahoi2013]差异 后缀自动机
- bzoj 3238: [Ahoi2013]差异 后缀树
- bzoj 3238 ahoi2013差异 后缀数组
- BZOJ 3238 [Ahoi2013]差异 后缀自动机
- bzoj 3238 [Ahoi2013]差异(SAM解法)
- BZOJ 3238 [Ahoi2013]差异 后缀数组+单调栈
- BZOJ 3238: [Ahoi2013]差异|后缀数组|乘法原理
- bzoj 3238 [Ahoi2013]差异 后缀数组 并查集
- bzoj 3238: [Ahoi2013]差异 (后缀自动机+树形dp)
- 【BZOJ 3238】[Ahoi2013]差异 后缀自动机构造后缀树
- BZOJ 3238 [Ahoi2013]差异 后缀数组+st表
- javascript canvas fps 帧速率计算
- spring 解读之 基本bean装配
- 在Mathematica中定义矩阵的半张量积运算
- 使用adb解锁
- CF #244 (Div. 2)D
- bzoj 3238: [Ahoi2013]差异
- (可视化模型表现)Visualizing Model Performance
- jQury animate操作 background-position 方法
- Android之TextView实现跑马灯的效果(笔记)
- C#求平均分和高于平均分的同学姓名。
- linux下动态库的符号冲突、隐藏和强制优先使用库内符号
- [Hnoi2013]游走(bzoj3143)
- java通过HTTP接收xml和返回xml
- POJ 3111 K Best(最大化平均值)