PS
BOJ 1622 : 공통 순열
lickelon
2025. 2. 3. 23:23
- 문제 링크 : http://boj.kr/1622
- 난이도 : S4
- 태그 : 문자열, 정렬
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string s, t;
while(getline(cin, s) && getline(cin, t)) {
for(int i = 0; i < 26; i++) {
int ca, cb;
ca = cb = 0;
for(auto c : s) {
if(c-'a' == i) ca++;
}
for(auto c : t) {
if(c-'a' == i) cb++;
}
int cnt = min(ca,cb);
while(cnt--) cout << (char)('a'+i);
}
cout << "\n";
}
return 0;
}
풀이
counting sort를 통해 해결할 수 있다.
빈 문자열이 입력으로 들어올 수 있다..
728x90