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

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

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

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

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

dc3敲起来太慢了

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #define LL long long
  5. //using namespace std;
  6.  
  7. const int maxn = 1e5+;
  8. #define F(x) ((x)/3+((x)%3 == 1?0:tb))
  9. #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
  10. int wa[maxn*],wb[maxn*],wv[maxn*],wss[maxn*];
  11.  
  12. int c0(int *r,int a,int b)
  13. {
  14. return r[a] == r[b] && r[a+] == r[b+] && r[a+] == r[b+];
  15. }
  16. int c12(int k,int *r,int a,int b)
  17. {
  18. if(k == )
  19. return r[a] < r[b] ||(r[a] == r[b] && c12(,r,a+,b+));
  20. else
  21. return r[a] < r[b] ||(r[a] == r[b] && wv[a+] < wv[b+]);
  22. }
  23. void sort(int *r,int *a,int *b,int n,int m)
  24. {
  25. int i;
  26. for(i=;i<n;i++) wv[i] = r[a[i]];
  27. for(i=;i<m;i++) wss[i] = ;
  28. for(i=;i<n;i++) wss[wv[i]]++;
  29. for(i=;i<m;i++) wss[i] += wss[i-];
  30. for(i=n-;i>=;i--)b[--wss[wv[i]] ] = a[i];
  31. }
  32. void dc3(int *r,int *_sa,int n,int m)
  33. {
  34. int i,j,*rn = r+n;
  35. int *san = _sa+n,ta = ,tb=(n+)/,tbc=,p;
  36. r[n] = r[n+] = ;
  37. for(i=;i<n;i++) if(i% != ) wa[tbc++] = i;
  38. sort(r+,wa,wb,tbc,m);
  39. sort(r+,wb,wa,tbc,m);
  40. sort(r,wa,wb,tbc,m);
  41. for(p=,rn[F(wb[])] = ,i=;i<tbc;i++)
  42. rn[F(wb[i])] = c0(r,wb[i-],wb[i]) ? p-:p++;
  43. if(p < tbc) dc3(rn,san,tbc,p);
  44. else for(i=;i<tbc;i++) san[rn[i]] = i;
  45. for(i=;i<tbc;i++) if(san[i]<tb) wb[ta++] = san[i]*;
  46. if(n%==) wb[ta++] = n-;
  47. sort(r,wb,wa,ta,m);
  48. for(i=;i<tbc;i++) wv[wb[i]=G(san[i]) ] = i;
  49. for(i=,j=,p=;i<ta&&j<tbc;p++)
  50. _sa[p] = c12(wb[j]%,r,wa[i],wb[j]) ? wa[i++]:wb[j++];
  51. for(;i<ta;p++) _sa[p] = wa[i++];
  52. for(;j<tbc;p++) _sa[p] = wb[j++];
  53. }
  54. void da(int str[],int _sa[],int _rank[],int _height[],int n,int m)
  55. {
  56. for(int i=n;i<n*;i++) str[i] = ;
  57. dc3(str,_sa,n+,m);
  58. int i,j,k = ;
  59. for(i=;i<=n;i++) _rank[_sa[i]] = i;
  60. for(i=;i<n;i++)
  61. {
  62. if(k) k--;
  63. j = _sa[_rank[i]- ];
  64. while(str[i+k] == str[j+k]) k++;
  65. _height[_rank[i]] = k;
  66. }
  67. }
  68.  
  69. int T;
  70. int sa[*maxn],hehe[*maxn],rank[maxn],height[maxn];
  71. int x;
  72. char line[*maxn];
  73.  
  74. void debug(int n)
  75. {
  76. for(int i=;i<=n;i++)
  77. {
  78. printf("sa:%d rank:%d height:%d\n",sa[i],rank[i],height[i]);
  79. }
  80. }
  81.  
  82. int main()
  83. {
  84. //freopen("input.txt","r",stdin);
  85. scanf("%d ",&T);
  86. int cas = ;
  87. while(T--)
  88. {
  89. scanf(" %c %s",&x,line);
  90. int len = strlen(line);
  91. for(int i=;i<len;i++) hehe[i] = line[i];
  92. int ch[maxn],cnt = ;
  93. for(int i=;i<len;i++)
  94. {
  95. if(line[i] == x) ch[cnt++] = i;
  96. }
  97. ch[cnt++] = len;
  98. da(hehe,sa,rank,height,len,);
  99. //debug(len);
  100. LL ans = ;
  101. for(int i=;i<=len;i++)
  102. {
  103. int pos = ch[std::lower_bound(ch,ch+cnt,sa[i]) - ch] - sa[i];
  104. ans += len -sa[i] ;
  105. ans -= std::max(pos,height[i]);
  106. }
  107. printf("Case #%d: %I64d\n",++cas,ans);
  108. }
  109. }

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. 面试 10:玩转 Java 选择和插入排序,附冒泡最终源码

    昨天给大家讲解了 Java 玩转冒泡排序,大家一定觉得并没有什么难度吧,不知道大佬们玩转了吗?不知道大家有没有多加思考,实际上在我们最后的一种思路上,还可以再继续改进. 我们先看看昨天最终版本的代码. ...

  2. SoftWater——SDN+UnderWater系列论文一

    ---- SoftWater: Software-defined networking for next-generation underwater communication systems 来源: ...

  3. Survey项目总结

    1.Ioc深入理解 Inverse of control org.springframework.scheduling.quartz.SchedulerFactoryBean org.mybatis. ...

  4. Django 生成验证码或二维码 pillow模块

    一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用.   PIL模块只支持到Python 2 ...

  5. Django之事务

    Django之事务 事务就是将一组操作捆绑在一起,只有当这一组操作全部都成功以后这个事务才算成功;当这组操作中有任何一个没有操作成功,则这个操作就会回滚,回到操作之前的状态. 其中牵扯到向数据库中写数 ...

  6. 我的第一个Go web程序 纪念一下

    参考Go web编程,很简单的程序: 大致的步骤: 绑定ip和端口 绑定对应的处理器或者处理器函数,有下面两种选择,选择一种即可监听ip及端口 处理器: 定义一个struct结构体 然后让这个结构体实 ...

  7. HDU 1089 到1096 a+b的输入输出练习

    http://acm.hdu.edu.cn/showproblem.php?pid=1089 Problem Description Your task is to Calculate a + b.T ...

  8. chrome extensions notifications

    developer.chrome.comhttps://developer.chrome.com/extensions/notifications notification | MDNhttps:// ...

  9. Windows10下安装VMware虚拟机并搭建CentOS系统环境

    转载: http://blog.51cto.com/10085711/2069270 操作系统 Windows 10专业版(64位) VMware虚拟机 产品:VMware® Workstation ...

  10. 使用fetch代替ajax请求 post传递方式

    let postData = {a:'b'}; fetch('http://data.xxx.com/Admin/Login/login', { method: 'POST', mode: 'cors ...