这题很简单,只需要做一个简单的遍历即可
#include <string>
#include <iostream>
#include <vector>
using namespace std;
string longestCommonPrefix(vector<string>& strs) {
if(strs.empty())
return "";
string comstr=strs[0];
for(int i=1;i!=strs.size();i++)
{
int j;
for(j=0;j!=comstr.size();j++)
{
if(comstr[j]==strs[i][j])
;
else
break;
}
comstr=comstr.substr(0,j);
}
return comstr;
}
int main()
{
string s1("abcdefg");
string s2("abcddde");
string s3("av");
vector<string> strs;
strs.push_back(s1);
strs.push_back(s2);
strs.push_back(s3);
cout<<longestCommonPrefix(strs)<<endl;
return 0;
}