《Cracking the Coding Interview程式設計師面試金典》----字串變換(字典樹)
時間限制:3秒 空間限制:32768K 熱度指數:327
演算法知識視訊講解題目描述
現有一個字典,同時給定字典中的兩個字串s和t,給定一個變換,每次可以改變字串中的任意一個字元,請設計一個演算法,計算由s變換到t所需的最少步數,同時需要滿足在變換過程中的每個串都是字典中的串。
給定一個string陣列dic,同時給定陣列大小n,串s和串t,請返回由s到t變換所需的最少步數。若無法變換到t則返回-1。保證字串長度均小於等於10,且字典中字串數量小於等於500。
測試樣例:
["abc","adc","bdc","aaa”],4,”abc","bdc"
返回:2
思路:圖的方式進行處理,先對現有詞典中的單詞進行排序,遍按照字元長度生成n棵樹,在每棵樹中進行連線,如果兩個給定的字串s和t中只有一個單詞不同的連線在一起,否則不連線。然後輸入一個單詞,對該單詞進行遍歷,當搜尋到另外一個單詞時,輸出路徑。
程式碼如下:
#include<iostream>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int countChanges(vector<string> dic, int n, string s, string t) {
// write code here
map<string,int> mapp;
for(int i=0;i<n;i++)
{
mapp[dic[i]]=1;
}
//寬度優先搜尋
queue<string> q;
q.push(s);
map<string,int> mapped;
mapped[s]=1;
//bool flag=false; //標示是否能變換成功
int cur=1; //當前層待彈出節點數目
int next=0;
int step=0;
while(!q.empty())
{
while(cur--)
{
string str=q.front();
for(int i=0;i<str.size();i++)
{
string ss=str;
for(char c='a';c<='z';c++)
{
ss[i]=c;
if(ss==t)
{
return ++step;
}
if(mapped[ss]==0&&mapp[ss]==1)
{
next++;
mapped[ss]=1;
q.push(ss);
}
}
}
q.pop();
}
cur=next;
next=0;
step++;
}
return -1;
}
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);
}
cout<<countChanges(v, m,"abc","bdc")<<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程式設計師面試
- 智力題(程式設計師面試經典)程式設計師面試