B
B
Bogdan11112016-12-07 03:39:12
Programming
Bogdan1111, 2016-12-07 03:39:12

How to work with string matrices?

I can’t figure out how to check for each row of matrix A whether it is possible to get a sequence of characters written in the rows of matrix B by permuting its characters.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-12-07
@Bogdan1111

The simplest way is to sort each row of both matrices. Lines that were originally anagrams will match, and now you can find out what matches what. Complexity O(w (h A +h B )max{h A +h B , log w})
More complex (better asymptotic estimate). After each row has been sorted, the rows of matrix B are additionally reordered in lexicographic order (AAA < AAB <...< AAA < ABA). We use binary search to find matches. The complexity is O(w (h A +h B ) log max{w, h A , h B }).
You can also calculate the hashes for each row of the matrix B and store it in some data structure: the hash did not match anything - a full comparison is not carried out. Doesn't improve grades in the worst case, but improves on average.
Oftop. There was a case, I optimized Doom's WADs in size (the format allowed several blocks to refer to the same section of the file, and in many mods - and even in Doom itself - there were repetitions). I managed with hashing, and the hash was the size + a few bytes from the middle of the block.

B
Bogdan1111, 2016-12-07
@Bogdan1111

Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question