最长公共前缀短语
题目:最长公共子串的变种。给出一系列语句,要找出最长的公共前缀短语。E.g. "test a short phrase", "test a slightly longer phrase" -> "test a"
需要注意的是前缀,所以都是从串首开始。短语,所以最长前缀短语是"test a",而不是"test a s“。
- 对于vector<string>::const_iterator strIT1,使用length()方法时,要用(*strIT1).length(),用括号括起来。
- 注意find()函数查找成功时返回所在位置,失败返回string::npos的值。
- operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
- operator[]和at()返回值为char,所以如果要跟空格比较的话,要用' '。
- string - C++ Reference
- http://www.cplusplus.com/reference/string/string/
- c++中string类的详解 - yzl_rex - 博客频道 - CSDN.NET
- http://blog.csdn.net/yzl_rex/article/details/7839379
- 字符串(C++ STL <字符串>)
- https://msdn.microsoft.com/zh-cn/library/y4k49tt9(v=vs.140).aspx
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 class Solution 6 { 7 public: 8 string substr(const vector<string>& vecStr) 9 { 10 int maxLength = 0; 11 string sLongestComm = ""; 12 13 // Corner case 14 if (vecStr.size() == 0) return ""; 15 16 for (vector<string>::const_iterator strIT1 = vecStr.begin(); strIT1 != vecStr.end(); strIT1 ++) 17 { 18 // Stop if it is shorter than the current longest common phrase 19 // Need to use (*strIT1).length() rather than *strIT1.length() 20 for (int i = (*strIT1).length(); i > maxLength; i --) 21 { 22 string sComm = (*strIT1).substr(0, i); 23 24 // Use at() rather than [] as it is exception safety 25 if ((i != (*strIT1).length()) && (*strIT1).at(sComm.length()) != ' ') 26 continue; 27 28 bool isComm = true; 29 30 for (vector<string>::const_iterator strIT2 = vecStr.begin(); strIT2 != vecStr.end(); strIT2 ++) 31 { 32 // Remember the way to check not found result 33 if ((*strIT2).find(sComm) == string::npos) 34 { 35 isComm = false; 36 break; 37 } 38 } 39 40 if (isComm && (sComm.length() > maxLength) && (((*strIT1).at(sComm.length()) == ' ') || (i == (*strIT1).length()))) 41 { 42 maxLength = sComm.length(); 43 sLongestComm = sComm; 44 } 45 } // end of for 46 } // end of for 47 48 return sLongestComm; 49 } 50 }; 51 52 int main () 53 { 54 Solution testSolution; 55 string array[] = {"test a short phrase", "test a slightly longer phrase"}; 56 vector<string> vecArray(array, array + 2); 57 58 for (vector<string>::const_iterator vecCIT = vecArray.begin(); vecCIT != vecArray.end(); vecCIT ++) 59 cout << *vecCIT << endl; 60 cout << endl; 61 62 cout << testSolution.substr(vecArray); 63 64 getchar(); 65 66 return 0; 67 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 using namespace std; 12 13 class Solution 14 { 15 public: 16 string substr(const vector<string>& vecStr) 17 { 18 int maxLength = 0; 19 string sLongestComm = ""; 20 21 // Corner case 22 if (vecStr.size() == 0) return ""; 23 24 for (auto j = 0; j < vecStr.size(); j ++) 25 { 26 // Stop if it is shorter than the current longest common phrase 27 // Need to use (*strIT1).length() rather than *strIT1.length() 28 for (auto i = vecStr[j].size(); i > maxLength; i --) 29 { 30 string sComm = vecStr[j].substr(0, i); 31 32 // Use at() rather than [] as it is exception safety 33 if ((i != vecStr[j].length()) && vecStr[j].at(sComm.length()) != ' ') 34 continue; 35 36 bool isComm = true; 37 38 for (auto k = 0; k < vecStr.size(); k ++) 39 { 40 // Remember the way to check not found result 41 if (vecStr[k].find(sComm) == string::npos) 42 { 43 isComm = false; 44 break; 45 } 46 } 47 48 if (isComm && (sComm.length() > maxLength) && ((vecStr[j].at(sComm.length()) == ' ') || (i == vecStr[j].length()))) 49 { 50 maxLength = sComm.length(); 51 sLongestComm = sComm; 52 } 53 } // end of for 54 } // end of for 55 56 return sLongestComm; 57 } 58 }; 59 60 int main () 61 { 62 Solution testSolution; 63 string array[] = {"test a short phrase", "test a slightly longer phrase", "test a slightly slightly longer phrase", "test "}; 64 vector<string> vecArray(array, array + 4); 65 66 for (auto vecCIT : vecArray) 67 cout << vecCIT << endl; 68 cout << endl; 69 70 cout << testSolution.substr(vecArray) << endl; 71 72 return 0; 73 }