[Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String
Description
每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种。
Solution
本题要求不是很高,\(O(n^2)\) 统计每个出现次数子串个数即可。
我因为一个lld WA了一晚上(猛然意识到要%d读入long long的时候,之前一直没有翻车的原因是开了全局,如果是局部又不初始化就瞬间gg了,然鹅这个错误本地查不出来)
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,q,m=256,sa[200005],y[200005],u[200005],v[200005],o[200005],r[200005],h[200005],T;
int buf[200005],answer[200005];
char str[200005];
int C[5005][5005];
const int modulo = 1e+9+7;
signed main()
{
C[0][0]=1;
for(int i=1;i<=5000;i++)
{
C[i][0]=1;
for(int j=1;j<=i;j++)
{
C[i][j]=(C[i-1][j]+C[i-1][j-1])%modulo;
}
}
scanf("%lld",&T);
while(T--)
{
memset(sa,0,sizeof sa);
memset(y,0,sizeof y);
memset(u,0,sizeof u);
memset(v,0,sizeof v);
memset(o,0,sizeof o);
memset(r,0,sizeof r);
memset(h,0,sizeof h);
memset(buf,0,sizeof buf);
memset(answer,0,sizeof answer);
memset(str,0,sizeof str);
scanf("%lld%lld",&n,&q);
scanf("%s",str+1);
for(int i=1; i<=n; i++) u[str[i]]++;
for(int i=1; i<=m; i++) u[i]+=u[i-1];
for(int i=n; i>=1; i--) sa[u[str[i]]--]=i;
r[sa[1]]=1;
for(int i=2; i<=n; i++) r[sa[i]]=r[sa[i-1]]+(str[sa[i]]!=str[sa[i-1]]);
for(int l=1; r[sa[n]]<n; l<<=1)
{
memset(u,0,sizeof u);
memset(v,0,sizeof v);
memcpy(o,r,sizeof r);
for(int i=1; i<=n; i++) u[r[i]]++, v[r[i+l]]++;
for(int i=1; i<=n; i++) u[i]+=u[i-1], v[i]+=v[i-1];
for(int i=n; i>=1; i--) y[v[r[i+l]]--]=i;
for(int i=n; i>=1; i--) sa[u[r[y[i]]]--]=y[i];
r[sa[1]]=1;
for(int i=2; i<=n; i++) r[sa[i]]=r[sa[i-1]]+((o[sa[i]]!=o[sa[i-1]])||(o[sa[i]+l]!=o[sa[i-1]+l]));
}
{
int i,j,k=0;
for(int i=1; i<=n; h[r[i++]]=k)
for(k?k--:0,j=sa[r[i]-1]; str[i+k]==str[j+k]; k++);
}
buf[1]=n*(n+1ll)/2ll;
for(int i=1; i<=n; i++) buf[1]-=h[i];
for(int i=1; i<=n; i++)
{
int l=h[i],r=h[i+1];
if(l<r)
{
++l;
for(int j=i+1; j<=n+1 && l<=r; j++)
{
while(r>h[j]&&l<=r)
{
buf[j-i]++;
--r;
}
}
}
}
for(int i=2;i<=n;i++) buf[1] -= buf[i];
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
(answer[i]+=(C[j][i]*buf[j])%modulo) %= modulo;
}
}
//for(int i=1;i<=n;i++) cout<<buf[i]<<" ";
//cout<<endl;1
for(int i=1;i<=q;i++)
{
int tmp;
scanf("%lld",&tmp);
if(tmp<=n) printf("%lld\n",answer[tmp]);
else printf("0\n");
}
}
}
[Codechef CHSTR] Chef and String - 后缀数组的更多相关文章
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- hdu 6194 沈阳网络赛--string string string(后缀数组)
题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...
- HDU 6194 string string string (后缀数组)
题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...
- Hackerrank--Ashton and String(后缀数组)
题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...
- POJ3729 Facer’s string 后缀数组
Fa ...
- hdu 5030 Rabbit's String(后缀数组&二分法)
Rabbit's String Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu-6194 string string string 后缀数组 出现恰好K次的串的数量
最少出现K次我们可以用Height数组的lcp来得出,而恰好出现K次,我们只要除去最少出现K+1次的lcp即可. #include <cstdio> #include <cstrin ...
- [Codechef TASTR] Level of Difference - 后缀数组,容斥原理
[Codechef TASTR] Level of Difference Description 给定两个字符串,求恰好在一个字符串中出现过的本质不同的子串数量. Solution 设 \(U(S)\ ...
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
随机推荐
- Flutter初探_环境配置以及创建项目
还没学会这个怎么排版,写了一版 太丑 没发看,' 先换到我熟悉的网站,后面搞定了排版再更新过来 https://www.jianshu.com/p/6fc913861461
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hdfs分布式文件系统安装
实验目的 复习安装jdk 学习免密码登录 掌握安装配置hdfs集群的方法 掌握hdfs集群的简单使用和检查其工作状态 实验原理 1.hdfs是什么 hadoop安装的第一部分是安装hdfs,hdfs是 ...
- C语言->关于文件数据的录入和输出调用的函数总结
数据输入输出对象之间的关系图: 函数使用说明: 1.一个字符的输入\输出,对象是键盘(缓存和屏幕) 1.1.getchar(a),putchar(a); 1.2.scanf(“%d”,&i), ...
- MyEclipse配置和使用Maven
maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 工具/原料 myeclipse maven 方法/步骤 ...
- 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...
- python全栈学习 day02
pycharm 安装设置: 按照百度百科或者官网介绍下载,安装. 激活步骤 1:改host 2.输入激活信息,注意有效期. python 逻辑运算符://返回的均为bool值 与 and A and ...
- 嵊州D6T2 城市 city
城市 city [问题描述] 众所周知,why 是czyz 王国的国王. czyz 王国一共有n 个城市,每个城市都有一条道路连向一个城市(可能连向这个城市自己). 同时,对于每一个城市,也只有一条道 ...
- gulp常用插件之wiredep使用
更多gulp常用插件使用请访问:gulp常用插件汇总 wiredep这是一款gulp插件,能够将js.css文件自动插入到html中. 更多使用文档请点击访问wiredep工具官网. Bower是一个 ...
- Normalizing flows
probability VS likelihood: https://zhuanlan.zhihu.com/p/25768606 http://sdsy888.me/%E9%9A%8F%E7%AC%9 ...
- js - 除法
取整数 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2 ...