Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 시간 복잡도
- 조세퍼스 순열
- 백준 17822
- 1764
- 백준
- 백준 5397
- 풀이
- 백준 1158
- 해시구현
- c#
- Stack 이란
- 게리멘더링2
- 자료구조
- AVL 시간 복잡도
- 해시 구현
- 백준 1406
- 별 찍기 10
- dfs
- 구현
- C/C++ 구현
- heap
- 버킷 정렬
- 스택의 특징
- 백준 17471
- ㅣ풀이
- 5397
- qorwns
- 백준 2447
- 원판 돌리기
- 백준 17779
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1431] 시리얼 번호 본문
https://www.acmicpc.net/problem/1431
요구사항
조건 대로 정렬하기
풀이
merge를 구현하였다.
구조체로 합, 길이, str을 한군데 넣고 mer 구현
#include <iostream>
#include <string>
using namespace std;
struct go {
int len;
int sum;
string str;
};
int n;
go map[1010];
go buf[1010];
void mer(go arr[], int len) {
//cout << "B" << endl;
if (len < 2) return;
int i, j, mid, k;
mid = len / 2;
i = 0; j = mid; k = 0;
mer(arr, mid);
mer(arr + mid, len - mid);
while (i < mid && j<len) {
// cout << "a" << endl;
if (arr[i].len < arr[j].len) {
buf[k++] = arr[i++];
}
else if (arr[i].len == arr[j].len) {
if (arr[i].sum < arr[j].sum) {
buf[k++] = arr[i++];
}
else if (arr[i].sum == arr[j].sum) {
if (arr[i].str < arr[j].str) {
buf[k++] = arr[i++];
}
else {
buf[k++] = arr[j++];
}
}
else {
buf[k++] = arr[j++];
}
}
else {
buf[k++] = arr[j++];
}
}
while (i < mid) {
// cout << "C" << endl;
buf[k++] = arr[i++];
}
while (j < len) {
// cout << "D" << endl;
buf[k++] = arr[j++];
}
for (int i = 0; i < len; i++) {
// cout << "E" << endl;
arr[i] = buf[i];
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> map[i].str;
map[i].len = map[i].str.length();
map[i].sum = 0;
for (int j = 0; j < map[i].len; j++) {
int now = (int)(map[i].str.at(j));
if (now >= 49 && now <= 57) {
map[i].sum += (now - 48);
}
}
}
mer(map, n);
for (int i = 0; i < n; i++) {
// cout << map[i].str << " " << map[i].len << " " << map[i].sum << endl;
cout << map[i].str << endl;
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2211] 네트워크 복구 (0) | 2020.02.15 |
---|---|
[백준 1213] 팰린드롬 만들기 (0) | 2020.02.15 |
[백준 1936] 소수 경로 (0) | 2020.02.13 |
[백준 16234] 인구 이동 (0) | 2020.02.08 |
[백준 14890] 경사로 (0) | 2020.02.08 |
Comments