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的 (因为我不会那个什么基数排序)

题目是道裸题 可以练练手

代码:

#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 差异的更多相关文章

  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. 改变VC生成exe图标

    默认 VC 生成的 EXE 文件的图标很大众,可以根据以下办法修改 exe 的图标. 以 Visual C++ 6.0 为例: 1. 创建项目,添加代码,并且保证项目可以正常编译. 2. 为项目增加资 ...

  2. (转)PHP zval内存回收机制和refcount_gc和is_ref_gc

    出处 : http://blog.sina.com.cn/s/blog_75a2f94f0101gygh.html 对于PHP这种需要同时处理多个请求的程序来说,申请和释放内存的时候应该慎之又慎,一不 ...

  3. 处理通过<input type="file">的Post 请求

    [HttpPost] public ActionResult Cal() { string ExcelName = System.DateTime.Now.ToString("yyyyMMd ...

  4. JVM基础和调优(四)

    垃圾回收算法中的一些问题 再上一遍中,说道JVM并不是采用一种垃圾回收的方法,因为不同的内存块采取的方法是不样的,那么:为什么要分块?为什么不采用同一种方法回收垃圾,这样不是更加的统一吗? 分块的垃圾 ...

  5. c++命名空间瀑布

    一般情况,函数放在命名空间外,命名空间内部放置声明 #include<iostream> using namespace std; //一般情况,函数放在命名空间外,命名空间内部放置声明 ...

  6. 运行tomcat7w.exe tomcat7.exe ,提示 指定的服务未安装 unable to open the service 'tomcat7'

    运行tomcat7w.exe tomcat7.exe ,提示 指定的服务未安装 unable to open the service 'tomcat7'(用的是绿色的Tomcat7) 解决方法: 打开 ...

  7. Appium 点击Android屏幕

    用driver.tap(1, 10, 10, 800); 点击屏幕,经常提示:An unknown server-side error occurred while processing the co ...

  8. JS~对img标签进行优化,使用onerror进行默认图像显示

    对于网站图像的不显示有很多原因,网络问题,文件本身问题,文件URL问题等,而当图像加载失败时会触发onerror这个事件,我们利用这点,可以有效的避免图像加载失败的尴尬! JS代码 //图像加载出错时 ...

  9. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  10. HLS直播和时移项目上线

    兄弟姐妹们: 天翼视讯的高清HLS直播和时移项目上线,测试观看地址为:http://118.85.192.228/live/,同时支持移动终端和pc终端.移动终端支持ios client和androi ...