פּערמיוטיישאַנז / אַנאַגראַמז אין אָביעקטיוו- C - איך בין פעלנדיק עפּעס

(קאָד ווייטער וועגן מיין קשיא)

פּער דעם אָנלייגן לויפן קשיא איך געוויינט פּעגאָלאָן ס צוגאַנג צו דזשענערייטינג אַלע מעגלעך פּערמיוטיישאַנז פון אַ גרופּע פון ​​אותיות אין אַ נססטרינג. אָבער, איך פרובירט איצט נישט בלויז צו מאַכן אַ ANAGRAM וואָס איז אַלע פּערמיוטיישאַנז פון דער זעלביקער לענג, אָבער אַלע מעגלעך קאַמבאַניישאַנז (קיין לענג) פון די אותיות אין אַ שטריקל.

וואָלט ווער עס יז וויסן ווי איך ווייַטערדיק קאָד צו באַקומען עס צו טאָן דאָס? דאָס איז ווי: גענעראַטע אַלע פּערמוטאַטיאָנס פון אַלע לענגטס - אָבער (פֿאַר מורא פון זיי וואָס דאַרפֿן ענטפֿערס צו לעקציעס) זיי האָבן נישט לאָזן קאָד. איך האָבן אַ מוסטער וואָס איך געדאַנק וואָלט טאָן דאָס אין די דנאָ פון דעם פּאָסטן ... אָבער עס איז נישט.

דער קאָד, ווי עס איז, דזשענערייץ די , 11151716], hte , het , eth and eht when given THE . וואָס איך דאַרפֿן איז אין די שורות פון: t , h , e , th , ht , te , ער (עטק) אין אַדישאַן צו די אויבן 3 כאַראַקטער קאַמבאַניישאַנז.

ווי וואָלט איך טוישן דעם, ביטע. (פּס: עס זענען צוויי מעטהאָדס אין דעם. איך צוגעגעבן allPermutationsArrayofStrings אין סדר צו באַקומען די רעזולטאַטן ווי סטרינגס, ווי איך וועלן זיי, ניט נאָר אַ מענגע פון ​​אותיות אין אן אנדער מענגע). איך בין אַסומינג די מאַגיש וואָלט פּאַסירן אין pc_next_permutation סייַ ווי סייַ - אָבער איך געדאַנק איך וואָלט דערמאָנען עס.

אין NSArray + Permutation.h

#import 

@interface NSArray(Permutation)
- (NSArray *)allPermutationsArrayofArrays;
- (NSArray *)allPermutationsArrayofStrings;

@end

אין NSArray + Permutation.m:

#define MAX_PERMUTATION_COUNT   20000

NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size);
NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size) 
{
    // slide down the array looking for where we're smaller than the next guy
    NSInteger pos1;
    for (pos1 = size - 1; perm[pos1] >= perm[pos1 + 1] && pos1 > -1; --pos1);

    // if this doesn't occur, we've finished our permutations
    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
    if (pos1 == -1)
        return NULL;

    assert(pos1 >= 0 && pos1 <= size);

    NSInteger pos2;
    // slide down the array looking for a bigger number than what we found before
    for (pos2 = size; perm[pos2] <= perm[pos1] && pos2 > 0; --pos2);

    assert(pos2 >= 0 && pos2 <= size);

    // swap them
    NSInteger tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;

    // now reverse the elements in between by swapping the ends
    for (++pos1, pos2 = size; pos1 < pos2; ++pos1, --pos2) {
        assert(pos1 >= 0 && pos1 <= size);
        assert(pos2 >= 0 && pos2 <= size);

        tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;
    }

    return perm;
}

@implementation NSArray(Permutation)

- (NSArray *)allPermutationsArrayofArrays
{
    NSInteger size = [self count];
    NSInteger *perm = malloc(size * sizeof(NSInteger));

    for (NSInteger idx = 0; idx < size; ++idx)
        perm[idx] = idx;

    NSInteger permutationCount = 0;

    --size;

    NSMutableArray *perms = [NSMutableArray array];

    do {
        NSMutableArray *newPerm = [NSMutableArray array];

        for (NSInteger i = 0; i <= size; ++i)
            [newPerm addObject:[self objectAtIndex:perm[i]]];

        [perms addObject:newPerm];
    } while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
    free(perm);

    return perms;
}

- (NSArray *)allPermutationsArrayofStrings
{
    NSInteger size = [self count];
    NSInteger *perm = malloc(size * sizeof(NSInteger));

    for (NSInteger idx = 0; idx < size; ++idx)
        perm[idx] = idx;

    NSInteger permutationCount = 0;

    --size;

    NSMutableArray *perms = [NSMutableArray array];

    do {
        NSMutableString *newPerm = [[[NSMutableString alloc]initWithString:@"" ]autorelease];

        for (NSInteger i = 0; i <= size; ++i)
        {
            [newPerm appendString:[self objectAtIndex:perm[i]]];
        }
        [perms addObject:newPerm];
    } while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
    free(perm);

    return perms;
}

@end

מייַן קאָד אַז איך געדאַנק איך וואָלט פאַרריכטן דעם:

for ( NSInteger i = 1; i <= theCount; i++) {
                NSRange theRange2;
                theRange2.location = 0;
                theRange2.length = i;
                NSLog(@"Location: %i (len: %i) is: '%@'",theRange2.location,theRange2.length,[array subarrayWithRange:theRange2]);

                NSArray *allWordsForThisLength = [[array subarrayWithRange:theRange2] allPermutationsArrayofStrings];
                for (NSMutableString *theString in allWordsForThisLength)
                {
                    NSLog(@"Adding %@ as a possible word",theString);
                    [allWords addObject:theString];
                }

איך וויסן עס וואָלט נישט זיין די מערסט עפעקטיוו .. אָבער איך געפרוווט צו פּרובירן.

דאָס איז וואָס איך גאַט:

2011-07-07 14:02:19.684 TA[63623:207] Total letters in word: 3
2011-07-07 14:02:19.685 TA[63623:207] Location: 0 (len: 1) is: '(
    t
)'
2011-07-07 14:02:19.685 TA[63623:207] Adding t as a possible word
2011-07-07 14:02:19.686 TA[63623:207] Location: 0 (len: 2) is: '(
    t,
    h
)'
2011-07-07 14:02:19.686 TA[63623:207] Adding th as a possible word
2011-07-07 14:02:19.687 TA[63623:207] Adding ht as a possible word
2011-07-07 14:02:19.688 TA[63623:207] Location: 0 (len: 3) is: '(
    t,
    h,
    e
)'
2011-07-07 14:02:19.688 TA[63623:207] Adding the as a possible word
2011-07-07 14:02:19.689 TA[63623:207] Adding teh as a possible word
2011-07-07 14:02:19.690 TA[63623:207] Adding hte as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding het as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding eth as a possible word
2011-07-07 14:02:19.692 TA[63623:207] Adding eht as a possible word

ווי איר קענען זען, קיין איין אָדער צוויי בריוו ווערטער - איך ציען מיין האָר! (און איך טאָן ניט האָבן פיל צו ספּער!)

7
задан Community 23 May 2017 в 12:12
поделиться