-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path(6 kyu) Salesman's Travel.cpp
36 lines (33 loc) · 1.06 KB
/
(6 kyu) Salesman's Travel.cpp
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
32
33
34
35
36
#include <regex>
#include <string>
#include <vector>
class SalesmanTravel
{
public:
static std::string travel(const std::string &orgr, std::string zipcode)
{
std::vector<std::string> fullAddresses;
auto it = std::find(orgr.begin(), orgr.end(), ',');
auto prevIt = orgr.begin();
do
{
fullAddresses.emplace_back(prevIt, it);
prevIt = ++it;
it = std::find(it, orgr.end(), ',');
} while (it != orgr.end());
std::string numbers;
std::string addresses;
std::regex zipcodeRegex("(\\d+)\\s(.+)\\s+" + zipcode + "$");
std::smatch match;
for (auto &&fullAddress : fullAddresses)
{
if (std::regex_match(fullAddress, match, zipcodeRegex) &&
match.size() == 3)
{
addresses += (addresses.empty() ? "" : ",") + std::string(match[2]);
numbers += (numbers.empty() ? "" : ",") + std::string(match[1]);
}
}
return zipcode + ":" + addresses + "/" + numbers;
}
};