알고리즘 문제풀이
[백준/BOJ] 백준 1919번 : 애너그램 만들기
GeniusJo
2020. 8. 8. 03:46
https://www.acmicpc.net/problem/1919
1919번: 애너그램 만들기
두 영어 단어가 철자의 순서를 뒤바꾸어 같아질 수 있을 때, 그러한 두 단어를 서로 애너그램 관계에 있다고 한다. 예를 들면 occurs 라는 영어 단어와 succor 는 서로 애너그램 관계에 있는데, occurs�
www.acmicpc.net
각 문자열에 등장하는 알파벳의 개수를 센 뒤, 같은 알파벳들의 개수 차이를 구해서 전체 차이를 구한다. 전체 차이가 애너그램을 만들기 위해 제거해야 하는 문자의 최소 개수이다
코드
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
string input1, input2;
int input1_cnt[26];
int input2_cnt[26];
int diff_cnt = 0;
memset(input1_cnt, 0, sizeof(input1_cnt));
memset(input2_cnt, 0, sizeof(input2_cnt));
cin >> input1 >> input2;
//각 문자열에 등장한 알파벳의 개수를 센다
for (int i = 0; i < input1.size(); i++)
{
input1_cnt[input1[i] - 'a']++;
}
for (int i = 0; i < input2.size(); i++)
{
input2_cnt[input2[i] - 'a']++;
}
//같은 알파벳들의 개수 차이를 구해서 전체 차이를 구한다
//전체 차이가 애너그램을 만들기 위해 제거해야 하는 문자의 최소 개수이다
for (int i = 0; i < 26; i++)
{
diff_cnt += abs(input1_cnt[i] - input2_cnt[i]);
}
cout << diff_cnt;
return 0;
}