3238: [Ahoi2013]差异

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 2357  Solved: 1067
[Submit][Status][Discuss]

Description

Input

一行,一个字符串S

Output

一行,一个整数,表示所求值

Sample Input

cacao

Sample Output

54

HINT

2<=N<=500000,S由小写英文字母组成

Source

[Submit][Status][Discuss]


后缀数组+单调栈

今天终于学了后缀数组 记得去年的寒假 zjw学长给我们讲过这个东西 那时连倍增是什么都不知道。。。

我的后缀数组 是 nlog2n的 (因为我不会那个什么基数排序)

题目是道裸题 可以练练手

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define For(i,x,y) for(int i=x;i<=y;++i)
  6. const int N = ;
  7. using namespace std;
  8. char s[N];
  9. int sa[N],rk[N],h[N];
  10. int Log[N];int n;
  11. int st[N][];
  12. struct data{
  13.     int i,x,y;
  14.     bool operator < (const data&a)const{
  15.         return (x<a.x||x==a.x&&y<a.y);
  16.     }
  17.     bool operator != (const data&a)const{
  18.         return (x!=a.x||y!=a.y);
  19.     }
  20. }a[N];
  21. void calh(){
  22.     int i,k=,j;
  23.     For(i,,n) rk[sa[i]]=i;
  24.     for(i=;i<=n;h[rk[i++]]=k)
  25.     {
  26.         if(k>)k--;int j=sa[rk[i]-];
  27.         while(s[i+k]==s[j+k])k++;
  28.     }
  29.              
  30.     return;
  31. }
  32. //int lcp(int x,int y){
  33. //    if(x==y)return n-x+1;
  34. //    x=rk[x];y=rk[y];
  35. //    if(x>y){x^=y;y^=x;x^=y;}
  36. //    int len=y-x;x++;
  37. //    return min(st[x][Log[len]],st[1+y-1<<Log[len]][Log[len]]);
  38. //}
  39. long long q[N],qsum[N];
  40. int main()
  41. {
  42. //  freopen("sa.in","r",stdin);
  43. //  freopen("sa.out","w",stdout);
  44.     scanf("%s",s+);
  45.     n=strlen(s+);
  46.     For(i,,n)rk[i]=s[i];
  47.     For(i,,N-)Log[i]=Log[i>>]+;
  48.     memset(st,,sizeof(st));
  49.     for(int j=;j<=Log[n]+;++j){
  50.         For(i,,n) a[i]=(data){i,rk[i],rk[i+(<<j)]};
  51.         sort(a+,a+n+);int k=;
  52.         For(i,,n){
  53.             rk[a[i].i]=k;
  54.             if(a[i]!=a[i+])k++;
  55.         }
  56.     }
  57.     For(i,,n)sa[rk[i]]=i;
  58.     calh();
  59. //  For(i,1,n)printf("%d ",h[i]);
  60. //  For(i,1,n)st[i][0]=h[i];
  61. //  for(int j=1;j<=Log[n]+1;++j){
  62. //      For(i,1,n){
  63. //          if(i+(1<<j-1)>n)break;
  64. //          st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
  65. //      }
  66. //  }
  67.     long long ans=;
  68.     int r=;long long sum=;
  69.     For(i,,n){
  70.         long long tsum=;
  71.         while(r>&&q[r]>h[i])
  72.             sum=sum-q[r]*qsum[r],tsum+=qsum[r],r--;
  73.         q[++r]=h[i];qsum[r]=tsum;sum+=tsum*h[i];
  74.         ans-=*sum;
  75.     }
  76.     For(i,,n) ans+=1LL*i*(n-);
  77.     cout<<ans;
  78.     return ;
  79. }

bzoj 3238 Ahoi2013 差异的更多相关文章

  1. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  2. BZOJ 3238: [Ahoi2013]差异 [后缀自动机]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2512  Solved: 1140[Submit][Status ...

  3. bzoj 3238: [Ahoi2013]差异 -- 后缀数组

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...

  4. ●BZOJ 3238 [Ahoi2013]差异

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...

  5. 洛谷 P4248: bzoj 3238: [AHOI2013]差异

    题目传送门:洛谷 P4248. 题意简述: 定义两个字符串 \(S\) 和 \(T\) 的差异 \(\operatorname{diff}(S,T)\) 为这两个串的长度之和减去两倍的这两个串的最长公 ...

  6. BZOJ 3238 [Ahoi2013]差异(后缀自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个串,设T[i]表示从第i位开始的后缀, 求sum(len( ...

  7. BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp

    http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...

  8. BZOJ 3238: [Ahoi2013]差异((单调栈+后缀数组)/(后缀树))

    [传送门[(https://www.lydsy.com/JudgeOnline/problem.php?id=3238) 解题思路 首先原式可以把\(len\)那部分直接算出来,然后通过后缀数组求\( ...

  9. BZOJ.3238.[AHOI2013]差异(后缀自动机 树形DP/后缀数组 单调栈)

    题目链接 \(Description\) \(Solution\) len(Ti)+len(Tj)可以直接算出来,每个小于n的长度会被计算n-1次. \[\sum_{i=1}^n\sum_{j=i+1 ...

随机推荐

  1. Swift 总结使用问号(?)和感叹号(!)-备用

    在使用可选类型和可选链时,多次使用了问号(?)和感叹号(!),但是它们的含义是不同的,下面我来详细说明一下. 1. 可选类型中的问号(?) 声明这个类型是可选类型,访问这种类型的变量或常量时要使用感叹 ...

  2. GoogLeNet学习心得

    转自:http://blog.csdn.net/liumaolincycle/article/details/50471289#t0 综述: http://blog.csdn.net/sunbaigu ...

  3. Zookeeper,也要接触起来啦

    分布式的东东,就是部署也方便,但管理,想法,大集群应用是要点...! 参考如下URL简单实现 ,以后应用时多留意. http://blog.csdn.net/shirdrn/article/detai ...

  4. EE就业最好的方向是转CS,其次是VLSI/ASIC DESIGN & VERIFICATION

    Warald在2012年写过一篇文章<EE现在最好就业的方向是VLSI/ASIC DESIGN VERIFICATION>,三年过去了,很多学电子工程的同学想知道现在形势如何. 首先,按照 ...

  5. BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 414  Solved: ...

  6. Spark集群模式概述

    作者:foreyou出处:http://www.foreyou.net/2015/06/22/spark-cluster-mode-overview/声明:本文采用以下协议进行授权: 署名-非商用|C ...

  7. hdu 5012 Dice

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  8. Android菜鸟的成长笔记(28)——Google官方对Andoird 2.x提供的ActionBar支持

    在Google官方Android设计指南中(链接:http://www.apkbus.com/design/get-started/ui-overview.html)有一个新特性就是自我标识,也就是宣 ...

  9. 【InversionCount 逆序对数 + MergeSort】

    Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < ...

  10. Android Notification使用及取消

    //发送通知 NotificationManager manger = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE ...