백준 1213 c++

magicdrill·2024년 4월 24일
0

백준 문제풀이

목록 보기
334/655

백준 1213 c++

이게 맞나? 너무 억지로 푼거 같다... 큐나 스택같은 자료구조를 사용하는 다른 방법이 있을거 같다...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string input_str()
{
	string str;

	cin >> str;

	return str;
}

string find_answer(string str)
{
	int i, j;
	int count = 1;
	string A = "", B = "";
	string answer;
	bool odd = false;
	char oddchar = ' ';
	vector<int> alphabet(26, 0);

	if (str.length() == 1)
	{
		return str;
	}
	for (i = 0; i < str.length(); i++)
	{
		//cout << str[i] - 'A' << " ";
		alphabet[str[i] - 'A']++;
	}
	for (i = 0; i < alphabet.size(); i++)
	{
		if (alphabet[i] % 2 == 1)
		{
			if (odd == false)
			{
				oddchar = i + 'A';
				odd = true;
				//cout << oddchar << "\n";
			}
			else
			{
				return  "I'm Sorry Hansoo";
			}
		}
	}
	for (i = 0; i < alphabet.size(); i++)
	{
		for (j = 0; j < alphabet[i] / 2; j++)
		{
			A += (i + 'A');
		}
	}
	//cout << A << "\n";
	for (i = A.length() - 1; i >= 0; i--)
	{
		B += A[i];
	}
	//cout << B << "\n";
	if (odd == true)
	{
		answer = A + oddchar + B;
	}
	else
	{
		answer = A + B;
	}

	return answer;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	string str;
	//vector <string> answers;

	str = input_str();
	str = find_answer(str);
	cout << str << "\n";

	return 0;
}

0개의 댓글