Objective c 目标C-重复项的置换计算

Objective c 目标C-重复项的置换计算,objective-c,recursion,permutation,Objective C,Recursion,Permutation,我似乎真的想不出一个办法来解决这个问题,因为某种原因我的大脑无法思考。我试图解决的问题是: 对于谜题解算器类型的算法,我将重复的字母作为NSString的子字符串。假设用户输入“rbeeeioouu” 我只是从字符串中提取重复项,并希望看到这些重复项的所有可能组合,而不是在字符串中的位置,而是通过改变重复计数。。 (对于这个问题,位置无关紧要,我稍后按字母顺序排列。) 所以,给定字符串EEEOOUUU, 我想推导出一组所有可能的字符串组合,基于基本上不同的重复项 在本例中,所有可能的字符串都包含

我似乎真的想不出一个办法来解决这个问题,因为某种原因我的大脑无法思考。我试图解决的问题是:

对于谜题解算器类型的算法,我将重复的字母作为NSString的子字符串。假设用户输入“rbeeeioouu” 我只是从字符串中提取重复项,并希望看到这些重复项的所有可能组合,而不是在字符串中的位置,而是通过改变重复计数。。 (对于这个问题,位置无关紧要,我稍后按字母顺序排列。)

所以,给定字符串EEEOOUUU, 我想推导出一组所有可能的字符串组合,基于基本上不同的重复项

在本例中,所有可能的字符串都包含3个或更少的E、2个或更少的O和3个或更少的U

所以,我想马上回来

EEEOOUUU(源字符串) 伊欧乌 伊欧欧乌 伊欧欧

伊欧乌 伊乌乌 伊欧乌 伊乌 EEOU 伊欧乌乌 ....... 等等

递归在这个问题上占有一席之地,因为某些原因,我甚至无法想象解决方案。 我有固定长度字母集合排列的算法,但这在这里没有帮助,或者至少我睡眠不足的大脑无法应用它们

任何慷慨的人愿意提供的帮助或建议,我都会感激你

关于对话的话题,我刚刚砍掉了一些不好的东西,这些东西可能会被扔掉,以代替一些东西。。。。当然,这假设了一些调用,我会在以后只返回重复的调用,等等。作为数组的使用,我对此并不感到兴奋,但它不会在一堆条目中重复

//probably the most inefficent way of doign this, sorry for my level of fail.
-(NSMutableArray*) getDuplicatePermutations:(NSMutableArray*)workingArray workingWord:(NSMutableString*)workingWord currentLetter:(NSString*)currentLetter{
    NSArray *dupeArray = [self getDuplicateLetters];  //this returns only the letters with an occurrence >1 so in EEEIIOOOT, it returns "E", I", "O"
    if (workingWord==nil){workingWord = [[NSMutableString alloc] init];
        for (NSString *thisLetter in dupeArray)
        {
            for (NSString* thisLetterStuff in [self possibleDupePermutationsForLetter:thisLetter theWord:self]){  ////this thing returns NSArray of NSStrings like so "EEE","EE", "E"
                workingWord = [workingWord stringByAppendingString:thisLetterStuff];
                if ([thisLetter isEqualToString:[dupeArray lastObject]]) { ///do... something.. at the lowest depth...
                    [workingArray addObject:workingWord];
                    workingWord = @"";
                }
                workingArray = [self getDuplicatePermutations:workingArray workingWord:workingWord currentLetter:thisLetter];  //I can haz recursion?  No idea where to do it,actually.
            }
        }

    }
    return workingArray;    ///ostensibly an array filled with crap the looping mess builds
}

如果我理解您的要求,这可以通过嵌套循环实现。在伪代码中:

for e-count in 1 to 3:
    for o-count in 1 to 2
        for u-count in 1 to 3:
            create string with e-count e's, o-count o's and u-count u's

如果我理解您的要求,这可以通过嵌套循环实现。在伪代码中:

for e-count in 1 to 3:
    for o-count in 1 to 2
        for u-count in 1 to 3:
            create string with e-count e's, o-count o's and u-count u's

