《Cracking the Coding Interview程式設計師面試金典》----最長合成字串
時間限制:3秒 空間限制:32768K 熱度指數:352
演算法知識視訊講解題目描述
有一組單詞,請編寫一個程式,在陣列中找出由陣列中字串組成的最長的串A,即A是由其它單片語成的(可重複)最長的單詞。
給定一個string陣列str,同時給定陣列的大小n。請返回最長單詞的長度,保證題意所述的最長單詞存在。
測試樣例:
["a","b","c","ab","bc","abc"],6
返回:3
思路:先遍歷一遍,構造雜湊,然後遍歷列表,將每個單詞分成可能的兩半,通過雜湊查詢看左右兩半是否在列表內,在就記錄,並且更新最長。如果是多個單片語成,則將右半部分是否在列表中改為遞迴檢查右半部分是否有列表中其他元素
程式碼如下:
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
bool compare(string str1, string str2)
{
return str1.size()<str2.size();
}
bool match(vector<string>& str, string s, int k) //判斷第k+1個字串能否由前k個字串表示
{
for (int i = k - 1; i >= 0; i--)
{
if (str[i] == s)
return true;
if (str[i].size()<s.size())
break;
}
for (int i = 1; i<s.size(); i++)
{
if (match(str, s.substr(0, i), k) && match(str, s.substr(i), k))
return true;
}
return false;
}
int getLongest(vector<string> str, int n)
{
// write code here
int length = 0;
sort(str.begin(), str.end(), compare);
for (int i = str.size() - 1; i >= 0; i--)
{
if (match(str, str[i], i))
{
length = str[i].size();
break;
}
}
return length;
}
int main()
{
vector<string> v;
int m;
string temp;
while (cin >> m)
{
v.clear();
for (int j = 0; j < m; j++)
{
cin >> temp;
v.push_back(temp);
}
// printVector(getMiddle(v, m),m);
cout << getLongest(v, m) << endl;
}
return 0;
}
不懂的可以加我的QQ群:261035036(IT程式設計師面試寶典
群) 歡迎你的到來哦,看了博文給點腳印唄,謝謝啦~~
相關文章
- 《Cracking the Coding Interview程式設計師面試金典》----字串變換(字典樹)View程式設計師面試字串
- 《Cracking the Coding Interview程式設計師面試金典》----基本字串壓縮View程式設計師面試字串
- 《Cracking the Coding Interview程式設計師面試金典》----清除行列View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----空格替換View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----詞頻統計View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----貓狗收容所View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----子串判斷View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----數字發音View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----最小調整有序View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----連結串列分割View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----原串翻轉View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----畫素翻轉View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----翻轉子串View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----最大和子矩陣View程式設計師面試矩陣
- 《Cracking the Coding Interview程式設計師面試金典》----實時中位數View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----整數對查詢View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----單詞最近距離View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----連結串列A+BView程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----迴文連結串列View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----確定字元互異View程式設計師面試字元
- 《Cracking the Coding Interview程式設計師面試金典》----C++過載>>和View程式設計師面試C++
- 《Cracking the Coding Interview程式設計師面試金典》----最大連續數列和View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----最大字母矩陣(字母相同)View程式設計師面試矩陣
- 《Cracking the Coding Interview程式設計師面試金典》----最大子方塊(尋找01)View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----確定兩串亂序同構View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----另類加法(不得使用+-x/運算子號)View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----下一個元素(下一個比他大的)View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----連結串列中倒數第k個結點View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----從0到n中某個數字的個數View程式設計師面試
- 《Cracking the Coding Interview程式設計師面試金典》----下一個較大元素(所有比他大中最小的)View程式設計師面試
- 【程式設計師面試金典】洪水程式設計師面試
- 程式設計師面試金典Chapter1程式設計師面試APT
- 技術面試聖經《Cracking the Coding Interview》題解C++版面試ViewC++
- 程式設計師面試金典--筆記(精華篇)程式設計師面試筆記
- cracking the coding interview系列C#實現ViewC#
- 【程式設計師面試金典】20180801程式設計師面試
- 智力題(程式設計師面試經典)程式設計師面試
- android程式設計師面試寶典Android程式設計師面試