2016暑假多校联合---Substring

Problem Description
?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. 
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.
 
Input
The first line of the input gives the number of test cases T;T test cases follow. 
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.

 
Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the answer you get for that case.
 
Sample Input
2
a
abc
b
bbb
 
Sample Output
Case #1: 3
Case #2: 3
 
Hint

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.

 
Author
FZU
 
Source

题意:输入字符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(后缀数组)的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  3. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  4. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  5. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  6. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  7. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

  8. 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...

  9. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

随机推荐

  1. javascript中可变值与不可变值(原始值)

    字符串原始值修改不了1 var str = "abc"; 2 str[0] = "d"; 3 console.log(str[1]="f") ...

  2. Oracle 11g系列:视图

    视图是数据库中特有的对象,视图用于存储查询,但不会存储数据(物化视图除外).这是视图和数据表的重要区别.Oracle中有4种视图:关系视图.内嵌视图.对象视图和物化视图. 1.关系视图 1>.创 ...

  3. 【WP开发】手电筒

    或许很多人都想到,可以利用手机上摄像头的闪光灯做手电筒,当然,有利必有害,每次使用的时间不要过长,几分钟一般不会有什么问题,如果时间太长,难保会有损伤. 以往的方案是调用视频录制功能来开始录制视频,同 ...

  4. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  5. Javascript定时器(三)——setTimeout(func, 0)

    setTimeout(func, 0)可以使用在很多地方,拆分循环.模拟事件捕获.页面渲染等 一.setTimeout中的delay参数为0,并不是指马上执行 <script type=&quo ...

  6. HTML Inspector – 帮助你编写高质量的 HTML 代码

    HTML Inspector 是一款代码质量检测工具,帮助你编写更优秀的 HTML 代码.HTML Inspector 使用 JavaScript 编写,运行在浏览器中,是最好的 HTML 代码检测工 ...

  7. [Java 基础]接口

    抽象类和抽象方法 抽象方法 在面向对象编程语言中抽象方法指一些只有方法声明而没有具体方法体的方法抽象方法一般存在于抽象类或接口中. java中,抽象方法就是以 abstract 修饰的方法,只需要声明 ...

  8. windows下配置Faster-RCNN

    mark一个 http://yun.baidu.com/share/link?shareid=1018944597&uk=1543560377 http://blog.csdn.net/sin ...

  9. 浏览器退出之后php还会继续执行么?

    浏览器退出之后php还会继续执行么? 前提:这里说的是典型的lnmp结构,nginx+php-fpm的模式 如果我有个php程序执行地非常慢,甚至于在代码中sleep(),然后浏览器连接上服务的时候, ...

  10. 扩展Exception,增加判断Exception是否为SQL引用约束异常方法!

    在设计数据表时,如果将某些列设置为关联其它表的外键,那么如果对其进行增加.修改操作时,其关联表若没有相匹配的记录则报错,或者在对其关联表进行删除时,也会报错,这就是外键约束的作用,当然除了外键还有许多 ...