嗯,我找到了一种有效的方法。。我觉得可能有人会敲我的门,拿走我的驾驶执照和电脑。。但这是

解决办法是这样的

对于源单词中重复的每个字母 构建一个“重复映射”,它本质上是一个字典,具有字母键和出现计数值”

用这个字母为单词部分添加种子(字母*它的出现次数-迭代)

对于字典中的每一个其他字母,迭代通过,从这些字母的可能迭代中删除一个构建wordparts的出现次数

在外循环的每次迭代之后,重新创建复制映射并再次执行

(显然,这包含实例上的一些其他方法,它们也在执行实用任务) 但我至少想和大家分享,即使现在情况很糟糕,可能有漏洞

-(NSMutableArray*) DoCrazyCalculationsForDuplicatePermutations
{
    NSMutableArray *resArray = [[NSMutableArray alloc] init];
    NSArray *myDupes = [self getDuplicateLetters];
    for (NSString *thisLetter in myDupes){
        NSMutableDictionary *myDupeMap = [self getDupeMap];
        NSArray *keys = [myDupeMap allKeys];
        NSMutableString *wordSeed = [[NSMutableString alloc] init];
        NSNumber *seedNumberFromDictionary = [myDupeMap objectForKey:thisLetter];
        unsigned seedLettersToMake = [seedNumberFromDictionary intValue];
        [myDupeMap setValue:[NSNumber numberWithInt:1] forKey:thisLetter];  //1 Out the entry for this primary letter because we will handle it as part of the outer loop  -- also prevents 1 infinite loop below
        unsigned w = 0;
        for (w=1; w <= seedLettersToMake; w++) {
            myDupeMap = [self getDupeMap];  //reset the dupe map...
            [wordSeed appendString:[self getLettersByLetterAndCount:thisLetter count:1]];  //I will be appended inside the loop, per word;
            unsigned dupeTotals = [myDupeMap myTotals];
            while (dupeTotals >= ([myDupeMap count])) {
                NSMutableString *thisWord = [[NSMutableString alloc] init];
                [thisWord appendString:wordSeed];
                for (NSString *thisKey in keys){
                    if(![thisKey isEqualToString:thisLetter]){
                        NSNumber *numberFromDictionary = [myDupeMap objectForKey:thisKey];
                        unsigned lettersToMake = [numberFromDictionary intValue];//how many for this letter? 
                        [thisWord appendString:[self getLettersByLetterAndCount:thisKey count:lettersToMake]];
                        if (lettersToMake > 1){
                            unsigned o = lettersToMake - 1;
                            [myDupeMap setValue:[NSNumber numberWithInt:o] forKey:thisKey];
                            dupeTotals = [myDupeMap myTotals];
                        }
                    }
                }
                if (([thisWord length]-(w-1)) == ([myDupeMap count])) {dupeTotals=.5;} //break out
                NSLog(@"CrazyDuplicateProcessing: %@",[thisWord alphabetized]);
                [resArray addObject:thisWord];
            }
        }
    }
    return resArray;
}
-(NSMutableArray*)DocRazyCalculations用于双重置换
{
NSMutableArray*resArray=[[NSMutableArray alloc]init];
NSArray*myDupes=[self-getDuplicateLetters];
for(NSString*此字母在MyDups中){
NSMutableDictionary*myDupeMap=[self-getDupeMap];
NSArray*keys=[myDupeMap allKeys];
NSMutableString*wordSeed=[[NSMutableString alloc]init];
NSNumber*seedNumberFromDictionary=[MyDupeMapObjectForkey:thisLetter];
无符号SeedLetterToMake=[seedNumberFromDictionary intValue];
[myDupeMap setValue:[NSNumber numberWithInt:1]forKey:thisLetter];//1请将此主字母的条目输出,因为我们将将其作为外部循环的一部分进行处理--还可以防止下面的无限循环
无符号w=0;
对于(w=1;w=([myDupeMap计数]){
NSMutableString*thisWord=[[NSMutableString alloc]init];
[thisWord appendString:wordSeed];
for(NSString*密钥中的此密钥){
如果(![thisKey IsequalString:thisLetter]){
NSNumber*numberFromDictionary=[myDupeMap objectForKey:thisKey];
unsigned lettermoke=[numberFromDictionary intValue];//此字母有多少个字符?
[thisWord appendString:[self-GetLettersByleterAndCount:thisKey count:LetterToMake];
如果(字母气孔>1){
无符号o=字母气孔-1;
[myDupeMap设置值:[NSNumber numberWithInt:o]forKey:thisKey];
dupeTotals=[myDupeMap myTotals];
}
}
}
如果(([thisWord length]-(w-1))==([myDupeMap count]){dupeTotals=.5;}//中断
NSLog(@“CrazyDuplicateProcessing:%@,[这个词按字母顺序排列]);
[重新排列添加对象:thisWord];
}
}
}
返回重新排列;
}

好吧,我找到了一个可行的方法。我觉得可能有人会敲我的门,拿走我的驾驶执照和电脑。但这是

解决办法是这样的

对于源单词中重复的每个字母 构建一个“重复映射”,它本质上是一个字典,具有字母键和出现计数值”

用这个字母为单词部分添加种子(字母*它的出现次数-迭代)

对于字典中的每一个其他字母,迭代通过,从这些字母的可能迭代中删除一个构建wordparts的出现次数

在外循环的每次迭代之后,重新创建复制映射并再次执行

(显然,这包含实例上的一些其他方法,它们也在执行实用任务) 但我至少想和大家分享,即使现在情况很糟糕,可能有漏洞

-(NSMutableArray*) DoCrazyCalculationsForDuplicatePermutations
{
    NSMutableArray *resArray = [[NSMutableArray alloc] init];
    NSArray *myDupes = [self getDuplicateLetters];
    for (NSString *thisLetter in myDupes){
        NSMutableDictionary *myDupeMap = [self getDupeMap];
        NSArray *keys = [myDupeMap allKeys];
        NSMutableString *wordSeed = [[NSMutableString alloc] init];
        NSNumber *seedNumberFromDictionary = [myDupeMap objectForKey:thisLetter];
        unsigned seedLettersToMake = [seedNumberFromDictionary intValue];
        [myDupeMap setValue:[NSNumber numberWithInt:1] forKey:thisLetter];  //1 Out the entry for this primary letter because we will handle it as part of the outer loop  -- also prevents 1 infinite loop below
        unsigned w = 0;
        for (w=1; w <= seedLettersToMake; w++) {
            myDupeMap = [self getDupeMap];  //reset the dupe map...
            [wordSeed appendString:[self getLettersByLetterAndCount:thisLetter count:1]];  //I will be appended inside the loop, per word;
            unsigned dupeTotals = [myDupeMap myTotals];
            while (dupeTotals >= ([myDupeMap count])) {
                NSMutableString *thisWord = [[NSMutableString alloc] init];
                [thisWord appendString:wordSeed];
                for (NSString *thisKey in keys){
                    if(![thisKey isEqualToString:thisLetter]){
                        NSNumber *numberFromDictionary = [myDupeMap objectForKey:thisKey];
                        unsigned lettersToMake = [numberFromDictionary intValue];//how many for this letter? 
                        [thisWord appendString:[self getLettersByLetterAndCount:thisKey count:lettersToMake]];
                        if (lettersToMake > 1){
                            unsigned o = lettersToMake - 1;
                            [myDupeMap setValue:[NSNumber numberWithInt:o] forKey:thisKey];
                            dupeTotals = [myDupeMap myTotals];
                        }
                    }
                }
                if (([thisWord length]-(w-1)) == ([myDupeMap count])) {dupeTotals=.5;} //break out
                NSLog(@"CrazyDuplicateProcessing: %@",[thisWord alphabetized]);
                [resArray addObject:thisWord];
            }
        }
    }
    return resArray;
}
-(NSMutableArray*)DocRazyCalculations用于双重置换
{
NSMutableArray*resArray=[[NSMutableArray alloc]init];
NSArray*myDupes=[self-getDuplicateLette