num_maked
라는 숫자를 만들었다는 플래그 변수를 true로 표시합니다.num_maked
가 true라면, numbers
배열에 만든 숫자 문자열을 추가합니다.#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int N;
cin >> N;
vector<string> numbers;
while (N--) {
string line;
cin >> line;
string num;
bool num_maked = false;
for (char c : line) {
if (isdigit(c)) {
num += c;
num_maked = true;
}
else if (num_maked) {
auto it = begin(num);
while (*it == '0') it++;
if (it != end(num))
numbers.emplace_back(num.substr(it - begin(num)));
else
numbers.emplace_back("0");
num_maked = false;
num.clear();
}
}
// check if there is number in end of line.
if (num_maked) {
auto it = begin(num);
while (*it == '0') it++;
if (it != end(num))
numbers.emplace_back(num.substr(it - begin(num)));
else
numbers.emplace_back("0");
}
}
// Sort string by numeric ascending order.
sort(begin(numbers), end(numbers), [](const auto& lhs, const auto& rhs) {
if (lhs.length() == rhs.length()) {
for (int i = 0; i < lhs.length(); i++)
if (lhs[i] != rhs[i]) return lhs[i] < rhs[i];
}
return lhs.length() < rhs.length();
});
for (const auto& i : numbers) cout << i << '\n';
return 0;
}
람다함수가 잘못 됐는데 엉뚱한 곳을 디버깅 하느라 엄청 틀렸습니다.