[HAOI2016]找相同子串

【题目描述】

给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数。两个方案不同当且仅当这两个子串中有一个位置不同。

【输入格式】

两行,两个字符串s1,s2,长度分别为n1,n2。

【输出格式】

输出一个整数表示答案。

【样例输入】

aabb
bbaa

【样例输出】

10

【数据范围】

对于20%的数据,满足1≤n1,n2≤500;

对于40%的数据,满足1≤n1,n2≤5000;

对于100%的数据,满足1≤n1,n2≤200000,字符串中只有小写字母。

  和POJ 3415几乎一样。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
char s[N];int len,d;
int r[N],Wa[N],Wb[N],sa[N],rk[N];
int Ws[N],Wv[N],lcp[N]; bool cmp(int *p,int i,int j,int l){
return p[i]==p[j]&&p[i+l]==p[j+l];
} void DA(int n,int m){
int i,j,p,*x=Wa,*y=Wb;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[x[i]]]=i;
for(j=,p=;p<n;j<<=,m=p){
for(p=,i=n-j;i<n;i++)y[p++]=i;
for(i=;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[Wv[i]=x[y[i]]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[Wv[i]]]=y[i];
for(swap(x,y),p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
} void LCP(int n){
int i,j,k=;
for(i=;i<=n;i++)rk[sa[i]]=i;
for(i=;i<n;lcp[rk[i++]]=k)
for(k?k--:k,j=sa[rk[i]-];r[i+k]==r[j+k];k++);
} int ID(int x){x=sa[x];
if(x==d)return ;
if(x<d)return ;
return ;
} int st[N]={-},top;
long long sum[N],tot[N],ans;
int main(){
freopen("find_2016.in","r",stdin);
freopen("find_2016.out","w",stdout);
scanf("%s",s);
len=d=strlen(s);
scanf("%s",s+len+);
s[d]='#';len=strlen(s);
for(int i=;i<len;i++)r[i]=s[i];
DA(len+,);LCP(len);
for(int i=;i<=len;i++){
if(!ID(i))continue;
if(ID(i)==)ans+=tot[top];
if(i!=len){
st[++top]=lcp[i+];sum[top]=-ID(i);
tot[top]=st[top]*sum[top]+tot[top-];
while(st[top]<=st[top-]){
sum[top-]+=sum[top];
st[top-]=st[top];top--;
tot[top]=st[top]*sum[top]+tot[top-];
}
}
}
top=;
for(int i=;i<=len;i++){
if(!ID(i))continue;
if(ID(i)==)ans+=tot[top];
if(i!=len){
st[++top]=lcp[i+];sum[top]=ID(i)-;
tot[top]=st[top]*sum[top]+tot[top-];
while(st[top]<=st[top-]){
sum[top-]+=sum[top];
st[top-]=st[top];top--;
tot[top]=st[top]*sum[top]+tot[top-];
}
}
}
printf("%lld\n",ans);
return ;
}

字符串(后缀数组):HAOI2016 找相同子串的更多相关文章

  1. bzoj4556: [Tjoi2016&Heoi2016]字符串 (后缀数组加主席树)

    题目是给出一个字符串,每次询问一个区间[a,b]中所有的子串和另一个区间[c,d]的lcp最大值,首先求出后缀数组,对于lcp的最大值肯定是rank[c]的前驱和后继,但是对于这个题会出现问题,就是题 ...

  2. 【BZOJ3277/3473】串/字符串 后缀数组+二分+RMQ+双指针

    [BZOJ3277]串 Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). Inpu ...

  3. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  4. Bzoj4556: [Tjoi2016&Heoi2016]字符串 后缀数组

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 87[Sub ...

  5. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

  6. POJ-2774-Long Long Message(后缀数组-最长公共子串)

    题意: 给定两个字符串 A 和 B,求最长公共子串. 分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大 ...

  7. BZOJ 3277: 串/ BZOJ 3473: 字符串 ( 后缀数组 + RMQ + 二分 )

    CF原题(http://codeforces.com/blog/entry/4849, 204E), CF的解法是O(Nlog^2N)的..记某个字符串以第i位开头的字符串对答案的贡献f(i), 那么 ...

  8. BZOJ3473:字符串(后缀数组,主席树,二分,ST表)

    Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...

  9. 【BZOJ-4556】字符串 后缀数组+二分+主席树 / 后缀自动机+线段树合并+二分

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 657  Solved: 274[Su ...

随机推荐

  1. SetConsoleCtrlHandler 处理控制台消息

    转载自csdn:http://blog.csdn.net/zhongguoren666/article/details/8770615   SetConsoleCtrlHandler 处理控制台消息 ...

  2. 在调用Qt库来实现功能过程中的一些总结

    1.对于QTabWidget中tab名字的变化.当其中只有一个&时,Qt Assistant中给出的解释是:If the tab's label contains an ampersand, ...

  3. 转载:Python正则表达式

    原文在  http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python ...

  4. JQuery slideToggle闪烁问题及解决办法

    在使用slideToggle的时候,会出现在实现隐藏效果后闪烁一下在消失,找了很多原因,本以为是浏览器问题,后来发现是文档定义类型的问题... 原来页面的文档定义:<!DOCTYPE HTML ...

  5. IS about 64bit system

    This function supports the 64-bit parts of the registry by using the REGDB_OPTION_WOW64_64KEY option ...

  6. hw-text1

    Text 1 测试题 python是什么类型的语言? 解释型语言,是脚本语言 百娘(脚本语言是为了缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程而创建的计算机编程语 ...

  7. windows下使用cxfreeze打包python3程序

    1:下载适合版本的cxfreeze http://sourceforge.net/projects/cx-freeze/files/4.3.2/ 2:安装,注意python版本是否正确 3:安装完成后 ...

  8. iOS MD5加密算法

    考虑到用户账户安全,对用户的登录密码进行MD5加密 什么是MD5加密呢...懒了就不在这搬砖了,大家可以自己搜索查查,在此记录下代码,以供以后学习查询! 下面,直接上代码... // 需要倒入这个头文 ...

  9. 去除UINavigationBar默认透明度的方法

    UINavigationbar的属性translucent,用来控制导航条的透明度的: iOS7+版本后,navigationbar的translucent属性默认为YES,及默认带有透明度 [sel ...

  10. open files

    /* * * Copyright (c) International Business Machines Corp., 2001 * * This program is free software; ...