-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlongest.cpp
More file actions
37 lines (30 loc) · 1.03 KB
/
longest.cpp
File metadata and controls
37 lines (30 loc) · 1.03 KB
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
#include <stdexcept>
const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) {
const char* start_of_longest_seq = nullptr;
count = 0;
size_t this_seq_count = 0;
if ((begin == nullptr) || (end==nullptr) || (begin > end)) {
return start_of_longest_seq;
}
const char* next_el = nullptr;
while(begin != end){
this_seq_count = 1;
if (begin < end - 1){
next_el = begin + 1;
for (; next_el != end; ++next_el){
if (*next_el != *begin) break;
++this_seq_count;
}
}
if (this_seq_count > count){
count = this_seq_count;
start_of_longest_seq = begin;
}
begin += this_seq_count;
}
return start_of_longest_seq;
}
char* FindLongestSubsequence(char* begin, char* end, size_t& count){
const char* const_result = FindLongestSubsequence(const_cast<const char*>(begin), const_cast<const char*>(end), count);
return const_cast<char*>(const_result);
}