-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55.cpp
More file actions
41 lines (37 loc) · 862 Bytes
/
55.cpp
File metadata and controls
41 lines (37 loc) · 862 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool iscommon(vector<string> sv, int len){
string check = sv[0].substr(0, len);
for(int i=1;i<(int)sv.size();i++){
if(sv[i].find(check)!=0)return false;
}
return true;
}
int main(){
//program to find longest common prefix in a vector of strings.
int n;
cin>>n;
vector<string> sv;
for(int i=0;i<n;i++){
string s;
cin>>s;
sv.push_back(s);
}
if(n==0){
cout<<""<<endl;
return 0;
}
int hi = sv[0].length();
int low = 0;
for(int i=1;i<sv.size();i++){
hi = min(hi, (int)sv[i].length());
}
while(low<=hi){
int mid = (low + hi)/2;
if(iscommon(sv, mid))low = mid + 1;
else hi = mid - 1;
}
cout<<sv[0].substr(0, (low+hi)/2)<<endl;
return 0;
}