2016暑假多校联合---Substring(后缀数组)
But ?? thinks that is too easy, he wants to make this problem more interesting.
?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X.
However, ?? is unable to solve it, please help him.
Each test case is consist of 2 lines:
First line is a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.
T<=30
1<=|S|<=10^5
The sum of |S| in all the test cases is no more than 700,000.
In first case, all distinct substrings containing at least one a: a, ab, abc.
In second case, all distinct substrings containing at least one b: b, bb, bbb.
题意:输入字符x和一个字符串,求包含字符x的不同子串的个数;
思路: 后缀数组sum=length-(sa[i]+height[i])[i从1~length] sum即为子串个数,稍作修改,用nxt[i]表示在i右侧距离i最近的字符x的坐标,则
sum=length-max(nxt[sa[i]],(sa[i]+height[i])) [i从1~length]就是所求结果;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e5+;
char s[maxn];
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int sa[maxn],ran[maxn],height[maxn]; int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
} void da(char *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) wss[i]=;
for(i=; i<n; i++) wss[x[i]=(int)r[i]]++;
for(i=; i<m; i++) wss[i]+=wss[i-];
for(i=n-; i>=; i--) sa[--wss[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<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) wss[i]=;
for(i=; i<n; i++) wss[wv[i]]++;
for(i=; i<m; i++) wss[i]+=wss[i-];
for(i=n-; i>=; i--) sa[--wss[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
} void callheight(char *r,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;i++)
ran[sa[i]]=i;
for(i=;i<n;height[ran[i++]]=k)
for(k?k--:,j=sa[ran[i]-];r[i+k]==r[j+k];k++);
return ;
} int main()
{
int T;
int Case=;
cin>>T;
char x;
while(T--)
{
scanf(" %c",&x);
scanf("%s",s);
int len=strlen(s);
da(s,sa,len+,);
callheight(s,sa,len);
int nxt[];
int tmp=len;
long long sum=;
for(int i=len-;i>=;i--)
{
if(s[i]==x) tmp=i;
nxt[i]=tmp;
}
for(int i=;i<=len;i++)
{
sum+=(long long)(len-max(sa[i]+height[i],nxt[sa[i]]));
}
printf("Case #%d: %lld\n",Case++,sum);
}
return ;
}
2016暑假多校联合---Substring(后缀数组)的更多相关文章
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- 2016暑假多校联合---Another Meaning
2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- 2016暑假多校联合---Counting Intersections
原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
- 2016暑假多校联合---GCD
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
随机推荐
- html5视频全频播放
html5视频全频播放 旋转90度 对video进行缩放 修正position 效果还凑合 代码 $(media).rotate({ // angle: 90, duration: 100, anim ...
- java基础-继承:矩形体积类问题
28.按要求编写一个Java应用程序: (1)定义一个类,描述一个矩形,包含有长.宽两种属性,和计算面积方法. (2)编写一个类,继承自矩形类,同时该类描述长方体,具有长.宽.高属性, 和计算体积的方 ...
- seajs模块化开发
seajs是一个起辅助作用的库,所以它可以更方便开发,而它可以解决以下问题: 1.命名问题,就是冲突 2.性能问题,就是只要一个功能,但却使用一个大插件中的一个小功能,所以要手动拆分出这个功能 3.j ...
- Enterprise Solution 开发框架功能点
1. 通用查询模块,可以通过关联数据库表,存储过程或程序代码开发查询,多个查询之间也可构成主从关联查询. 2. 业务异常处理 支持统一的异常处理. 3. 内置一个简单的SQL Server查询分析器, ...
- 深入理解javascript作用域系列第一篇——内部原理
× 目录 [1]编译 [2]执行 [3]查询[4]嵌套[5]异常[6]原理 前面的话 javascript拥有一套设计良好的规则来存储变量,并且之后可以方便地找到这些变量,这套规则被称为作用域.作用域 ...
- Kubernetes如何使用kube-dns实现服务发现
大纲: • Kubernetes中如何发现服务 • 如何发现Pod提供的服务 • 如何使用Service发现服务 • 如何使用kube-dns发现服务 ...
- Docker 有什么优势?
1.什么是容器? 依托与linux 内核功能的虚拟化技术 2. docker 是什么? 能够把应用程序自动部署到容器的开源引擎 3. docker 跟原有的工具有何区别? 传统的部署模式是:安装(包管 ...
- 使用Google产品以来遇到的最糟糕、最霸道、最让人抓狂的设计
很久没有登录cnblogs@gmail.com这个邮箱,今天通过gmail.com登录了一下,登录后出现一个对话框要求设置性别与出生日期,而且必须要设置,不设置不让登录. 这个邮箱是我们网站用的是邮箱 ...
- spring aop源码实现分析
1. 先分析Advice before执行Cglib2AopProxy的intercept方法: /** * General purpose AOP callback. Used when the t ...
- IOS开发-KVO
一.什么是kvo? key-value observing,观察者模式 观察者,观察对象属性的变化,当被观察者该属性发生变化时,观察者会接收到通知,可以在回调函数中做相应的处理 二.有什么作用? 变化 ...