本文主要是介绍UVA 10829 - L-Gap Substrings(后缀数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UVA 10829 - L-Gap Substrings
题目链接
题意:一个字符串如果形如UGU,的形式,G的长度为L,那么称这个字符串为L串,给定一个字符串,问这个字符串子串为g串的个数
思路:做这题前先做了POJ3693,有一个思想就是枚举长度分段,这样的话对于一个U长度为l的而言,只要在当前位置和当前位置之后(l + g)的位置分别向前向后找lcp,两个lcp加起来的长度减去l就是可以可以的种数,累加起来就是答案
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;typedef long long ll;const int MAXLEN = 100005;struct Suffix {int s[MAXLEN];int sa[MAXLEN], t[MAXLEN], t2[MAXLEN], c[MAXLEN], n;int rank[MAXLEN], height[MAXLEN];int best[MAXLEN][20];int g, len;char str[MAXLEN];void build_sa(int m) {n++;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];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;}n--;}void getHeight() {int i, j, k = 0;for (i = 1; 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;}}void initRMQ() {for (int i = 1; i <= n; i++) best[i][0] = height[i];for (int j = 1; (1<<j) <= n; j++)for (int i = 1; i + (1<<j) - 1 <= n; i++)best[i][j] = min(best[i][j - 1], best[i + (1<<(j - 1))][j - 1]);}int lcp(int L, int R) {L = rank[L]; R = rank[R];if (L > R) swap(L, R);L++;int k = 0;while ((1<<(k + 1)) <= R - L + 1) k++;return min(best[L][k], best[R - (1<<k) + 1][k]);}void init() {n = 0;scanf("%d%s", &g, str);len = strlen(str);for (int i = 0; i < len; i++)s[n++] = str[i] - 'a' + 1;s[n++] = 27;for (int i = len - 1; i >= 0; i--)s[n++] = str[i] - 'a' + 1;s[n] = 0;}ll solve() {init();build_sa(28);getHeight();initRMQ();ll ans = 0;for (int d = 1; d < len / 2; d++) {for (int j = 0; j < len; j += d) {int l = j, r = j + d + g, sum = 0;if (r < len) sum += min(lcp(l, r), d);if (l >= 1) sum += min(lcp(n - l, n - r), d - 1);ans += max(0, sum - d + 1);}}return ans;}} gao;int t;int main() {int cas = 0;scanf("%d", &t);while(t--) {printf("Case %d: %lld\n", ++cas, gao.solve());}return 0;
}
这篇关于UVA 10829 - L-Gap Substrings(后缀数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!