-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlongest.cpp
More file actions
36 lines (31 loc) · 1.04 KB
/
longest.cpp
File metadata and controls
36 lines (31 loc) · 1.04 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
#include <stdexcept>
#include <iostream>
char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) {
if (begin == nullptr || end == nullptr || begin >= end) {
count = 0;
return nullptr;
}
const char* longest_start = begin;
const char* current_start = begin;
size_t max_length = 1;
size_t current_length = 1;
for (const char* ptr = begin + 1; ptr < end; ++ptr) {
if (*ptr == *(ptr - 1)) {
++current_length;
} else {
if (current_length > max_length) {
max_length = current_length;
longest_start = current_start;
}
current_start = ptr;
current_length = 1;
}
}
// Если все символы повторяются нужно отдельно проверить еще раз
if (current_length > max_length) {
max_length = current_length;
longest_start = current_start;
}
count = max_length;
return const_cast<char*>(longest_start);
}