bzoj 3238 Ahoi2013 差异
3238: [Ahoi2013]差异
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 2357 Solved: 1067
[Submit][Status][Discuss]
Description
Input
一行,一个字符串S
Output
一行,一个整数,表示所求值
Sample Input
Sample Output
HINT
2<=N<=500000,S由小写英文字母组成
Source
后缀数组+单调栈
今天终于学了后缀数组 记得去年的寒假 zjw学长给我们讲过这个东西 那时连倍增是什么都不知道。。。
我的后缀数组 是 nlog2n的 (因为我不会那个什么基数排序)
题目是道裸题 可以练练手
代码:
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define For(i,x,y) for(int i=x;i<=y;++i)
- const int N = ;
- using namespace std;
- char s[N];
- int sa[N],rk[N],h[N];
- int Log[N];int n;
- int st[N][];
- struct data{
- int i,x,y;
- bool operator < (const data&a)const{
- return (x<a.x||x==a.x&&y<a.y);
- }
- bool operator != (const data&a)const{
- return (x!=a.x||y!=a.y);
- }
- }a[N];
- void calh(){
- int i,k=,j;
- For(i,,n) rk[sa[i]]=i;
- for(i=;i<=n;h[rk[i++]]=k)
- {
- if(k>)k--;int j=sa[rk[i]-];
- while(s[i+k]==s[j+k])k++;
- }
- return;
- }
- //int lcp(int x,int y){
- // if(x==y)return n-x+1;
- // x=rk[x];y=rk[y];
- // if(x>y){x^=y;y^=x;x^=y;}
- // int len=y-x;x++;
- // return min(st[x][Log[len]],st[1+y-1<<Log[len]][Log[len]]);
- //}
- long long q[N],qsum[N];
- int main()
- {
- // freopen("sa.in","r",stdin);
- // freopen("sa.out","w",stdout);
- scanf("%s",s+);
- n=strlen(s+);
- For(i,,n)rk[i]=s[i];
- For(i,,N-)Log[i]=Log[i>>]+;
- memset(st,,sizeof(st));
- for(int j=;j<=Log[n]+;++j){
- For(i,,n) a[i]=(data){i,rk[i],rk[i+(<<j)]};
- sort(a+,a+n+);int k=;
- For(i,,n){
- rk[a[i].i]=k;
- if(a[i]!=a[i+])k++;
- }
- }
- For(i,,n)sa[rk[i]]=i;
- calh();
- // For(i,1,n)printf("%d ",h[i]);
- // For(i,1,n)st[i][0]=h[i];
- // for(int j=1;j<=Log[n]+1;++j){
- // For(i,1,n){
- // if(i+(1<<j-1)>n)break;
- // st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
- // }
- // }
- long long ans=;
- int r=;long long sum=;
- For(i,,n){
- long long tsum=;
- while(r>&&q[r]>h[i])
- sum=sum-q[r]*qsum[r],tsum+=qsum[r],r--;
- q[++r]=h[i];qsum[r]=tsum;sum+=tsum*h[i];
- ans-=*sum;
- }
- For(i,,n) ans+=1LL*i*(n-);
- cout<<ans;
- return ;
- }
bzoj 3238 Ahoi2013 差异的更多相关文章
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- bzoj 3238: [Ahoi2013]差异 -- 后缀数组
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...
- ●BZOJ 3238 [Ahoi2013]差异
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...
- 洛谷 P4248: bzoj 3238: [AHOI2013]差异
题目传送门:洛谷 P4248. 题意简述: 定义两个字符串 \(S\) 和 \(T\) 的差异 \(\operatorname{diff}(S,T)\) 为这两个串的长度之和减去两倍的这两个串的最长公 ...
- BZOJ 3238 [Ahoi2013]差异(后缀自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个串,设T[i]表示从第i位开始的后缀, 求sum(len( ...
- BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp
http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...
- BZOJ 3238: [Ahoi2013]差异((单调栈+后缀数组)/(后缀树))
[传送门[(https://www.lydsy.com/JudgeOnline/problem.php?id=3238) 解题思路 首先原式可以把\(len\)那部分直接算出来,然后通过后缀数组求\( ...
- BZOJ.3238.[AHOI2013]差异(后缀自动机 树形DP/后缀数组 单调栈)
题目链接 \(Description\) \(Solution\) len(Ti)+len(Tj)可以直接算出来,每个小于n的长度会被计算n-1次. \[\sum_{i=1}^n\sum_{j=i+1 ...
随机推荐
- Swift 总结使用问号(?)和感叹号(!)-备用
在使用可选类型和可选链时,多次使用了问号(?)和感叹号(!),但是它们的含义是不同的,下面我来详细说明一下. 1. 可选类型中的问号(?) 声明这个类型是可选类型,访问这种类型的变量或常量时要使用感叹 ...
- GoogLeNet学习心得
转自:http://blog.csdn.net/liumaolincycle/article/details/50471289#t0 综述: http://blog.csdn.net/sunbaigu ...
- Zookeeper,也要接触起来啦
分布式的东东,就是部署也方便,但管理,想法,大集群应用是要点...! 参考如下URL简单实现 ,以后应用时多留意. http://blog.csdn.net/shirdrn/article/detai ...
- EE就业最好的方向是转CS,其次是VLSI/ASIC DESIGN & VERIFICATION
Warald在2012年写过一篇文章<EE现在最好就业的方向是VLSI/ASIC DESIGN VERIFICATION>,三年过去了,很多学电子工程的同学想知道现在形势如何. 首先,按照 ...
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
- Spark集群模式概述
作者:foreyou出处:http://www.foreyou.net/2015/06/22/spark-cluster-mode-overview/声明:本文采用以下协议进行授权: 署名-非商用|C ...
- hdu 5012 Dice
Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...
- Android菜鸟的成长笔记(28)——Google官方对Andoird 2.x提供的ActionBar支持
在Google官方Android设计指南中(链接:http://www.apkbus.com/design/get-started/ui-overview.html)有一个新特性就是自我标识,也就是宣 ...
- 【InversionCount 逆序对数 + MergeSort】
Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < ...
- Android Notification使用及取消
//发送通知 NotificationManager manger = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE ...