简体   繁体   中英

Porting c code to actionscript 2

please look at the following link. Permutation of String letters: How to remove repeated permutations?

I would like to port this to actionscript 2. Here is what i have so far:

var str:String = "AAC";
var arr:Array = str.split("");

permute(0,2);

function swap(i:Number, j:Number)
{
    var temp;
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

function permute(i:Number, n:Number)
{
   var k:Number;

   if (i == n)
     trace(arr);
   else
   {
        for (k = i; k <= n; k++)
       {
              swap(i, k);
              permute(i+1, n);
              swap(i, k);           
       }
   }
} 

This is working fine to list all permutations with duplicates, but i would like to remove those and have unique values. So far i haven't managed to port the ticked solution from the above link. Thank You for your help.

Below are two versions, the first is for AS3, the second is for AS2. It is a slightly different algorithm (it is a bit less efficient then the one you have, but I think it will do as an illustration). It is essentially the same algorithmic complexity, so it's OK, the redundancy is in generating intermediate results (shorter arrays), which are later discarded (but you could modify it to reuse those arrays, if this concerns you).

AS3

private function permuteRecursively(input:Array, 
    hash:Object = null):Array
{
    var first:String;
    var oldResult:Array;
    var newResult:Array;
    var times:int;
    var current:Array;
    var test:String;

    if (input.length > 1)
    {
        first = input.shift();
        if (!hash) hash = { };
        oldResult = this.permuteRecursively(input, hash);
        newResult = [];
        for each (var result:Array in oldResult)
        {
            times = result.length;
            while (times >= 0)
            {
                current = result.concat();
                if (times == result.length) current.push(first);
                else current.splice(times, 0, first);
                test = current.join("");
                if (!(test in hash))
                {
                    newResult.push(current);
                    hash[test] = 1;
                }
                times--;
            }
        }
    }
    else newResult = [input];
    return newResult;
}

AS2:

private function permuteRecursively(input:Array, 
    hash:Object):Array
{
    var first:String;
    var oldResult:Array;
    var newResult:Array;
    var times:Number;
    var current:Array;
    var test:String;
    var result:Array;

    if (input.length > 1)
    {
        first = input.shift();
        if (!hash) hash = { };
        oldResult = this.permuteRecursively(input, hash);
        newResult = [];
        for (var i:Number = 0; i < oldResult.length; i++)
        {
            result = oldResult[i];
            times = result.length;
            while (times >= 0)
            {
                current = result.concat();
                if (times == result.length) current.push(first);
                else current.splice(times, 0, first);
                test = current.join("");
                if (!(test in hash))
                {
                    newResult.push(current);
                    hash[test] = 1;
                }
                times--;
            }
        }
    }
    else newResult = [input];
    return newResult;
}

EDIT:

Actually, now that I think of it, I'm not sure what kind of duplicates you were trying to avoid. The above code treats the permutations of AAC such as AAC and ACA as if they were distinct (even though A is "duplicated" in them), but AAC and AAC are considered the same (even though the first A and the second A may come from different sources in the original array).

If what you wanted to remove duplicated elements of generated arrays, then, obviously, the best strategy would be to remove them from the source first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM