bzoj 3238 Ahoi2013 差异
3238: [Ahoi2013]差异
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 2357 Solved: 1067
[Submit][Status][Discuss]
Description
.jpg)
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 ...
随机推荐
- windows设备驱动安装指南
高观点下的设备驱动安装(overview) 一.windows是怎样安装设备的? 第一步:新设备的识别 在给一个新设备安装驱动之前,总线或集线器(hub)驱动会为连接到PC上的设备分配一个硬件ID(h ...
- 构建WDK驱动出现fatal error U1087: cannot have : and :: dependents for same target
原因:WDK在编译驱动时,是不允许源文件所在的路径(全路径)中包含空格的,如果你包含了空格,就会出现上述错误. 解决方法:把源文件放在一个没有空格的路径下. reference: http://blo ...
- Bootstrap风格登录界面设计样例
参考:http://bootsnipp.com/tags/login Register Page 127.8K 187 Modal Login with jQuery Effects 159. ...
- sicily 1007 To and Fro
题意:字符串的操作处理 // Problem#: 8768 // Submission#: 2606406 // The source code is licensed under Creative ...
- Corrupted MAC on input
Corrupted MAC on input Incorrect MAC received on packet
- Oracle百问百答(四)
Oracle百问百答(四) 31.怎样查看某用户下的表? select table_name from all_tables where owner=upper('jhemr'); 32.怎样查看某用 ...
- phpcms:四、尾部包含
四.尾部包含1.包含尾部文件:{template "content","footer"}2.栏目列表调用(关于我们| 联系方式| 版权声明| 招聘信息|):{p ...
- python3-day5(模块)
1.获取路径import os,sys #获取全部路径 print(os.path.abspath(__file__)) #获取目录 print(os.path.dirname(os.path.abs ...
- MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
转载请注明出处:http://blog.csdn.net/bettarwang/article/details/40180271 在执行一个Hibernate的演示样例时,配置了<propert ...
- 高性能MySql进化论【转】
高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...