알고리즘
백준 1474번 밑 줄 C++
영춘권의달인
2022. 1. 25. 19:28
_의 개수의 최대값과 최솟값의 차이는 1이 되어야 하기 때문에 가능한 밑줄의 종류는 두 가지이다.
1. 주어진 문자열과 m을 이용하여 짧은 밑줄에 _가 몇 개 들어가는지와 총 짧은 밑줄의 개수를 구한다.
2. 1과 마찬가지로 긴 밑줄에 _가 몇 개 들어가는지와 총 긴 밑줄의 개수를 구한다.
3. 문자열을 차례대로 보면서 맨 앞이 a~z일 경우 긴 밑줄의 개수가 1 이상이면 긴 밑줄을 붙이고 긴 밑줄의 개수를 1 줄인다.
4. 맨 앞이 A ~ Z일 경우 짧은 밑줄의 개수가 1 이상이면 짧은 밑줄을 붙이고 짧은 밑줄의 개수를 1 줄인다.
5. 3,4 조건만 있을 경우 모든 문자열의 첫 글자가 A~Z이면 짧은 밑줄만 쓰기 때문에 남은 짧은 밑줄의 개수가 없을 경우에는 긴 밑줄을 붙인다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int n, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
int cnt = m;
pair<int, int> short_underbar = { 0,0 };
pair<int, int> long_underbar = { 0,0 };
string str;
vector<string> v(n);
for (int i = 0; i < n; i++) {
cin >> str;
v[i] = str;
cnt -= str.length();
}
long_underbar = { cnt / (n - 1) + 1,cnt % (n - 1) };
short_underbar = { cnt / (n - 1),n - 1 - long_underbar.second };
string ans = v[0];
for (int i = 1; i < n; i++) {
if (v[i][0] >= 'a' && v[i][0] <= 'z') {
if (long_underbar.second > 0) {
long_underbar.second--;
for (int i = 0; i < long_underbar.first; i++) {
ans += '_';
}
ans += v[i];
continue;
}
}
if (short_underbar.second > 0) {
short_underbar.second--;
for (int i = 0; i < short_underbar.first; i++) {
ans += '_';
}
ans += v[i];
continue;
}
for (int i = 0; i < long_underbar.first; i++) {
ans += '_';
}
ans += v[i];
}
cout << ans;
};