-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlongest.cpp
More file actions
55 lines (51 loc) · 1.23 KB
/
longest.cpp
File metadata and controls
55 lines (51 loc) · 1.23 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdexcept>
char *FindLongestSubsequence(char *begin, char *end, size_t &count)
{
count = 0;
if (begin && end)
{
char *subsequence = nullptr;
for (long i = 0; i < end - begin; ++i)
{
char *newsequence = begin + i;
size_t t = 1;
while (*newsequence == *(newsequence + t) && newsequence + t < end)
{
++i;
++t;
}
if (t > count)
{
count = t;
subsequence = newsequence;
}
}
return subsequence;
}
return nullptr;
}
const char *FindLongestSubsequence(const char *begin, const char *end, size_t &count)
{
count = 0;
size_t pos = 0;
if (begin && end)
{
for (long i = 0; i < end - begin; ++i)
{
size_t t = 1;
const char *newsubsequence = begin + i;
while (*newsubsequence == *(newsubsequence + t) && i < end - begin - 1)
{
++i;
++t;
}
if (t > count)
{
count = t;
pos = i - t + 1;
}
}
return begin + pos;
}
return nullptr;
}