UVa Problem 10029 Edit Step Ladders (遞變階梯)

來源:互聯網
上載者:User
// Edit Step Ladders (遞變階梯)// PC/UVa IDs: 110905/10029, Popularity: B, Success rate: low Level: 3// Verdict: Accepted// Submission Date: 2011-09-30// UVa Run Time: 2.108s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// [問題描述]// 遞變(edit step)是指通過增加、減少或改變單詞 x 中的一個字母,使它變成字典中的另一個單詞 y。// 比如將 dig 變為 dog,將 dog 變成 do 都是遞變。遞變階梯(edit step ladder)是一個按字典// 序排列的單詞序列 w1,w2,... ,wn,滿足對於從 1 到 n - 1 的所有 i,單詞 wi 到 w(i+1)// 都是一次遞變。//// 給出一部字典,你要計算其中最長的遞變階梯。//// [輸入]// 輸入檔案是一部字典:每行都是一個由小寫字母構成的單詞。所有單詞按照字典序排列。所有的單詞長度都// 不超過 16,且字典中最多有 25 000 個單詞。//// [輸出]// 輸出一個整數,代表最長的遞變階梯包含的單詞數量。//// [範例輸入]// cat// dig// dog// fig// fin// fine// fog// log// wine//// [範例輸出]// 5//// [解題方法]// 由於輸出需要字典序,若對其進行圖建模,則得到一個有向非循環圖,問題就是在這個有向非循環圖中找最長路// 徑。這個問題也可以歸結為求最長遞增序列(Longest Increasing Subsequence,LIS)的問題。//// 由題意可知,需要求一個遞增的單詞序列,由於給定的輸入是排好序的,故只需考慮當前單詞和已讀入單詞// 之間的關係,設當前讀入的單詞為 W,其長度為 L,則 W 只可能與長度為 L - 1,L,L + 1 的單詞構// 成遞變關係(當 L = 1 時,只能與 L,L + 1 的單詞構成遞變)。且由於輸出字典序的要求,只考慮字典// 序小於 W 的且長度滿足要求的單詞 Y。故可以從單詞 W 出發,構造一個遞變 X,滿足長度要求,並尋找// X 是否在已讀入的單詞中,若存在則取出其最大遞變長度加 1,與 W 的當前遞變長度比較,若大於,則可以// 替換,否則繼續構造下一個遞變單詞,重複此過程,並記錄之中找到的最大遞變長度。以上是利用簡單動態// 規劃思想求 LIS 的演算法,時間複雜度為 O(n^2)。//// 附:看排名列表裡面有很多人的已耗用時間是 0s,估計是不斷嘗試出來的,因為只有一個輸入檔案,只要有// 一個可以通過的程式,則可以通過二分法不斷提交嘗試出這個最後結果來,我也試了一下,UVa 上的測試數// 據字典的最大遞變長度為 45。#include <iostream>#include <string>#include <map>using namespace std;#define MAXN 25000#define MAXLENGTH 16#define INIT_LENGTH 1map < string, int > dict[MAXLENGTH + 1];// 不同長度單詞分開,提高搜尋速度。int steps[MAXN];// 儲存對應單詞最大遞變長度。int exist(int index, string &temp, int step){map < string, int >::iterator it;it = dict[index].find(temp);if (it != dict[index].end())if ((steps[(*it).second] + 1) > step)step = steps[(*it).second] + 1;return step;}int main(int ac, char *av[]){string line, temp;int current = 0;int maximum = 0;// 初始化全部單詞的遞變長度為 1。for (int c = 0; c < MAXN; c++)steps[c] = INIT_LENGTH;while (cin >> line){int index = line.length() - 1;int step = 1;// 構建長度為 L - 1,L,L + 1,字典序小於當前單詞,與當前單詞構成遞變關係的// 單詞,尋找並比較最長遞增序列長度。若當前單詞長度為 1 ,則只需構造長度為 1// 的遞變單詞。if (index > 0){for (int i = 0; i <= index; i++){temp = line;if (i == index || temp[i] >= temp[i + 1]){temp.erase(temp.begin() + i);step = exist(index - 1, temp, step);}temp = line;while (temp[i] > 'a'){temp[i]--;step = exist(index, temp, step);}temp = line;temp.insert(temp.begin() + i, line[i]);while (temp[i] > 'a'){temp[i]--;step = exist(index + 1, temp, step);}}}else{temp = line;while (temp[0] > 'a'){temp[0]--;step = exist(index, temp, step);}}// 擷取最大值。if (step > maximum)maximum = step;// 儲存當前結果。steps[current] = step;dict[index].insert(make_pair < string, int >(line, current));current++;}cout << maximum << endl;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.