7 분 소요

🗒️ 11328번) Strfry

#include <iostream>
using namespace std;

bool IsPossible(string str, string comp)
{
	if (str.length() != comp.length())
		return false;

	int hasAlpha[26] = {};
	for (char c : str)
		++hasAlpha[c - 'a'];

	for (char c : comp)
	{
		if (!hasAlpha[c - 'a'])
			return false;

		--hasAlpha[c - 'a'];
	}

	return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		string str, comp;
		cin >> str >> comp;

		if (IsPossible(str, comp))
			cout << "Possible" << endl;
		else
			cout << "Impossible" << endl;
	}
	return 0;
}

  • 위의 코드(48ms)보다 아래의 코드(8ms)가 더 시간이 적게 걸림..
#include <iostream>
using namespace std;

bool result[1000];

bool IsPossible(string str, string comp)
{
	if (str.length() != comp.length())
		return false;

	int hasAlpha[26] = {};
	for (char c : str)
		++hasAlpha[c - 'a'];

	for (char c : comp)
	{
		if (!hasAlpha[c - 'a'])
			return false;

		--hasAlpha[c - 'a'];
	}

	return true;
}

int main() 
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;
	
	for (int i = 0; i < N; i++)
	{
		string str, comp;
		cin >> str >> comp;

		result[i] = IsPossible(str, comp);
	}

	for (int i = 0; i < N; i++)
	{
		if (result[i])
			cout << "Possible" << '\n';
		else
			cout << "Impossible" << '\n';
	}
	return 0;
}

  • strcpy
  • strcmp
  • strtol
  • strtok
  • strlen
  • strcat
  • strfry

카테고리:

업데이트:

댓글남기기