• 【BZOJ 4650】【UOJ #219】【NOI 2016】优秀的拆分


    http://www.lydsy.com/JudgeOnline/problem.php?id=4650
    http://uoj.ac/problem/219
    这里有非常好的题解qwq
    接着道题复习一下模板(还是打错了好多地方_(:з」∠)_)
    以及后缀数组面对多组数据清数组的正确打开方式qwq
    因为求height数组时依赖n之后的字符都为空,倍增算法利用sa计算第一关键字时也依赖n之后的关键字都为空。
    所以应至少在字符串第n+1位清空来终止可能会越界的求height数组的函数。还要把存第一关键字和第二关键字下标的数组都清空qwq
    还是这种设关键点+差分的(O(nln n))的方法也是一个经典模板。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 30013;
    
    int t1[N], t2[N], c[N];
    
    void st(int *x, int *y, int *sa, int n, int m) {
    	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];
    }
    
    void mkhz(int *r, int *sa, int n, int m) {
    	memset(t1, 0, sizeof(t1));
    	memset(t2, 0, sizeof(t2));
    	int *x = t1, *y = t2, *t, i, j, p;
    	for (i = 0; i < n; ++i) x[i] = r[i], y[i] = i;
    	st(x, y, sa, n, m);
    	for (j = 1, p = 1; j < n && p < n; j <<= 1, m = p - 1) {
    		for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
    		for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
    		st(x, y, sa, n, m);
    		for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, 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++;
    	}
    }
    
    void mkh(int *r, int *sa, int *rank, int *h, int n) {
    	h[0] = 0; int k = 0, j;
    	for (int i = 0; i < n; ++i) rank[sa[i]] = i;
    	for (int i = 1; i < n; h[rank[i++]] = k)
    		for (k ? --k : k = 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
    }
    
    void pref(int *h, int f[N][16], int n) {
    	for (int i = 1; i < n; ++i) f[i][0] = h[i];
    	for (int j = 1; (1 << j) < n; ++j)
    		for (int i = (1 << j); i < n; ++i)
    			f[i][j] = min(f[i][j - 1], f[i - (1 << (j - 1))][j - 1]);
    }
    
    char s1[N], s2[N];
    int r1[N], r2[N], f1[N][16], f2[N][16], sa1[N], sa2[N], h1[N], h2[N];
    int Log_2[N], rank1[N], rank2[N], n, sum_pre[N], sum_suf[N];
    
    int lcp(int i, int j) {
    	i = rank1[i]; j = rank1[j];
    	if (i > j) i ^= j ^= i ^= j;
    	int l = Log_2[j - i];
    	return min(f1[j][l], f1[i + (1 << l)][l]);
    }
    
    int lcs(int i, int j) {
    	i = rank2[n - i + 1]; j = rank2[n - j + 1];
    	if (i > j) i ^= j ^= i ^= j;
    	int l = Log_2[j - i];
    	return min(f2[j][l], f2[i + (1 << l)][l]);
    }
    
    int main() {
    	int cnt = 0;
    	for (int i = 1; i <= 30000; ++i) {
    		if ((1 << (cnt + 1)) <= i) ++cnt;
    		Log_2[i] = cnt;
    	}
    	int T; scanf("%d", &T);
    	while (T--) {
    		scanf("%s", s1 + 1);
    		n = strlen(s1 + 1);
    		for (int i = 1, j = n; i <= n; ++i, --j)
    			r2[j] = r1[i] = s1[i] - 'a' + 1;
    		r1[n + 1] = r2[n + 1] = 0;
    		mkhz(r1, sa1, n + 1, 26);
    		mkh(r1, sa1, rank1, h1, n + 1);
    		mkhz(r2, sa2, n + 1, 26);
    		mkh(r2, sa2, rank2, h2, n + 1);
    		pref(h1, f1, n + 1);
    		pref(h2, f2, n + 1);
    		
    		memset(sum_pre, 0, sizeof(int) * (n + 1));
    		memset(sum_suf, 0, sizeof(int) * (n + 1));
    		int x, y, l, r;
    		for (int len = 1; (len << 1) <= n; ++len)
    			for (int i = 1, j; (j = i + len) <= n; i = j) {
    				x = lcs(i, j);
    				y = lcp(i, j);
    				if (x + y - 1 >= len) {
    					++sum_pre[max(j, j + len - x)];
    					--sum_pre[min(j + len, j + y)];
    					++sum_suf[max(i - len + 1, i - x + 1)];
    					--sum_suf[min(i + 1, i + y - len + 1)];
    				}
    			}
    		
    		for (int i = 1; i <= n; ++i) {
    			sum_pre[i] += sum_pre[i - 1];
    			sum_suf[i] += sum_suf[i - 1];
    		}
    		
    		ll ans = 0;
    		for (int i = 2; i <= n; ++i)
    			ans += 1ll * sum_pre[i - 1] * sum_suf[i];
    		printf("%lld
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    ReSharper.8.0.14.856注册码
    asp.net 301重定向代码
    WebResource.axd 404 错误
    【原创】asp.net导出word 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a
    C#操作word或excel及水晶报表,检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005
    Repository 设计模式介绍
    将15位身份证转换成18位
    清理数据库木马文件
    Oracle的创建表空间及用户
    使用python脚本执行地理处理工具
  • 原文地址:https://www.cnblogs.com/abclzr/p/6294386.html
Copyright © 2020-2023  润新知