class concordance concordance(text-file) void add-page(page) page-list on-pages(string word) word-list has-words(unsigned page-no)
class query
query({ string, string, ... })
query({ unsigned, unsigned, ... })
unsigned count()
string get-word(unsigned)
unsigned get-pno(unsigned)
class page page(string) unsigned page-no() unsigned size() string get-word(unsigned)
typedef lst<string> word-list typedef vctr<unsigned> page-list
concordance conc(argv[1]) query q while cin >> q // for pno pno ... queries common = conc.has-words(q[0]) for i = 1 to q.size() common ∩= conc.has-words(q[i]) cout << common // similar for word word ... queries
root-word n
The n-neighbors of w are all the words that also appear on each of the pages i - n/2 through i + (n - n/2).
Let w be a word appearing on each of the pages i - n/2 through i + (n - n/2) of a text for non-negative n.
[ (i + n/2) - n/2, (i + n/2) + (n - n/2) ]
[ i, i + n ]
? i i + 1 ... i + nthat is
word-list
n-neighbor(unsigned i, unsigned n)
query q({ i, i + 1, ..., i + n } )
return conc.has-words(q)
word-list
n-neighbors(string w, unsigned n)
word-list nn
page-list pages =
conc.on-pages(query({ w }))
if pages.count() > 0
nn = n-neighbors(pages[0], n)
for i = 1 to pages.count()
nn ∪= n-neighbors(pages[i], n)
return nn
word-list all-words word-list nn = wn-neighbors(w, n) - all-words // add words in nn to the tree all-words = all-words ∪ nn
all-words is
build-tree(word, n, all-words)
queue q(new tree-node(word)
do tnode = q.deq()
nn = wn-neighbors(tnode.w, n) -
all-words
all-words = all-words ∪ nn
for w in nn
c = new tree-node(w)
tnode.add-child(c)
q.enq(c)
while not q.empty()
child_count(), add_child() and
get_child().
class tree-node vtr<tree-node *> children size-t child-count() return children.size() tree-node * get_child(i) return children.get(i) void add_child(tree-node * c) return children.push_back(c)
child_count() is implemented using the list size().
add_child() inserts the new child at the end of the list.
get_child() walks the list, returning the ith element.
|
|
|
|
int main(argc, argv)
concordance conc(argv[1])
query q
while cin >> q
show-tree(
build-tree(
q.word, q.n, word-list(q.word))
|
|
|
| Tests | |||||
| 1 | 2 | 3 | 4 | 5 | |
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 | |||||