Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like what you really want is something like Levenshtein distance (but not exactly that). Here's a first cut.</p> <p>What it does is walk the game tree of all possible rearrangements of <em>a</em> to see if they match <em>b</em>. It associates a cost with each rearrangement, expressed as a declining <em>budget</em>.</p> <p>The outer loop walks first with a budget of 0, so only an exact match is allowed.</p> <p>If it got no success, then it walks with a budget of 1, finding all matches containing only one rearrangement.</p> <p>If it got no success, then it walks with a budget of 2, and so on.</p> <p>As it matches, it keeps an array of integers <em>delta</em>, telling how far each element of <em>a</em> has been swapped. Whenever it gets a success, it somwhow prints out that delta array, and that is your record of what swaps were done to get that match.</p> <pre><code>void walk(char* a, char* b, int* delta, int budget, int&amp; nSuccess){ delta[0] = 0; if (budget &lt; 0) return; if (a[0] == '\0' &amp;&amp; b[0] == '\0'){ // end of both strings nSuccess++; // print out the deltas return; } if (a[0] == '\0') return; // excess chars in b if (b[0] == '\0') return; // excess chars in a if (a[0] == b[0]){ // first chars are equal, move to next walk(a+1, b+1, delta+1, budget, nSuccess); return; } for (int i = 1; a[i] != '\0'; i++){ delta[0] = i; swap(a[0], a[i]); if (a[0] == b[0]){ walk(a+1, b+1, delta+1, budget-1, nSuccess); } swap(a[0], a[i]); delta[0] = 0; } } void top(char* a, char* b){ int nSuccess = 0; int delta[512]; for (int budget = 0; nSuccess==0; budget++){ walk(a, b, budget, delta, nSuccess); } } </code></pre> <p>The performance of the algorithm is exponential in N, where N is the minimum number of rearrangements needed to make the strings match. So it probably should not be used until after you've verified that each strings has the same number of each character, and only use it if you need to see that record of rearrangements.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload