当前位置:网站首页>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;
}边栏推荐
- NO.6浮点数的表示与运算
- What are the ways to realize load balancing?
- 同一宿主机不同容器网络通信流程分析
- 洛谷:P3092 [USACO13NOV]No Change G(状压+二分,独特的状态定义,不写会后悔一辈子的题)
- FreeRTOS implementation of idle tasks and blocking delay
- asterisk:No compatible codecs, not accepting this offer!
- BiShe - online reservation and registration system based on SSM
- NO.2汇编初步
- Importerror: DLL load failed while importing win32api: the specified program cannot be found.
- Analyze and hook sshd to hijack password
猜你喜欢

谷歌浏览器开发者工具的使用(掌握!)

unity 实现UI-背包装备拖拽功能

JVM性能优化

FreeRTOS-空闲任务和阻塞延时的实现

FreeRTOS personal notes - multi priority support

No.3 advanced assembly

NFT市场格局仍未变化,Okaleido能否掀起新一轮波澜?
![[acwing] solution of the 60th weekly match](/img/79/5cc097c7a432e40c4bda3ef5a167de.gif)
[acwing] solution of the 60th weekly match

Tencent cloud object storage operation process

Ranking of top ten ERP software systems at home and abroad!
随机推荐
Redis源码与设计剖析 -- 1.简单动态字符串
STL string find substring
类3实践
Installation of Topy Library (topology optimization software)
贝塞尔曲线简单介绍
Version announcement | Apache Doris 1.1 release version officially released!
Silent AI: how does shengteng AI solve the problem of sign language learning with large models?
Okaleido或杀出NFT重围,你看好它吗?
How to prepare for the autumn recruitment for the graduate students of the second non science class of Graduate School
JVM性能优化
009 execution sequence of SQL statement of interview questions
Compréhension initiale de la fonction - partie 2
Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!
FreeRTOS-空闲任务和阻塞延时的实现
华为无线设备配置智能漫游
ImportError: DLL load failed while importing win32api: 找不到指定的程序。
FreeRTOS personal notes - multi priority support
慎用TongWeb的热部署功能
AcWing 136. Adjacent value lookup
Importerror: DLL load failed while importing win32api: the specified program cannot be found.