给定一个字符x和一个字符串。要求输出包含此字符的所有不同字串。

后缀数组可以计算一个字符串的所有不同字串,理解了原理就能做这题了。

对于每一个后缀i,将产生len-sa[i]-hight[i]的前缀,累加起来就是所有不同字串。这里要求字串必须包含x

也就是对于每一个后缀i,要减去不含x的前缀。

保存每一个x的位置,用lower_bound查询在此后缀中的第一个x,然后之前的都不计算到贡献当中。

dc3敲起来太慢了

 #include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
//using namespace std; const int maxn = 1e5+;
#define F(x) ((x)/3+((x)%3 == 1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[maxn*],wb[maxn*],wv[maxn*],wss[maxn*]; int c0(int *r,int a,int b)
{
return r[a] == r[b] && r[a+] == r[b+] && r[a+] == r[b+];
}
int c12(int k,int *r,int a,int b)
{
if(k == )
return r[a] < r[b] ||(r[a] == r[b] && c12(,r,a+,b+));
else
return r[a] < r[b] ||(r[a] == r[b] && wv[a+] < wv[b+]);
}
void sort(int *r,int *a,int *b,int n,int m)
{
int i;
for(i=;i<n;i++) wv[i] = r[a[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--)b[--wss[wv[i]] ] = a[i];
}
void dc3(int *r,int *_sa,int n,int m)
{
int i,j,*rn = r+n;
int *san = _sa+n,ta = ,tb=(n+)/,tbc=,p;
r[n] = r[n+] = ;
for(i=;i<n;i++) if(i% != ) wa[tbc++] = i;
sort(r+,wa,wb,tbc,m);
sort(r+,wb,wa,tbc,m);
sort(r,wa,wb,tbc,m);
for(p=,rn[F(wb[])] = ,i=;i<tbc;i++)
rn[F(wb[i])] = c0(r,wb[i-],wb[i]) ? p-:p++;
if(p < tbc) dc3(rn,san,tbc,p);
else for(i=;i<tbc;i++) san[rn[i]] = i;
for(i=;i<tbc;i++) if(san[i]<tb) wb[ta++] = san[i]*;
if(n%==) wb[ta++] = n-;
sort(r,wb,wa,ta,m);
for(i=;i<tbc;i++) wv[wb[i]=G(san[i]) ] = i;
for(i=,j=,p=;i<ta&&j<tbc;p++)
_sa[p] = c12(wb[j]%,r,wa[i],wb[j]) ? wa[i++]:wb[j++];
for(;i<ta;p++) _sa[p] = wa[i++];
for(;j<tbc;p++) _sa[p] = wb[j++];
}
void da(int str[],int _sa[],int _rank[],int _height[],int n,int m)
{
for(int i=n;i<n*;i++) str[i] = ;
dc3(str,_sa,n+,m);
int i,j,k = ;
for(i=;i<=n;i++) _rank[_sa[i]] = i;
for(i=;i<n;i++)
{
if(k) k--;
j = _sa[_rank[i]- ];
while(str[i+k] == str[j+k]) k++;
_height[_rank[i]] = k;
}
} int T;
int sa[*maxn],hehe[*maxn],rank[maxn],height[maxn];
int x;
char line[*maxn]; void debug(int n)
{
for(int i=;i<=n;i++)
{
printf("sa:%d rank:%d height:%d\n",sa[i],rank[i],height[i]);
}
} int main()
{
//freopen("input.txt","r",stdin);
scanf("%d ",&T);
int cas = ;
while(T--)
{
scanf(" %c %s",&x,line);
int len = strlen(line);
for(int i=;i<len;i++) hehe[i] = line[i];
int ch[maxn],cnt = ;
for(int i=;i<len;i++)
{
if(line[i] == x) ch[cnt++] = i;
}
ch[cnt++] = len;
da(hehe,sa,rank,height,len,);
//debug(len);
LL ans = ;
for(int i=;i<=len;i++)
{
int pos = ch[std::lower_bound(ch,ch+cnt,sa[i]) - ch] - sa[i];
ans += len -sa[i] ;
ans -= std::max(pos,height[i]);
}
printf("Case #%d: %I64d\n",++cas,ans);
}
}

HDU5769-Substring-多校#4-1006-后缀数组的更多相关文章

  1. Lexicographical Substring Search SPOJ - SUBLEX (后缀数组)

    Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...

  2. HDU 4691 Front compression (2013多校9 1006题 后缀数组)

    Front compression Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Othe ...

  3. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  4. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

  5. HDU 4691(多校第九场1006) 后缀数组

    ...还能多说什么. 眼角一滴翔滑过. 一直以为题意是当前串与所有之前输入的串的LCP...然后就T了一整场. 扫了一眼标程突然发现他只比较输入的串和上一个串? 我心中突然有千万匹草泥马踏过. 然后随 ...

  6. 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)

    链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...

  7. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  8. HDU 5769 Substring 后缀数组

    Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...

  9. HDU 5679 Substring 后缀数组判重

    题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...

  10. csu 1305 Substring (后缀数组)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1305 1305: Substring Time Limit: 2 Sec  Memory Limi ...

随机推荐

  1. A2D JS框架 - DES加密解密 与 Cookie的封装(C#与js互相加密解密)

    这次实现了JS端的DES加密与解密,并且C#端也能正确解析DES的密文(反之也实现了) 使用的代码如下,非常方便: <script src="A2D.js" type=&qu ...

  2. 三、临时弹出一个QQ对话窗口

    第一种:需要添加好友才可以访问 <a href="http://wpa.qq.com/msgrd?v=3&uin=317985559&site=qq&menu= ...

  3. Python编码与变量

    (一)Python执行的方式 Window: 在CMD里面,使用 Python + 相对路径/绝对路径 在解释器里面,直接输入,一行代码一行代码的解释 Linux: 明确地指出用Python解释器来执 ...

  4. openstack-KVM安装与使用

    一.KVM安装 1.安装条件 VT-x BIOS Intel9R) Virtualization Tech [Enabled] cat /proc/cpuinfo | grep -e vmx -e n ...

  5. windows中在vs code终端使用bash

    vs code Visual Studio Code的缩写,很好用的一个编辑器. 终端 vs code的终端(命令行)在windows中,默认使用的是powershell,如下图所示: powersh ...

  6. Mysql drop function xxxx ERROR 1305 (42000): FUNCTION (UDF) xxxx does not exist

    mysql> drop function GetEmployeeInformationByID;ERROR 1305 (42000): FUNCTION (UDF) GetEmployeeInf ...

  7. 如何在 Linux 中查找最大的 10 个文件

    https://linux.cn/article-9495-1.html

  8. Jenkins Installing and migration

    JAVA_Zookeeper_hadoop - CSDN博客https://blog.csdn.net/wangmuming Installing Jenkins on Red Hat distrib ...

  9. java随笔5 完整路径的应用

    不仅类,函数,甚至参数都可以获取完整路径

  10. 补充:pyhton 2 和3中的beyts类型

    在python2里,bytes == str python2里还有个单独的类型是unicode , 把字符串解码后,就会变成unicode. 既然python2中byets == str,那为什么还要 ...