标签:swap linker 后缀数组 长度 link pac lib open air
题意:给定上一个串,问你多少种UVU这一种形式的串,其中U不为空并且V的长度给定了。
析:枚举 U 的长度L,那么U一定是经过 0 L 2L 3L .... 其中的一个,所以求两个长度反lcp,一个向前延伸lcp1,一个向后延伸lcp2,然后加起来,要保证每个都不超过L,否则就会重复,然后个数就是 lcp1+lcp2-L。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 10; const int maxm = 1e6 + 10; const int mod = 50007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } struct Suffix_array{ int s[maxn], sa[maxn], t[maxn], t2[maxn]; int c[maxn], h[maxn], r[maxn]; int n; int dp[maxn][20]; void init(){ n = 0; ms(sa, 0); } void build_sa(int m){ int *x = t, *y = t2; for(int i = 0; i < m; ++i) c[i] = 0; for(int i = 0; i < n; ++i) ++c[x[i] = s[i]]; for(int i = 1; i < m; ++i) c[i] += c[i-1]; for(int i = n-1; i >= 0; --i) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1){ int p = 0; for(int i = n-k; i < n; ++i) y[p++] = i; for(int i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k; for(int i = 0; i < m; ++i) c[i] = 0; for(int i = 0; i < n; ++i) ++c[x[y[i]]]; for(int i = 1; i < m; ++i) c[i] += c[i-1]; for(int i = n-1; i >= 0; --i) sa[--c[x[y[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]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p - 1 : p++; if(p >= n) break; m = p; } } void getHight(){ int k = 0; for(int i = 0; i < n; ++i) r[sa[i]] = i; for(int i = 0; i < n; ++i){ if(k) --k; int j = sa[r[i]-1]; while(s[i+k] == s[j+k]) ++k; h[r[i]] = k; } } void rmq_init(){ for(int i = 1; i <= n; ++i) dp[i][0] = h[i]; for(int j = 1; (1<<j) <= n; ++j) for(int i = 1; i + (1<<j) <= n; ++i) dp[i][j] = min(dp[i][j-1], dp[i+(1<<j-1)][j-1]); } int query(int L, int R){ L = r[L]; R = r[R]; if(L > R) swap(L, R); ++L; int k = log(R - L + 1.0) / log(2.0); return min(dp[L][k], dp[R-(1<<k)+1][k]); } LL solve(int len, int g){ LL ans = 0; for(int i = 1; i < len; ++i){ for(int j = 0; j + i + g < len; j += i){ int lcp = min(i, query(j, j+i+g)) + min(query(n-1-j, n-1-j-i-g), i-1); ans += max(0, lcp - i + 1); } } return ans; } }; Suffix_array arr; char s[maxn]; int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ scanf("%d", &n); arr.init(); scanf("%s", s); int len = strlen(s); for(int i = 0; i < len; ++i) arr.s[arr.n++] = s[i] - ‘a‘ + 1; arr.s[arr.n++] = 27; for(int i = len-1; i >= 0; --i) arr.s[arr.n++] = s[i] - ‘a‘ + 1; arr.s[arr.n++] = 0; arr.build_sa(30); arr.getHight(); arr.rmq_init(); printf("Case %d: %lld\n", kase, arr.solve(len, n)); } return 0; }
UVa 10829 L-Gap Substrings (后缀数组+rmq)
标签:swap linker 后缀数组 长度 link pac lib open air
原文地址:http://www.cnblogs.com/dwtfukgv/p/7628752.html