Posts [BOJ 백준] 1919번 애너그램 만들기 / C++
Post
Cancel

[BOJ 백준] 1919번 애너그램 만들기 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


a : 0 ~ z : 26 을 매칭시켜 알파벳이 등장한 만큼, 배열의 index를 count 해주었습니다.

두 배열의 index별 원소값의 차이를 합한 값을 구하였습니다.


소스 코드


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
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std;

#define N 26

int main(int argc, char**argv) {
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	vector<int> v1(N);
	vector<int> v2(N);
	string str1, str2;
	int res=0;
	
	cin >> str1 >> str2;
	
	for(int i=0; i<str1.size(); i++)
		v1[str1.at(i)-'a']++;
	
	for(int i=0; i<str2.size(); i++)
		v2[str2.at(i)-'a']++;
	
	
	for(int i=0; i<N; i++)
		res+=abs(v1[i]-v2[i]);
		

	cout << res;
	 
	return 0;
}



This post is licensed under CC BY 4.0 by the author.

[BOJ 백준] 13300번 방 배정 / C++

[BOJ 백준] 1406번 에디터 / C++

Comments powered by Disqus.