-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_prefix.cpp
More file actions
53 lines (43 loc) · 864 Bytes
/
Copy pathlongest_common_prefix.cpp
File metadata and controls
53 lines (43 loc) · 864 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "essentials.cpp"
string lcp_util(string s1, string s2)
{
string result = "";
for (int i = 0, j = 0; i < s1.length(), j < s2.length(); i++, j++)
{
if (s1[i] == s2[j])
{
result += s1[i];
}
else
{
break;
}
}
return result;
}
string lcp(vector<string> &strs)
{
if (strs.size() == 0)
{
return "";
}
string result = strs[0];
for (auto it = next(strs.begin()); it != strs.end(); it++)
{
result=lcp_util(result,*it);
}
return result;
}
int main()
{
vector<string> v1;
v1.push_back("geeks");
v1.push_back("geekforgeeks");
v1.push_back("geezer");
v1.push_back("geetam");
v1.push_back("geez");
string result = "";
result += lcp(v1);
cout << result << endl;
return 1;
}