【VOJ1895】 ニニスの守護 后缀数组 DP

有一个十进制数字符串S,它是由一个严格上升的数列A拼接而成,要求你构造A使得:

1. 最后一个数最小

2. 在1的基础上字典序最大

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#define Rep(i, x, y) for (int i = x; i <= y; i ++)
#define Dwn(i, x, y) for (int i = x; i >= y; i --)
#define RepE(i, x) for (int i = pos[x]; i; i = g[i].nex)
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 100005;
const LL mod = 100000000000007LL, m2 = mod * 20;
int n, sa[N], t1[N], t2[N], rk[N], ht[N], c[N], f[N], g[N], ans, nx[N];
LL hs[N], sh[N];
char s1[N];
LL Mult(LL x, LL y) {
	LL z = 0;
	while (y) {
		if (y & 1) z += x;
		x <<= 1, y >>= 1;
		if (x > m2) x %= mod;
	}
	return z % mod;
}
bool cmp(int *r, int x, int y, int l) { return r[x] == r[y] && r[x + l] == r[y + l]; }
void Bsa(char *s, int n, int m) {
	int *x = t1, *y = t2, p = 0;
	Rep(i, 0, m) c[i] = 0;
	Rep(i, 0, n) c[ x[i] = s[i] ] ++;
	Rep(i, 1, m) c[i] += c[i - 1];
	Dwn(i, n, 0) sa[ --c[ x[i] ] ] = i;
	for (int j = 1; p <= n; j <<= 1, m = p) {
		p = 0;
		Rep(i, n-j+1, n) y[p ++] = i;
		Rep(i, 0, n) if (sa[i] >= j) y[p ++] = sa[i] - j;
		Rep(i, 0, m) c[i] = 0;
		Rep(i, 0, n) c[ x[y[i]] ] ++;
		Rep(i, 1, m) c[i] += c[i - 1];
		Dwn(i, n, 0) sa[ --c[ x[y[i]] ] ] = y[i];
		swap(x, y), p = 1, x[ sa[0] ] = 0;
		Rep(i, 1, n) x[ sa[i] ] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p ++;
	}
}
void Bh(char *s, int n) {
	Rep(i, 0, n) rk[ sa[i] ] = i;
}
int main()
{
	scanf ("%s", s1), n = strlen(s1), s1[n] = 0;
	Bsa(s1, n, 300), Bh(s1, n), n --;
	f[0] = 0, sh[0] = 1;
	Rep(i, 1, n) sh[i] = sh[i - 1] * 37 % mod; // assert(sh[i] < 0);
	Dwn(i, n, 0) hs[i] = (hs[i + 1] * 37 + s1[i] - 'a') % mod; // 
	for (int i = 0, lt = 0; i <= n; i = lt) {
		while (s1[lt] == '0') lt ++;
		Rep(j, i, lt - 1) nx[j] = lt;
		if (s1[i] != '0') nx[i] = i, lt ++;
	}
	Rep(i, 0, n) {
		if (i) f[i] = max(f[i], f[i - 1]);
		 int k = f[i];
		f[i] = nx[ f[i] ];
		int l = i - f[i] + 1, j = nx[i + 1];
		LL h1, h2;
		h1 = hs[ f[i] ] - Mult(hs[ f[i] + l ], sh[l]) + mod;
		h2 = hs[j] - Mult(hs[j + l], sh[l]) + mod;
		if (rk[j] < rk[ f[i] ] || h1 % mod == h2 % mod) l ++;
		f[j + l - 1] = i + 1;
		 f[i] = k;
	}
	int p = f[n] - 1; g[ f[n] ] = n + 1;
	Dwn(i, n, 1) {
		if (s1[i] == '0') g[i] = max(g[i], g[i + 1]);
		if (!g[i]) continue ;
		int j = nx[i], l = g[i] - nx[i];
		if (i < l) { g[0] = max(g[0], i); break ; }
		LL h1, h2;
		h1 = hs[i - l] - Mult(hs[i], sh[l]) + mod;
		h2 = hs[j] - Mult(hs[j + l], sh[l]) + mod;
		if (rk[i - l] > rk[j] || h1 % mod == h2 % mod) l --;
		Rep(j, i - l, min(p, nx[ f[i - 1] ])) g[j] = i;
		p = min(p, i - l - 1);
	}
	while (ans <= n) {
		while (ans <= n && s1[ans] == '0') ans ++;
		if (!g[ans]) g[ans] = n + 1;
		Rep(i, ans, g[ ans ] - 1) printf("%c", s1[i]);
		printf(" "); ans = g[ans];
	}
	puts("");

	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个更详细的 Java 软件离线授权实现代码示例: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.Signature; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.Date; public class LicenseManager { private static final String LICENSE_FILE = "license.json"; private static final long EXPIRATION_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days private String licenseKey; private Date expirationDate; public boolean validateLicense() { try { BufferedReader reader = new BufferedReader(new FileReader(LICENSE_FILE)); String json = reader.readLine(); reader.close(); // Parse license information from JSON JSONObject obj = new JSONObject(json); licenseKey = obj.getString("license_key"); long expirationTime = obj.getLong("expiration_date"); expirationDate = new Date(expirationTime); // Check if the license has expired if (expirationDate.before(new Date())) { return false; } // Validate the license signature String signature = obj.getString("signature"); if (!validateSignature(json, signature)) { return false; } // Check if the license key is valid (e.g. check with a license server) return validateLicenseKey(licenseKey); } catch (IOException | JSONException e) { return false; } } private boolean validateSignature(String data, String signature) { try { // Load the public key from a file or a resource byte[] publicKeyBytes = loadPublicKey(); X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); PublicKey publicKey = factory.generatePublic(spec); // Verify the signature Signature sig = Signature.getInstance("SHA256withRSA"); sig.initVerify(publicKey); sig.update(data.getBytes()); return sig.verify(Base64.getDecoder().decode(signature)); } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | java.security.SignatureException | java.security.InvalidKeyException e) { return false; } } private byte[] loadPublicKey() throws IOException { // TODO: Load the public key from a file or a resource // In this example, we load the public key from a Base64-encoded string String publicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmJ3MvW6Y5Kfj2YKZv1zt\n" + "ZxOwXe0mN9Y6Quv2Gt2mxaRJnZ5y+9WnMhj8Z+SCMjEivKtLWtQwGJQVW2VWd0Kz\n" + "pB+QpZw2dOsTnixyI1i2RbCwQYw6q2rYXzXjvA6XJ2a6i7L9a1wF7QyIeqAtdH0u\n" + "3JQwz5uG/3e6LH6O5j8jHJj5vJyVvDkXQ1lBfUWn6+XKJdmpNqTcKx9YXJpN8mGz\n" + "V1ZJ6h0UW+auZ7m8Ld0gJy0l1lQaKwvW7C1tc0GcJpPQm6E5XJcUw1Yr3LQs7Gif\n" + "s1O8Rj0vOJ8jR8Mz7dWJ8d9tV9pQ5OuEzJZ2XnL1hE8W7sH8CjFZcZ1ZD6LjXEYJ\n" + "eQIDAQAB"; return Base64.getDecoder().decode(publicKeyString); } private boolean validateLicenseKey(String licenseKey) { // TODO: Implement license key validation logic return true; } } ``` 在上述代码中,`LICENSE_FILE` 常量指定授权文件的路径,`EXPIRATION_TIME` 常量指定授权的有效期限(单位为毫秒)。 `validateLicense()` 方法用于验证授权文件的有效性,首先读取授权文件中的授权信息(这里使用 JSON 格式),判断授权是否已过期,然后再根据授权信息验证授权的有效性。 `validateSignature()` 方法用于验证授权文件的数字签名,该方法需要使用 RSA 算法进行数字签名验证,具体步骤包括加载公钥、验证签名等。 `loadPublicKey()` 方法用于加载公钥,可以从文件或者资源中加载公钥,或者直接使用 Base64 编码的公钥字符串。 `validateLicenseKey()` 方法是一个示例方法,用于与授权服务器验证授权密钥的有效性,可以根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值