1. /*
  2. 统计每个节点的max和min, 然后求和即可
  3. min = max[fa] + 1
  4. */
  5. #include<cstdio>
  6. #include<algorithm>
  7. #include<iostream>
  8. #include<cstring>
  9. #include<queue>
  10. #define ll long long
  11. #define M 6010
  12. #define mmp make_pair
  13. using namespace std;
  14. int read()
  15. {
  16. int nm = 0, f = 1;
  17. char c = getchar();
  18. for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
  19. for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
  20. return nm * f;
  21. }
  22. int t, ch[M][26], len[M], fa[M], cnt, lst;
  23. char s[M];
  24. void insert(int c)
  25. {
  26. int p = ++cnt, f = lst;
  27. len[p] = len[f] + 1;
  28. lst = p;
  29. while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
  30. if(!f)
  31. {
  32. fa[p] = 1;
  33. return;
  34. }
  35. int q = ch[f][c], nq = ++cnt;
  36. if(len[f] + 1 == len[q])
  37. {
  38. fa[p] = q;
  39. cnt--;
  40. return;
  41. }
  42. len[nq] = len[f] + 1;
  43. fa[nq] = fa[q];
  44. fa[q] = fa[p] = nq;
  45. memcpy(ch[nq], ch[q], sizeof(ch[nq]));
  46. while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
  47. }
  48. void init()
  49. {
  50. cnt = lst = 1;
  51. memset(ch, 0, sizeof(ch));
  52. memset(len, 0, sizeof(len));
  53. memset(fa, 0, sizeof(fa));
  54. }
  55. int main()
  56. {
  57. t = read();
  58. while(t--)
  59. {
  60. init();
  61. scanf("%s", s + 1);
  62. int l = strlen(s + 1);
  63. for(int i = 1; i <= l; i++) insert(s[i] - 'A');
  64. int ans = 0;
  65. for(int i = 1; i <= cnt; i++) ans += len[i] - len[fa[i]];
  66. cout << ans << "\n";
  67. }
  68. return 0;
  69. }

SP694 DISUBSTR - Distinct Substrings的更多相关文章

  1. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  2. spoj694 DISUBSTR - Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  3. SPOJ - DISUBSTR Distinct Substrings (后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  4. SPOJ DISUBSTR Distinct Substrings 后缀数组

    题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...

  5. SPOJ 694 DISUBSTR - Distinct Substrings

    思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...

  6. SPOJ 694&&SPOJ705: Distinct Substrings

    DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...

  7. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  8. SPOJ - Distinct Substrings,求不同的字串个数!

    DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...

  9. Distinct Substrings SPOJ - DISUBSTR 后缀数组

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

随机推荐

  1. 在浏览器中输入url回车之后会发生什么

    在浏览器中输入URL之后,浏览器会经历以下5个步骤: 1.解析URL 2.DNS域名解析 3.浏览器与网站建立TCP链接(三次握手) 4.请求和传输数据 5.浏览器渲染页面 一.解析URL 什么是ur ...

  2. VARCHAR(N)类型,utf8编码,则N最大值为多少,n表示什么?

    4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无论存放的是数字.字母还是UTF8 ...

  3. centos7 pptp 安装

    1  安装 ppp yum install -y ppp 2 安装 pptpd yum install -y pptpd 3 编辑/etc/pptpd.conf 在最后 添加 localip 192. ...

  4. CenterOS下安装NodeJS

    1. 首先下载NodeJS的版本 wget https://nodejs.org/dist/v8.1.3/node-v8.1.3-linux-x64.tar.gz 我这里下载的是v8.1.3 版本 n ...

  5. 使用Vivado的block design

    使用Vivado的block design (1)调用ZYNQ7 Processing System (2)配置ZYNQ7系统 (3)外设端口配置 根据开发板原理图MIO48和MIO49配置成了串口通 ...

  6. XE5 Android 开发实现手机打电话和发短信 [转]

    其实都可以通过intent和URI调用系统功能.Windows程序员可以理解成是ShellExecute.这个是万金油.可以有调用各种功能.后面会介绍. 1.短信息.很简单 方法a.不使用Intent ...

  7. Azure PowerShell (15) 批量导出Azure ASM/ARM VM信息

    <Windows Azure Platform 系列文章目录> 客户又提出新的需求,需要知道所有订阅下的虚拟机数量.运行情况等信息. 我花了点时间,写了一个PowerShell脚本,发布到 ...

  8. java经典5种 FlowLayout 、BorderLayout、GridLayout、GridBagLayout、CardLayout布局

    Java 程序通过jvm可以很好的移植到其他平台上,但是java 生成的图形界面样式,在不使用布局的情况下,往往需要重新设定大小,才能在新的平台上调整到最佳样式.这是由于组件的最佳大小 往往是与平台相 ...

  9. 黄聪:FFmpeg 使用指南

    1. ffmpeg 是什么? ffmpeg(命令行工具) 是一个快速的音视频转换工具. 2. ffmpeg 的使用方式 命令行: ffmpeg [全局选项] {[输入文件选项] -i ‘输入文件’} ...

  10. 解决下载经过GZip压缩后的网页乱码问题

    目前很多网站默认采用GZip压缩,如果不进行解压缩,下载后生成的html页面打开后会出现中文乱码 乱码前: string url = "http://quote.eastmoney.com/ ...