forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_bruteforce_n2.cpp
More file actions
41 lines (36 loc) · 882 Bytes
/
Copy pathAC_bruteforce_n2.cpp
File metadata and controls
41 lines (36 loc) · 882 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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_bruteforce_n2.cpp
* Create Date: 2014-12-03 22:21:37
* Descripton: bruteforce
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if (strs.empty())
return "";
for (int i = 0; i < strs[0].length(); i++) {
for (int j = 1; j < strs.size(); j++)
if (i >= strs[j].length() || strs[j][i] != strs[0][i])
return strs[0].substr(0, i);
}
return strs[0];
}
};
int main() {
Solution s;
int n;
string t;
while (cin >> n) {
vector<string> strs;;
while (n--) {
cin >> t;
strs.push_back(t);
}
cout << s.longestCommonPrefix(strs) << endl;
}
return 0;
}