Algorithm/Baekjoon
[Algorithm]Baekjoon 2607번 :: 비슷한 단어
MaxLevel
2022. 5. 17. 21:14


각 알파벳횟수를 벡터에 인덱싱 후, 횟수 비교.
다른알파벳횟수가 2개이하이고, 문자열길이가 1이하면 count++.
처음 접근은 괜찮게했는데 다른알파벳횟수를 1개이하라 생각했었다.
그런데 AAAB ,ABBA 경우 비슷한단어인데 다른알파벳횟수가 2회라서 예외가 났다.
각각 횟수가 기준문자열 A(3),B(1) 이고 비교문자열이 A(2),B(1)이라서 횟수가 2가나온다.
혹시나 다른 케이스가 더 있나 생각해봤는데 2가 컷트라인이 맞는것같다.
AAAB,ABBA는 문자자체는 같은문자로 이루어져있기때문에 다른 예제들에 비해 좀 더 비슷한단어인데, 여기서 2가나왔으니..
|
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> sv;
vector<string> testsv;
vector<int> standIndex(26);
vector<int> tempIndex(26);
int solution()
{
string stand = sv[0]; // 기준 단어.
int result = 0;
for (int i = 0; i < stand.size(); i++)
{
standIndex[(size_t)stand[i] - 65] += 1;
}
for (int i = 1; i < sv.size(); i++)
{
int count = 0;
string temp = sv[i];
memset(tempIndex.data(), 0, sizeof(int) * 26);
for (int j = 0; j < temp.size(); j++)
{
tempIndex[temp[j] - 65] += 1;
}
for (int i = 0; i < 26; i++)
{
if (standIndex[i] != tempIndex[i])
{
count += abs(standIndex[i] - tempIndex[i]);
}
}
if (count <= 2 && abs((int)stand.size() - (int)temp.size()) <= 1)
{
result++;
}
}
return result;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n = 0;
string s = "";
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s;
sv.push_back(s);
}
int result = solution();
cout << result;
}
|
cs |