HDU5769-Substring-多校#4-1006-后缀数组
给定一个字符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-后缀数组的更多相关文章
- Lexicographical Substring Search SPOJ - SUBLEX (后缀数组)
Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...
- HDU 4691 Front compression (2013多校9 1006题 后缀数组)
Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Othe ...
- 2016暑假多校联合---Substring(后缀数组)
2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...
- 2016多校联合训练4 F - Substring 后缀数组
Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...
- HDU 4691(多校第九场1006) 后缀数组
...还能多说什么. 眼角一滴翔滑过. 一直以为题意是当前串与所有之前输入的串的LCP...然后就T了一整场. 扫了一眼标程突然发现他只比较输入的串和上一个串? 我心中突然有千万匹草泥马踏过. 然后随 ...
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- HDU 5769 Substring 后缀数组
Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- csu 1305 Substring (后缀数组)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1305 1305: Substring Time Limit: 2 Sec Memory Limi ...
随机推荐
- 面试 10:玩转 Java 选择和插入排序,附冒泡最终源码
昨天给大家讲解了 Java 玩转冒泡排序,大家一定觉得并没有什么难度吧,不知道大佬们玩转了吗?不知道大家有没有多加思考,实际上在我们最后的一种思路上,还可以再继续改进. 我们先看看昨天最终版本的代码. ...
- SoftWater——SDN+UnderWater系列论文一
---- SoftWater: Software-defined networking for next-generation underwater communication systems 来源: ...
- Survey项目总结
1.Ioc深入理解 Inverse of control org.springframework.scheduling.quartz.SchedulerFactoryBean org.mybatis. ...
- Django 生成验证码或二维码 pillow模块
一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用. PIL模块只支持到Python 2 ...
- Django之事务
Django之事务 事务就是将一组操作捆绑在一起,只有当这一组操作全部都成功以后这个事务才算成功;当这组操作中有任何一个没有操作成功,则这个操作就会回滚,回到操作之前的状态. 其中牵扯到向数据库中写数 ...
- 我的第一个Go web程序 纪念一下
参考Go web编程,很简单的程序: 大致的步骤: 绑定ip和端口 绑定对应的处理器或者处理器函数,有下面两种选择,选择一种即可监听ip及端口 处理器: 定义一个struct结构体 然后让这个结构体实 ...
- HDU 1089 到1096 a+b的输入输出练习
http://acm.hdu.edu.cn/showproblem.php?pid=1089 Problem Description Your task is to Calculate a + b.T ...
- chrome extensions notifications
developer.chrome.comhttps://developer.chrome.com/extensions/notifications notification | MDNhttps:// ...
- Windows10下安装VMware虚拟机并搭建CentOS系统环境
转载: http://blog.51cto.com/10085711/2069270 操作系统 Windows 10专业版(64位) VMware虚拟机 产品:VMware® Workstation ...
- 使用fetch代替ajax请求 post传递方式
let postData = {a:'b'}; fetch('http://data.xxx.com/Admin/Login/login', { method: 'POST', mode: 'cors ...