题目 | Prefix Equality
AtCoder Beginner Contest 250
E - Prefix Equality
https://atcoder.jp/contests/abc250/tasks/abc250_e
Time Limit: 4 sec / Memory Limit: 1024 MB
Score :
Problem Statement
You are given integer sequences
For
- If the set of values contained in the first
terms of , and the set of values contained in the first terms of , , are equal, then printYes
; otherwise, printNo
.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print
Sample Input 1
5
1 2 3 4 5
1 2 2 4 3
7
1 1
2 2
2 3
3 3
4 4
4 5
5 5
Sample Output 1
Yes
Yes
Yes
No
No
Yes
No
Note that sets are a concept where it matters only whether each value is contained or not.
For the
Also, for the 66-th query, the values appear in different orders, but they are still equal as sets.
我的笔记
使用
判断时,只需判断
源码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 20;
int N, Q;
unsigned int hash_a[MAXN], hash_b[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
set<int> st;
for (int i = 0; i < N; i++)
{
int tmp;
cin >> tmp;
if (st.find(tmp) == st.end())
{
st.insert(tmp);
hash_a[i + 1] = (hash_a[i] + tmp * (tmp + 1326) * (tmp + 9185));
}
else
{
hash_a[i + 1] = hash_a[i];
}
}
st.clear();
for (int i = 0; i < N; i++)
{
int tmp;
cin >> tmp;
if (st.find(tmp) == st.end())
{
st.insert(tmp);
hash_b[i + 1] = (hash_b[i] + tmp * (tmp + 1326) * (tmp + 9185));
}
else
{
hash_b[i + 1] = hash_b[i];
}
}
cin >> Q;
while (Q--)
{
int a, b;
cin >> a >> b;
if (hash_a[a] == hash_b[b])
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi