zoukankan      html  css  js  c++  java
  • [SDOI2008]Sandy的卡片

    Description

    (Sandy)(Sue) 的热衷于收集干脆面中的卡片。

    然而,(Sue) 收集卡片是因为卡片上漂亮的人物形象,而 (Sandy) 则是为了积攒卡片兑换超炫的人物模型。

    每一张卡片都由一些数字进行标记,第 (i) 张卡片的序列长度为 (M_i),要想兑换人物模型,首先必须要集够 (N) 张卡片,对于这 (N) 张卡片,如果他们都有一个相同的子串长度为 (k),则可以兑换一个等级为 (k) 的人物模型。相同的定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串。

    (Sandy) 的卡片数远远小于要求的 (N),于是 (Sue) 决定在 (Sandy) 的生日将自己的卡片送给 (Sandy),在 (Sue) 的帮助下,(Sandy) 终于集够了N张卡片,但是,(Sandy) 并不清楚他可以兑换到哪个等级的人物模型,现在,请你帮助 (Sandy)(Sue),看看他们最高能够得到哪个等级的人物模型。

    Solution

    对每个字符串差分后直接求最长公共子串。

    Code

    #include <bits/stdc++.h>
    using namespace std;
    
    inline int ty() {
    	char ch = getchar(); int x = 0, f = 1;
    	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    	return x * f;
    }
    
    const int _  = 1000 + 10;
    const int __ = _ * _;
    int N, n, s[__], belong[__], rnk[__], sa[__], height[__];
    
    void SA() {
    	static int t[__], a[__], buc[__], fir[__], sec[__], tmp[__];
    	copy(s + 1, s + N + 1, t + 1);
    	sort(t + 1, t + N + 1);
    	int *end = unique(t + 1, t + N + 1);
    	for (int i = 1; i <= N; ++i) a[i] = lower_bound(t + 1, end, s[i]) - t;
    	fill(buc + 1, buc + N + 1, 0);
    	for (int i = 1; i <= N; ++i) ++buc[a[i]];
    	for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
    	for (int i = 1; i <= N; ++i) rnk[i] = buc[a[i] - 1] + 1;
    	for (int len = 1; len <= N; len <<= 1) {
    		for (int i = 1; i <= N; ++i) {
    			fir[i] = rnk[i];
    			sec[i] = i + len > N ? 0 : rnk[i + len];
    		}
    		fill(buc + 1, buc + N + 1, 0);
    		for (int i = 1; i <= N; ++i) ++buc[sec[i]];
    		for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
    		for (int i = 1; i <= N; ++i) tmp[N - --buc[sec[i]]] = i;
    		fill(buc + 1, buc + N + 1, 0);
    		for (int i = 1; i <= N; ++i) ++buc[fir[i]];
    		for (int i = 1; i <= N; ++i) buc[i] += buc[i - 1];
    		for (int i = 1, j; i <= N; ++i) {
    			j = tmp[i];
    			sa[buc[fir[j]]--] = j;
    		}
    		bool uni = true;
    		for (int i = 1, j, last = 0; i <= N; ++i) {
    			j = sa[i];
    			if (!last) rnk[j] = 1;
    			else if (fir[j] == fir[last] && sec[j] == sec[last])
    				rnk[j] = rnk[last], uni = false;
    			else rnk[j] = rnk[last] + 1;
    			last = j;
    		}
    		if (uni) break;
    	}
    	for (int i = 1, k = 0; i <= N; ++i) {
    		if (rnk[i] == 1) k = 0;
    		else {
    			if (k > 0) --k;
    			int j = sa[rnk[i] - 1];
    			while (i + k <= N && j + k <= N && a[i + k] == a[j + k]) ++k;
    		}
    		height[rnk[i]] = k;
    	}
    }
    
    bool check(int k) {
    	static int tot, vis[_];
    	int cnt = 0;
    	++tot;
    	for (int i = 1; i <= N; ++i) {
    		if (height[i] < k) ++tot, cnt = 0;
    		else {
    			if (vis[belong[sa[i]]] != tot)
    				++cnt, vis[belong[sa[i]]] = tot;
    			if (vis[belong[sa[i - 1]]] != tot)
    				++cnt, vis[belong[sa[i - 1]]] = tot;
    			if (cnt == n) return true;
    		}
    	}
    	return false;
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	freopen("sandy.in", "r", stdin);
    	freopen("sandy.out", "w", stdout);
    #endif
    	static int t[_];
    	n = ty();
    	int now = 0;
    	for (int i = 1; i <= n; ++i) {
    		++now;
    		int m = ty();
    		for (int j = 1; j <= m; ++j) t[j] = ty();
    		for (int j = 1; j <= m; ++j) t[j] = t[j + 1] - t[j];
    		for (int j = now; j <= now + m - 1; ++j)
    			s[j] = t[j - now + 1], belong[j] = i;
    		now += m - 1;
    		s[++now] = 128 + i;
    	}
    	N = now;
    	SA();
    	int l = 0, r = N;
    	while (l < r) {
    		int mid = (l + r + 1) >> 1;
    		if (check(mid)) l = mid;
    		else r = mid - 1;
    	}
    	printf("%d
    ", l + 1);
    	return 0;
    }
    
  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/newbielyx/p/12161485.html
Copyright © 2011-2022 走看看