【SPOJ-SUBST1】New Distinct Substrings【后缀数组】
2016-02-15 21:23
477 查看
和http://blog.csdn.net/braketbn/article/details/50649595一样
就是数据规模大了...但是还是照样过。
就是数据规模大了...但是还是照样过。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 50005, M = 300; int sa[maxn], rank[maxn], height[maxn]; int wa[maxn], wb[maxn], wv[maxn], cnt[maxn]; void SA(int *r, int n, int m) { int *x = wa, *y = wb; for(int i = 0; i < m; i++) cnt[i] = 0; for(int i = 0; i < n; i++) cnt[x[i] = r[i]]++; for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1]; for(int i = n - 1; i >= 0; i--) sa[--cnt[x[i]]] = i; for(int j = 1; j < n; j <<= 1) { int p = 0; for(int i = n - j; i < n; i++) y[p++] = i; for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j; for(int i = 0; i < n; i++) wv[i] = x[y[i]]; for(int i = 0; i < m; i++) cnt[i] = 0; for(int i = 0; i < n; i++) cnt[wv[i]]++; for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1]; for(int i = n - 1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for(int i = 1; i < n; i++) x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1 : p++; if(p >= n) break; m = p; } } void calcHeight(int *r, int n) { int i, j, k; for(i = j = k = 0; i < n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++); } int n, s[maxn]; char str[maxn]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", str); n = strlen(str); for(int i = 0; i < n; i++) s[i] = str[i]; s = 0; SA(s, n + 1, M); for(int i = 0; i <= n; i++) rank[sa[i]] = i; calcHeight(s, n); int ans = (LL)n * (n + 1) >> 1; for(int i = 1; i <= n; i++) ans -= height[i]; printf("%d\n", ans); } return 0; }
相关文章推荐
- 自编题——染色
- EJB--实体Bean
- Educational Codeforces Round 7总结
- MySQL中的级联约束
- MySQL将查询结果输出都爱服务器文件中
- UVA 1583 - Digit Generator
- DNS域名解析
- C++之路进阶——最小费用最大流(支线剧情)
- MySQL、SQLServer、Oracle中约束和索引的关系
- 5. Longest Palindromic Substring
- android studio从布局文件中提取style
- Spark基础知识学习分享
- MySQL-SQL Server、Oracle中的索引维护方式
- socket阻塞与非阻塞,同步与异步、I/O模型
- PHP:常用PHP数组操作函数
- sqlserver、oracle、mysql 锁类型
- 【转载】从 LinkedIn 的数据处理机制学习数据架构
- java 是值传递还是引用传递
- C++三大继承与多级派生
- Android - 利用内容提供者删除通话记录