새로새록

[c++] 애너그램 삭제 최소 문자수 본문

소프트웨어융합/경희대 c++ 과제

[c++] 애너그램 삭제 최소 문자수

류지나 2020. 7. 16. 21:21

#include<iostream>
#include<string>
using namespace std;

int anagram(const string& lhs, const string& rhs);
int main() {
	string s1, s2;
	cin >> s1 >> s2;
	cout << anagram(s1, s2) << "\n";
}
int anagram(const string& lhs, const string& rhs) {
	int result = 0;
	int lhsAlphabet[26]{ 0 };
	int rhsAlphabet[26]{ 0 };
	
	for (size_t i = 0; i < lhs.size(); i++)
	{
		lhsAlphabet[static_cast<int>(lhs[i] - 'a')]++;
		result++;
	}
	for (size_t i = 0; i < rhs.size();i++) {
		rhsAlphabet[static_cast<int>(rhs[i] - 'a')]++;
		result++;
	}
		for (int i = 0; i < sizeof(lhsAlphabet) / sizeof(int);i++) {
			if (lhsAlphabet[i] != 0 && rhsAlphabet[i] != 0) {
			{	int common = (lhsAlphabet[i] > rhsAlphabet[i]) ?
					rhsAlphabet[i] : lhsAlphabet[i];
				result -= common * 2;
			}
		}
	return result;
}

'소프트웨어융합 > 경희대 c++ 과제' 카테고리의 다른 글

[c++]9. stack class  (0) 2020.07.17
[c++]9. 숫자 야구  (0) 2020.07.17
[c++]9. 학생과 조교  (0) 2020.07.16
[c++]8. 어벤져스 캐릭터 배틀  (0) 2020.07.16
[c++]8. 기차의 사람 수 계산  (0) 2020.07.16