SETI--后缀数组
来源:互联网 发布:国外网络手游 编辑:程序博客网 时间:2024/06/10 19:54
Problem Description
Amateur astronomers Tom and Bob try to find radio broadcasts of extraterrestrial civilizations in the air. Recently they received some strange signal and represented it as a word consisting of small letters of the English alphabet. Now they wish to decode the signal. But they do not know what to start with.
They think that the extraterrestrial message consists of words, but they cannot identify them. Tom and Bob call a subword of the message a potential word if it has at least two non-overlapping occurrences in the message.
For example, if the message is “abacabacaba”, “abac” is a potential word, but “acaba” is not because two of its occurrences overlap.
Given a message m help Tom and Bob to find the number of potential words in it.
Input
Output
Sample Input
abacabacaba
Sample Output
15
思路:后缀数组搞。len表示之前最长的已经贡献种数的长度。如果当前height[i]<len。那么是不能贡献新串的,这时更新len。如果height[i]>len,用min(abs(sa[i]-sa[i-1]),height[i])尝试去更新len。注意还得往前面跑,虽然往前面跑LCP肯定是减少,但是sa[i]-sa[pos]可能变大。导致min()变大。#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <iomanip>#include <cassert>using namespace std;#pragma comment(linker, "/stack:1024000000,1024000000")#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i>=(m);i--)#define travel(e, u) for(int e = u, v = vv[u]; e; e = nxt[e], v = vv[e])#define bit(n) (1LL<<(n))#define And(a, b) ((a) & (b))#define Xor(a, b) ((a) ^ (b))#define Or(a, b) ((a) | (b))#define mid ((l+r)/2)#define ls (pos<<1)#define rs (ls|1)#define lson l, m, ls#define rson m+1, r, rstypedef long long LL;typedef unsigned long long ULL;void work();int main(){#ifdef ACM freopen("in.txt", "r", stdin);#endif // ACM work();} void scanf(int & x, char c = 0){ while((c = getchar()) < '0' || c > '9'); x = c - '0'; while((c = getchar()) >= '0' && c <= '9') x = x * 10 + (c - '0');} #define rep(i,a,b) for(int i = (a);i<(b);++i)#define rrep(i,a,b) for(int i = (a);i>=(b);--i)#define clr(a,x) memset(a,x,sizeof(a))/*****************************************华丽分割线**********************************************/#define maxn 20008int str[maxn],vis[maxn];char s[maxn];int sa[maxn],t[maxn],t2[maxn],c[maxn],key[maxn];int height[maxn],Rank[maxn];void build_sa(int * s,int n,int m){ int i,*x = t,*y = t2; for(i = 0;i < m;i++) c[i] = 0; for(i = 0;i < n;i++) c[ x[i] = s[i] ]++; for(i = 1;i < m;i++) c[i] += c[i-1]; for(i = n-1;i >= 0;i--) sa[--c[x[i]]] = i; for(int k = 1;k <= n;k <<= 1) { int p = 0; for(i = n - k;i < n;i++) y[p++] = i; for(i = 0;i < n;i++) if(sa[i] >= k) y[p++] = sa[i] - k; for(i = 0;i < m;i++) c[i] = 0; for(i = 0;i < n;i++) c[ x[y[i]] ]++; for(i = 0;i < m;i++) c[i] += c[i-1]; for(i = n-1;i >= 0;i--) sa[--c[x[y[i]]]] = y[i]; //根据sa和y数组计算新的数y组 swap(x,y); p = 1; x[sa[0]] = 0; for(i = 1;i < n;i++) x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1] + k] == y[sa[i] + k] ? p-1:p++; if(p >= n) break; m = p; }} void getHeight(int * s,int n){ int i,j,k = 0; for(i = 0;i < n;i++) Rank[sa[i]] = i; for(i = 0;i < n;i++) { if(k) k--; int j = sa[Rank[i]-1]; while(s[i+k] == s[j+k]) k++; height[Rank[i]] = k; }} LL gao(int n){ LL ans = 0; int len = 0; for(int i = 1;i < n;i++) { if(height[i] > len) { int pos = i-1,fuck = height[i],lcp = height[i]; while(1) { fuck = min(abs(sa[i]-sa[pos]),lcp); if(lcp > len) { if(fuck > len) { ans += fuck - len; len = fuck; } pos--; if(pos == 0) break; } else break; lcp = min(lcp,height[pos+1]); } } else len = height[i]; } return ans;}void work(){ while(scanf("%s",s)!=EOF) { int len = strlen(s); for(int i = 0;i < len;i++) str[i] = s[i] - 'a' + 2; str[len] = 0; build_sa(str,len+1,30); getHeight(str,len+1); LL ans = gao(len+1); printf("%lld\n",ans); }}
- SETI--后缀数组
- Acdream 1430 SETI(后缀数组)
- acdream OJ 1430 SETI 后缀数组
- acdream 1430 SETI 后缀数组+height分组
- ACdream 1430 SETI 后缀自动机/后缀数组 不重叠子串的个数
- ACdream 1430 SETI 后缀自动机
- 后缀树/后缀数组
- 后缀树 后缀数组
- 【后缀数组】后缀排序
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 后缀数组
- 在service中的BroadcasReceiver遇到no empty constructor错误
- 解决dedecms的php.ini register_globals must is Off!
- HTC T328W刷机包 仿三星S5 UI美化 精简 S5下拉
- 链表 总结
- 笔试题目解答
- SETI--后缀数组
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
- 深入理解 HashMap 加载因子 loadFactor
- 通用基础的CSS样式
- 【环境配置】Popush工程在Linux下的配置
- Android 接收蓝牙耳机按键操作
- 缺陷管理系统相关技术文章收集
- mysql使用
- 【iOS开发-31】UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式