https://www.acmicpc.net/problem/2870

 

2870번: 수학숙제

종이에서 찾은 숫자의 개수를 M이라고 하면, 출력은 M줄로 이루어져야 한다. 각 줄에는 종이에서 찾은 숫자를 하나씩 출력해야 한다. 이때, 비내림차순으로 출력해야 한다. 비내림차순은 내림차

www.acmicpc.net

이문제는 range 범위 터지는거에 대해서 좀 신경 써야하는 문제였습니다.

문자열 비교 문제고 

string->int로 파싱해서 변경비교하면 자료형 범위가 터지는 문제가 발생해서

무조건 string에서 비교하여 정렬해야해합니다. 

엄청 긴 문자열에 대한 생각이 부족했습니다.

문자열 크기 비교 정렬:

bool compareStrings(const std::string& str1, const std::string& str2) {
	if (str1.length() != str2.length()) {
		return str1.length() < str2.length();
	}
	else {
		return str1 < str2;
	}

}

정답:

#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

struct Point {
	int i, j, cnt;
	char c;
};

void Prints(vector<vector<int>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<bool>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
bool compareStrings(const std::string& str1, const std::string& str2) {
	if (str1.length() != str2.length()) {
		return str1.length() < str2.length();
	}
	else {
		return str1 < str2;
	}

}
int main() {
	int n;
	cin >> n;
	string str;
	vector<string>data;
	for (int i = 0; i < n; i++) {
		cin >> str;
		data.push_back(str);

	}
	char arrays[] = { '0','1','2','3','4','5','6','7','8','9' };
	vector<string>anw;
	for (int i = 0; i < data.size(); i++) {
		string tmp = "";


		bool bCheck = false;
		for (int j = 0; j < data[i].size(); j++) {
			bCheck = false;
			for (int z = 0; z < 10; z++) {

				//	cout << data[i][j]<<","<<arrays[z]<<endl;

				if (data[i][j] == arrays[z]) {
					//0    0
					//   
					bCheck = true;
					if (tmp[0] == '0') {
						tmp = "";
					}
					tmp += data[i][j];
					//cout << tmp<<endl;
					//cout << tmp;
					z = 10;
				}


			}
			if (bCheck == false) {
				if (tmp != "") {
					//cout << tmp << endl;
					anw.push_back(tmp);
					tmp = "";
				}
			}
		}
		if (bCheck == true) {
			if (tmp != "") {
				//	cout << tmp << endl;
				anw.push_back(tmp);
				tmp = "";
				bCheck = false;
			}
		}

	}

	sort(anw.begin(), anw.end(), compareStrings);
	for (int i = 0; i < anw.size(); i++) {
		cout << anw[i] << endl;
	}

	return 0;
}

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

백준 회의실 배정  (0) 2023.07.17
백준 영단어 암기는 괴로워  (0) 2023.07.16
백준 전쟁-전투  (0) 2023.07.16
백준 적록색약  (0) 2023.07.16
백준 음식물 피하기  (0) 2023.07.16

+ Recent posts