Created
November 21, 2017 02:16
-
-
Save whoo24/90aa478f9cb258094b3ef7aa0bebe0a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Algorism = not 'Algorithm' | |
/* | |
1 < N < 100,000 | |
예) | |
입력 : abaccchhaa | |
결과 : b1a1c3h2a3 | |
*/ | |
#include "stdafx.h" | |
#include <gtest\gtest.h> | |
#include <vector> | |
std::string ToString(int value) { | |
char buffer[255]; | |
const errno_t error = ::_ltoa_s(value, buffer, 10); | |
if (error != 0) { | |
return ""; | |
} | |
return buffer; | |
} | |
std::string ToString(const std::vector<std::pair<char, int>>& source) { | |
std::string result; | |
result.reserve(source.size() * 2); | |
for (auto& p : source) { | |
result.append(1, p.first); | |
result.append(ToString(p.second)); | |
} | |
return result; | |
} | |
std::vector<std::pair<char, int>> Solve(const std::string& source) { | |
std::vector<std::pair<char, int>> result_vector; | |
for (auto it = source.begin(); it != source.end(); ++it) { | |
const char ch = *it; | |
if (result_vector.empty() || result_vector.rbegin()->first != ch) { | |
result_vector.emplace_back(ch, 0); | |
} | |
result_vector.rbegin()->second += 1; | |
} | |
if (result_vector.size() > 1) { | |
if (result_vector.front().first == result_vector.rbegin()->first) { | |
result_vector.rbegin()->second += 1; | |
} else { | |
result_vector.emplace_back(*result_vector.rbegin()); | |
} | |
result_vector.erase(result_vector.begin()); | |
} | |
return std::move(result_vector); | |
} | |
TEST(Test, Test) { | |
auto test_string = "abaccchhaa"; | |
auto result = Solve(test_string); | |
EXPECT_STREQ("b1a1c3h2a3", ToString(result).c_str()); | |
} | |
int main(int argc, char **argv) { | |
::testing::InitGoogleTest(&argc, argv); | |
return RUN_ALL_TESTS(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment