当前位置:网站首页>Prefix Equality 【DP | 哈希】
Prefix Equality 【DP | 哈希】
2022-07-17 20:46:00 【Codiplay】
E - Prefix Equality
题意:给出两段整数序列a,b,接着是q次询问,询问给出两个整数x,y,问a的前x位数字和b的前y位数字种类是否完全相同。
DP的思想!
- 我们只关心a前x位数字储存的信息和b前y位储存的信息
- 信息储存什么东西呢?对于这个题,数字可能相同,我们只关心数字第一次出现的位置,储存到prea里面
- ansa表示对于a的前x个数字,b相应数字出现的坐标的最大值,如果a的前x数字中b中不存在某数,则设为inf。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
int a[200010], b[200010];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
std::map<int, int> prea,preb;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
if(!prea.count(a[i]))prea[a[i]] = i;
}
for (int i = 1; i <= n; i ++ ) {
cin >> b[i];
if(!preb.count(b[i]))preb[b[i]] = i;
}
std::vector<int> ansa(n + 1, 0), ansb(n + 1, 0);
for (int i = 1; i <= n; i ++ ) {
ansa[i] = max(ansa[i - 1], preb.count(a[i]) ? preb[a[i]] : inf);
ansb[i] = max(ansb[i - 1], prea.count(b[i]) ? prea[b[i]] : inf);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
cout << ((ansb[y] <= x && ansa[x] <= y) ? "Yes" : "No") << '\n';
}
return 0;
}哈希
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll a[200010], b[200010];
set<ll> se;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
ll s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
a[i] = s;
se.insert(x);
}
se.clear();
s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
b[i] = s;
se.insert(x);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
if(a[x] == b[y]) {
cout << "Yes\n";
}
else cout << "No\n";
}
return 0;
}边栏推荐
- 无声的AI:昇腾AI如何用大模型破解手语学习的难题?
- 洛谷P3522 [POI2011]TEM-Temperature 题解
- Go exceed API source code reading (III) -- openreader ()
- 分析并HOOK SSHD来劫持密码
- 4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C
- 数据库的增删改查
- Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!
- NO.2汇编初步
- 洛谷:P4516 [JSOI2018] 潜入行动(树形dp、树上分组背包统计方案数)
- 【ACWing】2492. HH的项链
猜你喜欢
随机推荐
ModuleNotFoundError: No module named ‘_ distutils_ hack‘
FreeRTOS-空闲任务和阻塞延时的实现
Redis源码与设计剖析 -- 2.链表
(附源码)多种机器学习模型(KNN\LR\RF\Ada\Xg\GBDT...)下的降水降尺度中的模型训练
Introduction:Multiple DataFrames
ImportError: DLL load failed while importing win32api: 找不到指定的程序。
Redis源码与设计剖析 -- 4.跳跃表
4某公司在6个城市c1,c2,c3…c6中有分公司,已知城市ci到cj(I,j=1,2,3,…6)的联通情况下及费用的大小列于以下带权邻接矩阵中C中
Luogu p3522 [poi2011] TEM temperature solution
同一宿主机不同容器网络通信流程分析
揭开服务网格~Istio Service Mesh神秘的面纱
FreeRTOS个人笔记-支持多优先级
A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
96. 不同的二叉搜索树
洛谷P3512 [POI2010]PIL-Pilots 题解
通达信开户是真的吗?通达信开户安全吗?
Okaleido or get out of the NFT siege, are you optimistic about it?
Keil环境下STM32定位hardfault位置方法和遇到的情况
No.2 compilation preliminary









