国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:127. Word Ladder

2019-11-08 18:28:57
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>#include <string>#include <queue>#include <unordered_map>using namespace std;/*問題:Given two Words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a time.Each transformed word must exist in the word list. Note that beginWord is not a transformed word.For example,Given:beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log","cog"]As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",return its length 5.Note:Return 0 if there is no such transformation sequence.All words have the same length.All words contain only lowercase alphabetic characters.You may assume no duplicates in the word list.You may assume beginWord and endWord are non-empty and are not the same.分析:這是求最短字符串長度。廣度優先搜索用于求解最優問題。可以使用廣度優先搜索,設定每個字符串到初始字符串的距離。根據當前遍歷的字符串,找到其所有變位詞,遍歷變位詞,如果變位詞的距離已經被設置了則跳過(說明該單詞之前被訪問過,不可能在一個里面出現重復的變位詞),如果沒有訪問過,獲取當前單詞距離,設置變位詞的距離=當前單詞距離+1輸入:hit cog 6hot dot dog lot log coghit cog 5hot dot dog lot log輸出:50*/class Solution {public:	vector<string> getOneEditWords(string& word , unordered_map<string,int>& words , unordered_map<string , vector<string> >& wordToNextWords)	{		vector<string> nextWords;		if(word.empty())		{			return nextWords;		}		if(1 == wordToNextWords.count(word))		{			return wordToNextWords[word];		}		int len = word.length();		char temp;		for(int i = 0; i < len ; i++)		{			temp = word.at(i);			for(char ch = 'a' ; ch <= 'z' ; ch++)			{				word.at(i) = ch;				if(1 == words.count(word))				{					nextWords.push_back(word);				}				word.at(i) = temp;			}		}		wordToNextWords[word] = nextWords;		return nextWords;	}	int bfs(string beginWord, string endWord , vector<string>& wordList , 		unordered_map<string,int>& words , unordered_map<string , vector<string> >& wordToNextWords)	{		unordered_map<string ,int> strToDistance;		queue<string> queueWords;		queueWords.push(beginWord);		strToDistance[beginWord] = 0;		vector<string> nextWords;		string word;		string nextWord;		int curDistance = 0;		int size;		bool isFind = false;		while(!queueWords.empty())		{			word = queueWords.front();			queueWords.pop();			nextWords = getOneEditWords(word ,words , wordToNextWords);			if(nextWords.empty())			{				continue;			}			curDistance = strToDistance[word];			size = nextWords.size();			for(int i = 0 ; i < size ; i++)			{				nextWord = nextWords.at(i);				//如果沒有訪問過				if(strToDistance.find(nextWord) == strToDistance.end())				{					strToDistance[nextWord] = curDistance + 1;					queueWords.push(nextWord);					if(nextWord == endWord)					{						isFind = true;					}				}			}			if(isFind)			{				break;			}		}		if(isFind)		{			return (curDistance + 2);//curDistance需要加上從當前字符串到結尾字符串的距離1,以及多1個元素		}		else		{			return 0;		}	}    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {        if(beginWord.empty() || endWord.empty() || wordList.empty())		{			return 0;		}		unordered_map<string , int> words;		int size = wordList.size();		for(int i = 0; i < size ; i++)		{			words[ wordList.at(i) ] = 1;		}		unordered_map<string , vector<string> > wordToNextWords;		int result = bfs(beginWord, endWord , wordList , words , wordToNextWords);		return result;    }};void PRocess(){	 vector<string> nums;	 string value;	 int num;	 Solution solution;	 string beginWord;	 string endWord;	 vector<vector<string> > result;	 while(cin >> beginWord >> endWord >> num )	 {		 nums.clear();		 for(int i = 0 ; i < num ; i++)		 {			 cin >> value;			 nums.push_back(value);		 }		 int distance = solution.ladderLength(beginWord , endWord , nums);		 cout << distance << endl;	 }}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连南| 长春市| 永州市| 岐山县| 绥棱县| 托里县| 宁强县| 阳信县| 通渭县| 台中县| 玛多县| 进贤县| 太仓市| 武川县| 无锡市| 海晏县| 搜索| 四子王旗| 阳曲县| 华蓥市| 陆川县| 资兴市| 团风县| 九江县| 辽中县| 洛扎县| 临颍县| 三门县| 犍为县| 泾源县| 太原市| 台北县| 凤翔县| 安庆市| 迁西县| 乡城县| 玉树县| 陈巴尔虎旗| 梧州市| 射洪县| 隆德县|