1. 程式人生 > >Accelerated-c++-置換索引

Accelerated-c++-置換索引

問題

第五章練習題5-1.
設計和實現一個程式以產生一個置換索引。
在一個置換索引當中,每個短語都是以這個短語的每一個單詞作為索引的(類似倒排,內容做索引)。因此,假如有如下的輸入:

The quick brown fox
jumped over the fence
得到如下的輸出:
The quick \t brown fox
jumped over the \t fence
The quick brown \t fox
\t jumped over the fence
jumped \t over the fence
The \t quick brown fox
jumped over \t the fence
\t The quick brown fox

這個輸出有一點摸不著頭腦!

思路

這個題用的是書本給出的思路。
1.產生輪轉集合
The quick brown fox (The是置換索引)
quick brown fox The(quick)
brown fox The quick(brown)
fox The quick brown(fox)
jumped over the fence(jumped)
over the fence jumped(over)
the fence jumped over(the)
fence jumped over the(fence)

2.排序。
對字串進行排序,但是排序的一句是置換索引。
排序之後的結果為:
brown fox The quick(brown)
fence jumped over the(fence)
fox The quick brown(fox)
jumped over the fence(jumped)
over the fence jumped(over)
quick brown fox The(quick)
the fence jumped over(the)
The quick brown fox(The)

3.值置換索引開始輸出,直到當前串的第一個單詞,用\t分割
brown fox \t The quick
fence \t jumped over the
fox \t The quick brown
jumped over the fence \t
over the fence \t jumped
quick brown fox \t The
the fence \t jumped over
The quick brown fox \t