Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Last active August 29, 2015 14:17
Show Gist options
  • Save mizuhara/5ce2f1bbae41752f5645 to your computer and use it in GitHub Desktop.
Save mizuhara/5ce2f1bbae41752f5645 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/range/algorithm_ext/erase.hpp>
bool is_remove_string(const std::string &s)
{
const std::string not_remove_string = "wxyz/";
return not_remove_string.find(s) == std::string::npos;
}
std::string join(const std::vector<std::string> &v, const std::string &separator)
{
std::vector<std::string> res;
std::transform(v.begin(), v.end(), std::back_inserter(res),
[&](const std::string &s) { return res.size() + 1 == v.size() ? s : s + separator; });
return std::accumulate(res.begin(), res.end(), std::string(""));
}
std::string get_result_string(const std::string &src, const std::vector<std::string> &v)
{
if(v.empty()) {
return "none";
}
if(2 <= v.size()) {
return "many";
}
const std::string wxyz = "wxyz";
const std::string s = v[0];
std::vector<std::string> result_string;
for(size_t i = 0; i < src.length(); ++i) {
const std::string chr = src.substr(i, 1);
if(wxyz.find(chr) != std::string::npos) {
result_string.push_back(join({chr, s.substr(i, 1)}, "="));
}
}
return join(result_string, ",");
}
std::string solve(const std::string &src)
{
std::vector<std::string> patterns = {
"12S/453", "146/53D", "15S/3D4", "1T6/D45",
"215/T64", "2S5/41T", "354/S21", "3D4/15S",
"41T/2S5", "453/12S", "46T/512", "4D3/S51",
"512/46T", "53D/146", "54D/6T1", "5S2/T14",
"641/D35", "6T1/54D", "D35/641", "D45/1T6",
"S21/354", "S51/4D3", "T14/5S2", "T64/215",
};
for(size_t i = 0; i < src.length(); ++i) {
const std::string chr = src.substr(i, 1);
boost::remove_erase_if(
patterns,
[&](const std::string &s) { return is_remove_string(chr) && s.substr(i, 1) != chr; });
}
return get_result_string(src, patterns);
}
void test(const std::string &src, const std::string &expected)
{
const std::string actual = solve(src);
std::cout
<< (actual == expected ? "OK" : "***NG***")
<< std::endl;
}
void test_all()
{
/*0*/ test("Tx4/5yz", "x=1,y=S,z=2");
/*1*/ test("14S/xyz", "none" );
// 略
}
int main()
{
test_all();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment