How do I get all permutations of xPy?

后端 未结 3 720
小蘑菇
小蘑菇 2021-01-19 05:07

I\'d like to calculate all the permutations of size Y of a set of size X. That is if I had (1,2,3) and want all permutations of size 2, 3P2, it would be (1,2) (1,3) (2,1) (

相关标签:
3条回答
  • 2021-01-19 05:38

    I do not exacly get your question about cryptogram. But if you want to find longest permutation (anagram) of this words in your dictionary you can try his way.

    1. create bitmask of your word. You can probably use 64 bit arithmetics so you can fit almost 3 alpahbets inside.

    a-> first bit, b-> second bit and so on. If you have word in your "ouglg ouyakl" case this mean

     abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnop
     100000100011001000001001000000010000100100000100000000000000000000
    

    (hope i did not missed something) Now you create same bitmasks for your vocabulary.

    And when you chek against vocabulary you just have to do is

     vocabulary & ( ouglg_ouyakl ^ vocabulary)
    

    and this trows 0 if your vocabulary word is from ouglg_ouyakl.

    About permutations

    for each permutation of numbers fom  1-n // that is 1,2 and 2,1
      for(i=0;i<end;i++)
        for(j=i+1;j<end;j++)
          SET[permutation[i]],SET[permutation[j]]
    

    EDIT: prevous soluton was inapropriate for 24P7.

    0 讨论(0)
  • 2021-01-19 05:40

    I've used this library before (note it is C++) in code that needed to do something similar. It has permutations and combinations, with and without repetition. For your problem, this should suffice (untested...):

    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    
    std::vector<int>::iterator first = v.begin(), middle = v.begin() + 2, last = v.end();
    
    do {
        // do stuff with elements in range first...middle (but dont change them)
    } while(next_partial_permutation(first, middle, last));
    
    0 讨论(0)
  • 2021-01-19 05:42

    You can get the combinations by using std::next_permutation() on a vector<bool> of flags. Taking your example of picking 2 elements from (1,2,3), start your vector as (false, true, true). Repeating next_permutation() on this will give you (true, false, true) then (true, true, false), before starting over.

    Since you want permutations not combinations, map each combination to the set of actual elements (e.g. (true, false, true) becomes (1, 3)) and then generate all the permutations of these using next_permutation() again.

    0 讨论(0)
提交回复
热议问题