题目链接 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. Library setup

  2. 【mysql】The server quit without updating PID file

      groupadd mysql useradd -r -g mysql mysql cd /usr/local/mysql chown -R mysql:mysql . scripts/mysql_ ...

  3. 20181206(re,正则表达式,哈希)

    1.re&正则表达式 2.hashlib 一:re模块&正则表达式 正则:正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描 ...

  4. JAVA基础篇—异常

    五种常见异常 1.NullPointerException 空指针 2.ClassNotFoundException 指定类不存在 3.ArithmeticException运算异常 4.ArrayI ...

  5. Tourists Gym - 101002I LCA——dfs+RMQ在线算法

    LCA(Least Common Ancestors),即最近公共祖先,是指这样一个问题:在有根树中,找出某两个结点u和v最近的公共祖先(另一种说法,离树根最远的公共祖先). 知识需求:1)RMQ的S ...

  6. ACM-ICPC 2017 Asia Urumqi G. The Mountain

    All as we know, a mountain is a large landform that stretches above the surrounding land in a limite ...

  7. 百度地图的API接口----多地址查询和经纬度

    最近看了百度地图的API的接口,正想自己做点小东西,主要是多地址查询和经纬度坐标跟踪, 下面的代码直接另存为html就可以了,目前测试Chrome和360浏览器可以正常使用. <!DOCTYPE ...

  8. 在从1到n的正数中1出现的次数 【微软面试100题 第三十题】

    题目要求: 给定 一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有“1”的个数.    例如:N = 2,写下1,2.这样只出现了1个“1”.          N = 12 ...

  9. Vmware占用宿主机硬盘越来越大

    Vmware占用宿主机硬盘越来越大 root /usr/bin/vmware-toolbox-cmd disk shrink /

  10. HDU——1405The Last Practice(试手map)

    The Last Practice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...