[λ°±μ€] λ¬Έμμ΄ μ²λ¦¬ - 1764. λ£λ³΄μ‘
λ¬Έμ
κΉμ§μμ΄ λ£λ λͺ»ν μ¬λμ λͺ λ¨κ³Ό, 보λ λͺ»ν μ¬λμ λͺ λ¨μ΄ μ£Όμ΄μ§ λ, λ£λ 보λ λͺ»ν μ¬λμ λͺ λ¨μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ λ ₯
첫째 μ€μ λ£λ λͺ»ν μ¬λμ μ N, 보λ λͺ»ν μ¬λμ μ Mμ΄ μ£Όμ΄μ§λ€. μ΄μ΄μ λμ§Έ μ€λΆν° Nκ°μ μ€μ κ±Έμ³ λ£λ λͺ»ν μ¬λμ μ΄λ¦κ³Ό, N+2μ§Έ μ€λΆν° 보λ λͺ»ν μ¬λμ μ΄λ¦μ΄ μμλλ‘ μ£Όμ΄μ§λ€. μ΄λ¦μ λμ΄μ°κΈ° μμ΄ μμ΄ μλ¬Έμλ‘λ§ μ΄λ£¨μ΄μ§λ©°, κ·Έ κΈΈμ΄λ 20 μ΄νμ΄λ€. N, Mμ 500,000 μ΄νμ μμ°μμ΄λ€.
μΆλ ₯
λ£λ³΄μ‘μ μμ κ·Έ λͺ λ¨μ μ¬μ μμΌλ‘ μΆλ ₯νλ€.
μμ μ λ ₯1
3 4
ohhenrie
charlie
baesangwook
obama
baesangwook
ohhenrie
clinton
μμ μΆλ ₯1
2
baesangwook
ohhenrie
μκ°
μ²μμ find() ν¨μλ₯Ό μ¬μ©ν΄λ³΄λ κ²μΌλ‘ νλ€. O(N)μ μκ°λ³΅μ‘λκ° λμ μκ° μ΄κ³Όκ° λμ¨λ€.
binary_search()λ₯Ό μ΄μ©ν΄μ νμ΄μΌνλ€. (μκ°λ³΅μ‘λ μκ°ν΄μΌλλ λ¬Έμ => dp, λΆν μ 볡! )
push_backμ μ¬μ©νλ κ²λ³΄λ€ resize() λ₯Ό μ¬μ©ν΄μ 미리 λ©λͺ¨λ¦¬λ₯Ό ν λΉνλ©΄ λ©λͺ¨λ¦¬ μ¬μ©μ μ€μΌ μ μλ€.
λ°±μ€μμ μ€νν΄λ³΄λ 200KBλ μ°¨μ΄κ° λ¬λ€.
μμ±ν μ½λ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, const char *argv[])
{
// insert code here...
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
vector<string> a; //λ£λ λͺ»ν μ¬λ
vector<string> result; //λ£λ 보λ λͺ»ν μ¬λ
cin >> n >> m;
a.resize(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// μ΄μ§ νμμ μν μ λ ¬
sort(a.begin(), a.end());
for (int i = 0; i < m; i++)
{
string s;
cin >> s;
if (binary_search(a.begin(), a.end(), s))
{
result.push_back(s);
}
}
sort(result.begin(), result.end());
cout << result.size() <<"\n";
for (int i = 0; i < result.size(); i++)
{
cout << result[i] << "\n";
}
return 0;
}
Colored by Color Scripter
|