#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
/*
* 思路:首先统计字符串数组中最短的长度,接着开始竖向比较,一旦有不同,跳出循环,否则前缀+1
*/
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string res = "";
int lst_num = 202;
for (int i = 0; i < strs.size(); i++)
{
if (lst_num > strs[i].size())
lst_num = strs[i].size();
}
for (int j = 0; j < lst_num; j++)
{
char s = strs[0][j];
for (int i = 1; i < strs.size(); i++)
{
if (s != strs[i][j])
{
return res;
}
}
res += s;
}
return res;
}
};
int main()
{
int x;
cin >> x;
vector<string>input;
string s;
for (int i = 0; i < x; i++)
{
cin >> s;
input.push_back(s);
}
Solution A;
cout << A.longestCommonPrefix(input) << endl;
}
07-18
548
02-24
509