-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlongest.cpp
More file actions
31 lines (25 loc) · 902 Bytes
/
longest.cpp
File metadata and controls
31 lines (25 loc) · 902 Bytes
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
#include <stdexcept>
template<typename CharPtr>
CharPtr FindLongestSubsequenceImpl(CharPtr begin, CharPtr end, size_t& count) {
if (!begin || !end || begin >= end) {
count = 0;
return nullptr;
}
CharPtr best = begin;
CharPtr current = begin;
size_t bestLen = 1;
size_t currentLen = 1;
for (CharPtr p = begin + 1; p < end; ++p) {
(*p == *(p - 1)) ?
(currentLen++, (currentLen > bestLen) ? (void)(bestLen = currentLen, best = current) : (void)0) :
(void)(current = p, currentLen = 1);
}
count = bestLen;
return best;
}
char* FindLongestSubsequence(char* begin, char* end, size_t& count) {
return FindLongestSubsequenceImpl(begin, end, count);
}
const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) {
return FindLongestSubsequenceImpl(begin, end, count);
}