UVa Problem 10150 Doublets (Doublets 序列)

來源:互聯網
上載者:User
// Doublets (Doublets 序列)// PC/UVa IDs: 110307/10150, Popularity: C, Success rate: average Level: 3// Verdict: Accepted// Submission Date: 2011-05-20// UVa Run Time: 0.200s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// 此題目可以轉換為求無權圖的最短路問題,可以用 BFS 來實現。由於最長的單詞不超過 16 個字母,故可// 將單詞按長度分為 16 種類別,兩個不同長度的單詞不會構成 Doublets,這樣每種類別平均單詞個數大// 概為 25143/16,同時只在需要時才構建相應長度單詞的圖表示。使用map或二分尋找(先排序)來取得單// 詞在數組中的序號可能會獲得更好的 RT。本程式在 Programming Challenges 上結果為 WA。在 UVa// 上為 AC。#include <iostream>#include <sstream>#include <vector>#include <iterator>#include <algorithm>#include <queue>#include <cstring>using namespace std;#define MAXSIZE 25143// 字典最大數量為25143。// 判斷兩個單詞是否組成一對 Doublets。bool doublets(string &a, string &b){// 長度不等,不會是 Doublets。if (a.length() != b.length())return false;// 尋找兩個單詞不同的個數。int diff = 0;for (int i = 0; i < a.length(); i++)if (a[i] != b[i]){diff++;if (diff > 1)return false;}return diff == 1;}void no_solution(){cout << "No solution." << endl;}void find_path(vector < string > &points, int parent[MAXSIZE], int x, int y){// 根據 parent 數組回溯得到最短路。從終點 y 往回找,如果父節點為 -1 則表示未找到,如// 果為起點x則輸出此條路徑。vector < int > path;while (parent[y] != x && parent[y] != -1){path.push_back(y);y = parent[y];}if (parent[y] == -1)no_solution();else{path.push_back(y);path.push_back(x);for (int m = path.size() - 1; m >= 0; m--)cout << points[path[m]] << endl;}}// 使用 BFS(寬度優先搜尋)尋找最短路並輸出。void bfs(vector < vector < int > > &edges, int parent[MAXSIZE], int x, int y){// 尋找序號為 x 到序號為 y 的單詞間的最短路徑。bool discovered[MAXSIZE];queue < int > q;int v;memset(discovered, 0, sizeof(discovered));memset(parent, -1, sizeof(parent));q.push(x);discovered[x] = true;// BFS 遍曆圖直到遍曆完畢或找到序號為 y 的單詞。bool finish = false;while (q.size()){v = q.front();q.pop();for (int i = 0; i < edges[v].size(); i++){if (!discovered[edges[v][i]]){q.push(edges[v][i]);discovered[edges[v][i]] = true;parent[edges[v][i]] = v;if (edges[v][i] == y){finish = true;break;}}}if (finish)return;}}int main(int ac, char *av[]){vector < vector < int > > edges[16];vector < string > points[16];bool inited[16];string line;int m, n;memset(inited, false, sizeof(inited));// 讀入單詞字典資料。while (getline(cin, line), line.length() > 0){m = line.length() - 1;points[m].push_back(line);}int cases = 0;while (cases++, getline(cin, line)){istringstream iss(line);string a, b;iss >> a >> b;// 輸出空行。if (cases > 1)cout << endl;// 若單詞長度不等,無解。if (a.size() != b.size()){no_solution();continue;}// 找到單詞 a 和 b 在圖中的序數。int x = -1, y = -1;int m = a.length() - 1;// 在圖中找到單詞 a 和 b 的序號。for (int i = 0; i < points[m].size(); i++){    if (x == -1 && a == points[m][i])        x = i;    if (y == -1 && b == points[m][i])        y = i;    if (x != -1 && y != -1)        break;}// 未找到。if (x == -1 || y == -1)no_solution();// 找到,但是單詞長度均為 1。else if (a.length() == 1 && b.length() == 1){// 長度均為 1,但兩者不同,直接輸出。if (a != b)cout << a << endl << b << endl;else{// 因為所有單詞互不相同,故只要找到一個單詞,它與x和y均不同即可。// 當然必須單詞數大於 2 個。if (points[0].size() > 2){// 因為所有單詞互不相同,故只要找到一個序號,它與//  x 和 y 均不同即可。當然必須單詞數大於 1 個。cout << a << endl;for (int i = 0; i < points[0].size(); i++)if (i != x && i != y){cout << points[0][i] << endl;break;}cout << b << endl;}elseno_solution();}}else{// 單詞長度為(m + 1)的圖尚未構建,則構建。if (!inited[m]){edges[m].resize(points[m].size());for (int i = 0; i < points[m].size() - 1; i++)for (int j = (i + 1); j < points[m].size(); j++)if (doublets(points[m][i], points[m][j])){edges[m][i].push_back(j);edges[m][j].push_back(i);}inited[m] = true;}// 兩者長度不為 1,但兩個單詞相同,則只需要查看該單詞是否有 Doublets,// 有則將單詞作為中間輸出。if (x == y){if (edges[m][x].size()){cout << a << endl;cout << points[m][edges[m][x][0]] << endl;cout << b << endl;}elseno_solution();}else{int parent[MAXSIZE];// 使用 BFS(寬度優先搜尋)尋找最短路並輸出。bfs(edges[m], parent, x, y);find_path(points[m], parent, x, y);}}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.