题目链接 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. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals

    1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...

  2. Lecture 1

    Principles of GIS( UNSW Metternicht ) outline:data input---data management---data manipulation+data ...

  3. GoF23种设计模式之结构型模式之外观模式

    一.概述         为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 二.适用性 1.当你要为一个复杂子系统提供一个简单接口的时候.子系统 ...

  4. Python基础-os模块 sys模块

    sys模块 与操作系统交互的一个接口 文件夹相关 os.makedirs('dirname1/dirname2')    可生成多层递归目录 os.removedirs('dirname1')    ...

  5. python3中urllib库的request模块详解

    刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...

  6. Markdown 使用锚点

    首先是建立一个跳转的连接: [说明文字](#jump) 然后标记要跳转到什么位置即可: <span id = "jump">跳转到这里:</span>

  7. 【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸

    多维数组声明 数据类型[][] 数组名称; 数据类型[] 数组名称[]; 数据类型数组名称[][]; 以上三种语法在声明二维数组时的功能是等价的.同理,声明三维数组时需要三对中括号,中括号的位置可以在 ...

  8. Leetcode207--->课程表(逆拓扑排序)

    题目: 课程表,有n个课程,[0, n-1]:在修一个课程前,有可能要修前导课程: 举例: 2, [[1,0]] 修课程1前需要先修课程0 There are a total of 2 courses ...

  9. IDEA界面创建Scala的Maven项目

    1. 创建Maven工程,勾选右侧的Create from archetype选项,然后选中下方的scala-archetype-simple选项,如图所示:2. 填写相应的GroupId.Artif ...

  10. js的编码函数

    js对文字进行编码,涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...