Algorithm/C++
[백준 11328번] Strfry
imagineer_jinny
2022. 8. 10. 17:36
11328번: Strfry
C 언어 프로그래밍에서 문자열(string)은 native한 자료형이 아니다. 사실, 문자열은 그저, 문자열의 끝을 표시하기 위한 말단의 NULL이 사용된, 문자들로 이루어진 문자열일 뿐이다. 하지만 프로그래
www.acmicpc.net
배운 것
string 2개 비교할 때 각각 빈 배열 하나씩 총 두개 만들어줄 생각만 했는데
같은지 아닌지를 비교하는 단순한 것이라면
하나 만들고 ++ -- 해서 0이 아닌것만 골라내는 방식을 쓰는 것이 더 간단함
정답 풀이
// Authored by : OceanShape
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/a3d03c0124b544759d306668e55bbf4b
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
while (N--) {
int a[26] = {}; // 각 문자의 개수를 저장하는 배열
string s1, s2;
cin >> s1 >> s2;
for (auto c : s1) a[c-'a']++; // 첫 번째 문자열의 각 문자는 개수+1
for (auto c : s2) a[c-'a']--; // 두 번째 문자열의 각 문자는 개수-1
// 0이 아닌 배열의 요소가 있을 경우, 개수가 다른 문자가 존재하므로 false
bool isPossible = true;
// 중괄호가 없어도 문제는 없으나 가독성을 위해 삽입
for (int i : a){
if (i != 0) isPossible = false;
}
if(isPossible) cout << "Possible\n";
else cout << "Impossible\n";
}
}