-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
135 lines (112 loc) · 2.92 KB
/
main.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 69-distinct-subsequences.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cassert>
#include <algorithm>
#include <numeric>
using IndexRank = std::pair <size_t, int>;
using IndexRankVector = std::vector<IndexRank>;
auto split(const std::string &value, char delimiter) -> std::vector<std::string>;
auto subsequences(const std::string &sequence, const std::string &subsequence) -> int;
auto calc_rank(size_t index, const IndexRankVector &previous) -> int;
auto calc_score(const IndexRankVector &v) -> int;
int main(int argc, char *argv[])
{
if (argc > 1)
{
std::string filename(argv[1]);
std::ifstream fin(filename.c_str());
if (fin.is_open())
{
std::string line;
while (fin.good())
{
std::getline(fin, line);
if (!line.empty())
{
std::vector<std::string> tokens = split(line, ',');
assert(tokens.size() == 2);
int answer = subsequences(tokens.at(0), tokens.at(1));
std::cout << answer << "\n";
}
}
}
}
return 0;
}
// String tokeniser
std::vector<std::string> split(const std::string &value, char delimiter)
{
std::vector<std::string> ret;
size_t start = 0;
while (true)
{
size_t pos = value.find(delimiter, start);
if (pos == -1)
{
ret.push_back(value.substr(start));
break;
}
else
{
ret.push_back(value.substr(start, pos - start));
start = pos + 1;
}
}
return ret;
}
// Function to return the score of the test
int subsequences(const std::string &sequence, const std::string &subsequence)
{
IndexRankVector current, previous;
// gcc < 5.0 doesn't support std::rbegin and std::rend. So rather work with a reversed copy.
std::string rev_subsequence{ subsequence };
std::reverse(std::begin(rev_subsequence), std::end(rev_subsequence));
for (auto it = std::begin(rev_subsequence); it != std::end(rev_subsequence); ++it)
{
size_t off = 0;
size_t index = sequence.find(*it, off);
// early exit if no subsequence exists at all
if (off == 0 && index == sequence.npos)
return 0;
while (index != sequence.npos)
{
int rank = calc_rank(index, previous);
current.push_back(std::make_pair(index, rank));
off = index + 1;
index = sequence.find(*it, off);
}
previous = current;
current.clear();
}
return calc_score(previous);
}
// The rank is the no. of items in the vector having an index greater than `index`
int calc_rank(size_t index, const IndexRankVector &v)
{
if (v.empty())
{
return 1;
}
else
{
return std::accumulate(std::begin(v), std::end(v), 0, [index](int val, const IndexRank &item)
{
if (item.first > index)
return val + item.second;
else
return val;
});
}
}
// The score is the sum of all ranks of a vector
int calc_score(const IndexRankVector &v)
{
return std::accumulate(std::begin(v), std::end(v), 0, [](int val, const IndexRank &item)
{
return val + item.second;
});
}