1일1알

백준 1474번 밑 줄 C++ 본문

알고리즘

백준 1474번 밑 줄 C++

영춘권의달인 2022. 1. 25. 19:28

출처 : https://www.acmicpc.net/problem/1474

 

_의 개수의 최대값과 최솟값의 차이는 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;
};

'알고리즘' 카테고리의 다른 글

백준 1083번 소트 C++  (0) 2022.01.27
백준 1092번 배 C++  (0) 2022.01.26
백준 2872번 우리집엔 도서관이 있어 C++  (0) 2022.01.24
백준 2891번 카약과 강풍 C++  (0) 2022.01.23
백준 5747 Odd or Even C++  (0) 2022.01.23