计蒜客 UCloud 的安全秘钥(随机化+Hash)
题目链接 UCloud 的安全秘钥
对于简单的版本,我们直接枚举每个子序列,然后sort一下判断是否完全一样即可。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 200010; int n;
int a[N], b[N], c[N];
int m,q; int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i); scanf("%d", &q); while (q--){
scanf("%d", &m);
rep(i, 1, m) scanf("%d", b + i); if (m > n){
puts("0");
continue;
} sort(b +1, b + m + 1); int ans = 0; rep(i, 1, n - m +1){
rep(j, 1, m) c[j] = a[i + j - 1];
sort(c + 1, c + m + 1);
bool fl = true;
rep(j, 1, m) if (c[j] != b[j]){
fl = false;
break;
} if (fl) ++ans;
} printf("%d\n", ans);
} return 0; }
对于中等版本,这个时候不能在判断两个序列是否相似上面花太多的条件。
这个时候就想到了Hash
对$1$到$n$的每一个数,随机一个权值。
两个序列相似则有这两个序列的每个元素的Hash和相等
那么就可以维护一个Hash值的前缀和,判断的时候$O(1)$完成。
但Hash和相等只是必要条件,并不是充分条件,当数据大的时候很容易出错。
所以我随机了四组Hash值,仅当四组Hash值都与原串相等时才算相似。
这样出错概率就基本为零了
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 50010;
const long long mod = 1000007; int n;
long long w1[N], w2[N], w3[N], w4[N];
long long c1[N], c2[N], c3[N], c4[N];
int a[N];
int q, m;
int b[N << 2]; int main(){ srand(0);
scanf("%d", &n); rep(i, 1, n) w1[i] = rand() % mod;
rep(i, 1, n) w2[i] = rand() % mod;
rep(i, 1, n) w3[i] = rand() % mod;
rep(i, 1, n) w4[i] = rand() % mod; rep(i, 1, n) scanf("%d", a + i); scanf("%d", &q);
while (q--){ int ans = 0;
scanf("%d", &m);
rep(i, 1, m) scanf("%d", b + i);
long long st1 = 0, st2 = 0, st3 = 0, st4 = 0;
rep(i, 1, m){
st1 += w1[b[i]];
st2 += w2[b[i]];
st3 += w3[b[i]];
st4 += w4[b[i]];
} memset(c1, 0, sizeof c1);
memset(c2, 0, sizeof c2);
memset(c3, 0, sizeof c3);
memset(c4, 0, sizeof c4); rep(i, 1, n){
c1[i] = c1[i - 1] + w1[a[i]];
c2[i] = c2[i - 1] + w2[a[i]];
c3[i] = c3[i - 1] + w3[a[i]];
c4[i] = c4[i - 1] + w4[a[i]];
} rep(i, 1, n - m + 1){
long long n1 = c1[i + m - 1] - c1[i - 1];
long long n2 = c2[i + m - 1] - c2[i - 1];
long long n3 = c3[i + m - 1] - c3[i - 1];
long long n4 = c4[i + m - 1] - c4[i - 1];
if (n1 == st1 && n2 == st2 && n3 == st3 && n4 == st4) ++ans;
}
printf("%d\n", ans);
} return 0; }
对于困难版本,如果m比较小,则枚举连续子序列的时间会很长。
那么考虑把m小的时候的答案全部塞到map里,询问的时候直接拿出来。
不过Hash的时候还是要保证至少两组,不然出错的概率相当大。
我也是调了好久才卡过去的QAQ
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int L = 2;
const int N = 50010;
const LL mod = 1000000007; int S = 10;
int n, m, q;
int a[N];
LL Hash[N][L << 1];
LL c[N][L << 1], s[N][L << 1];
LL hash_now[L << 1];
map <LL, int> mp[20][L << 1]; int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i); srand(time(0));
rep(op, 0, L - 1){
rep(i, 1, n){
Hash[i][op] = (LL)rand();
}
} rep(i, 1, n){
rep(j, 0, L - 1){
c[i][j] = Hash[a[i]][j];
s[i][j] = s[i - 1][j] + c[i][j]; }
} S = min(S, n);
rep(len, 1, S){
rep(i, 1, n - len + 1){
rep(j, 0, L - 1){
++mp[len][j][s[i + len - 1][j] - s[i - 1][j]]; }
}
} for (scanf("%d", &q); q--;){
scanf("%d", &m);
memset(hash_now, 0, sizeof hash_now); rep(i, 1, m){
int x;
scanf("%d", &x);
rep(j, 0, L - 1) hash_now[j] += Hash[x][j];
} if (m <= S){
int ans = 1 << 30;
rep(i, 0, L - 1) ans = min(ans, mp[m][i][hash_now[i]]);
printf("%d\n", ans);
continue;
} if (m > n){
puts("0");
continue;
} int ans = 0; rep(i, 1, n - m + 1){
bool fl = true;
rep(j, 0, L - 1) if (s[i + m - 1][j] - s[i - 1][j] != hash_now[j]) fl = false;
if (fl) ++ans;
} printf("%d\n", ans);
} return 0;
}
计蒜客 UCloud 的安全秘钥(随机化+Hash)的更多相关文章
- 计蒜客 UCloud 的安全秘钥 ——(hash)
题目链接:https://nanti.jisuanke.com/t/15769. 题意是求可以变换位置以后相同的子串有多少个,那么做法是只要每个数字的平方和,立方和以及四次方和都相同就可以了. 代码如 ...
- 计蒜客 UCloud 的安全秘钥(困难)(哈希)
UCloud 的安全秘钥(困难) 编辑代码 9.53% 1200ms 262144K 每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘 ...
- 计蒜课/UCloud 的安全秘钥(hash)
题目链接:https://nanti.jisuanke.com/t/15768 题意:中文题诶- 思路:直接hash就好了,当时zz了没想到... 代码: #include <iostream& ...
- (计蒜客)UCloud 的安全秘钥
UCloud 的安全秘钥 题意 给出一个数组 s 串,和数组 t 串,那么如果两者长度相同且两者所含的数字全部相同,则说这两个串相似. 给定原始串 S ,以及 m 个询问 T 串,问 S 串有多少个连 ...
- 计蒜客 作弊揭发者(string的应用)
鉴于我市拥堵的交通状况,市政交管部门经过听证决定在道路两侧安置自动停车收费系统.当车辆驶入车位,系统会通过配有的摄像头拍摄车辆画面,通过识别车牌上的数字.字母序列识别车牌,通过连接车管所车辆信息数据库 ...
- 计蒜客的一道题dfs
这是我无聊时在计蒜客发现的一道题. 题意: 蒜头君有一天闲来无事和小萌一起玩游戏,游戏的内容是这样的:他们不知道从哪里找到了N根不同长度的木棍, 看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形. ...
- 计蒜客模拟赛5 D2T1 成绩统计
又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...
- 计蒜客 等边三角形 dfs
题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...
- 计蒜客 方程的解数 dfs
题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...
随机推荐
- python入门:1-99所有数的和的等式
#!/usr/bin/env python # -*- coding:utf-8 -*- #1-99所有数的和的等式 #start(开始,译音:思达二测)sum(合计,译音:桑木)temp(临时雇员, ...
- linux文件属性之linux文件删除原理
Linux是通过link的数量来控制文件删除的,只有当一个文件不存在任何link的时候,这个文件才会被删除.一般来说,每个文件都有2个link计数器:i_count和i_nlink. i_count的 ...
- python getopt模块使用方法
python中 getopt 模块,是专门用来处理命令行参数的 getop标准格式: 函数getopt(args, shortopts, longopts = []) shortopts 是短参数 ...
- LeetCode(143) Reorder List
题目 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...
- 数字pid笔记(1)
针对stm32中可以如下实现: p->IncrementVal = (p->Kp * (p->err - p->err_next)) + (p->Ki * p->e ...
- Developing for nRF52810(转载)
Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...
- selenium2 页面对象模型Page Object
开发Selenium WebDriver测试时,可以使用页面对象模型,这样可使得测试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来.同时页面对象模型也提供了一个注释,帮助缓存远程,避免出现元素 ...
- excludeFromRecents标签
Android:excludeFromRecents控制在不在recent列表中显示. true时不显示:false显示,默认. 运行如下activity后,不会显示在recent列表中. <a ...
- Hive中集合类型
Hive中集合类型 创建表,集合是以 - 分割的 数据文件 加载数据 查询数据 查询数组中第一个字段 再建一个表,使用map 查看数据文件 加载数据 查询数据 查询键值 创建表,struct类型 查看 ...
- loj2256 「SNOI2017」英雄联盟
真的是裸背包啊-- #include <iostream> #include <cstdio> using namespace std; typedef long long l ...