题目链接 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)的更多相关文章

  1. 计蒜客 UCloud 的安全秘钥 ——(hash)

    题目链接:https://nanti.jisuanke.com/t/15769. 题意是求可以变换位置以后相同的子串有多少个,那么做法是只要每个数字的平方和,立方和以及四次方和都相同就可以了. 代码如 ...

  2. 计蒜客 UCloud 的安全秘钥(困难)(哈希)

    UCloud 的安全秘钥(困难) 编辑代码 9.53% 1200ms 262144K 每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘 ...

  3. 计蒜课/UCloud 的安全秘钥(hash)

    题目链接:https://nanti.jisuanke.com/t/15768 题意:中文题诶- 思路:直接hash就好了,当时zz了没想到... 代码: #include <iostream& ...

  4. (计蒜客)UCloud 的安全秘钥

    UCloud 的安全秘钥 题意 给出一个数组 s 串,和数组 t 串,那么如果两者长度相同且两者所含的数字全部相同,则说这两个串相似. 给定原始串 S ,以及 m 个询问 T 串,问 S 串有多少个连 ...

  5. 计蒜客 作弊揭发者(string的应用)

    鉴于我市拥堵的交通状况,市政交管部门经过听证决定在道路两侧安置自动停车收费系统.当车辆驶入车位,系统会通过配有的摄像头拍摄车辆画面,通过识别车牌上的数字.字母序列识别车牌,通过连接车管所车辆信息数据库 ...

  6. 计蒜客的一道题dfs

    这是我无聊时在计蒜客发现的一道题. 题意: 蒜头君有一天闲来无事和小萌一起玩游戏,游戏的内容是这样的:他们不知道从哪里找到了N根不同长度的木棍, 看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形. ...

  7. 计蒜客模拟赛5 D2T1 成绩统计

    又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...

  8. 计蒜客 等边三角形 dfs

    题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...

  9. 计蒜客 方程的解数 dfs

    题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...

随机推荐

  1. Linux常用快捷键以及如何查看命令帮助

    1.1    Linux系统快速操作常用快捷键 快捷键名称 快捷作用 Ctrl + a 将光标移至行首 Ctrl + e 将光标移至行尾 Ctrl + u 前提光标在行尾,则清除当前行所有的内容(有空 ...

  2. Vue之数据传递

    基础:vue的响应式规则 简单的props更新 父组件 <template> <div> <block-a :out-data="x">< ...

  3. Python学习day01

    age = 23 count=0 while count<3: guess_age = int (input("My age:")) if age ==guess_age: ...

  4. Liunx将私密代理添加到环境变量

    .bash_profile文件存在于用户主目录下,绝对路径为/home/$name/.bash_profile.bash_profile文件是隐藏文件,里面包含的是用户的用户的环境变量. 注意: 这个 ...

  5. Android Studio中不能显示svn的上传下载两个图标同时version control为灰,不可点击

    最近在接触Android Studio,涉及到svn的配置,因为是先安装的svn,后安装的Android Studio,后边同事告诉我, Android Studio 的SVN安装与其他IDE有很大差 ...

  6. socket中send和recv函数

    Socket一次Recv接受的字节有限制么? 从套接字接收数据. 返回值是表示接收数据的字符串. 一次接收的最大数据量由bufsize指定.它默认为零. 注意为了最好地匹配硬件和网络现实,bufsiz ...

  7. dedecms 建站相关问题

    1.栏目新建文章提示:模板文件不存在,无法解析文档! 解决方法:把模板文件使用".html"的格式 /include/arc.archives.class.php 556行 if ...

  8. 计算n的阶乘(n!)末尾0的个数

    题目: 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数. 举例: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 24 ...

  9. day04_06 短路原则

    True和False不能写成ture和false,不然会报错 not not True or False and not True 按照not>and>or来进行括号 (not (not ...

  10. RIP 路由协议

    RIP动态路由选择协议 routing information protocol     IGP   小范围   路由器限制为15台  超过可能无法收敛   收敛概念  在一个域内  各个路由器知道各 ...