程序员面试金典——18.7最长合成字符串
参考网址:https://www.nowcoder.com/profile/2896594/codeBookDetail?submissionId=13543240
Solution1:一开始看这道题真是木有一点思路,别人家的思维真是妙妙妙啊~~~
class LongestString {
public:
int getLongest(vector<string> str, int n) {
// write code here
sort(str.begin(), str.end(), comp);
int i, num;
for (i = n - 1; i >= 0; i--) {
string temp = str[i];
for (int j = i-1; j >= 0; j--) {
while ((num = temp.find(str[j])) != -1) {
temp.erase(num, str[j].size());
}
}
if(temp.empty()) {
return str[i].size();
}
}
return -1;
}
static bool comp(const string &a, const string &b) {
return a.size() < b.size();
}
};