Fisher-Yates在单个字符串上随机播放还是使用等长排列? [英] Fisher-Yates shuffle on a single string vs. using an equal length permutation?

查看:88
本文介绍了Fisher-Yates在单个字符串上随机播放还是使用等长排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在开发一套文字游戏,以自学(并重新创建一些我最喜欢的文字游戏!),在一个实际"学习过的编程朋友的帮助下,我们实现了一种不错的排列方法在我的一堂课上.它正在查找3个字母以上的所有排列,并将它们与我所包含的字符串列表进行比较,这些字符串实质上是Scrabble锦标赛单词列表.

这就是背景,这是我当前的问题:我现在拥有所有排列并将它们与现有单词进行比较,并创建了一个新列表,其中包含给定字符串中所有可能的单词组合.但是,当我将此字符串提供给用户时,我需要对其进行加密.我发现了Fisher-Yates随机播放的一些C#实现,但是我无法使它们适应单个字符串(使用char []数组解决的Fisher-Yates问题).然后我想到了一些技巧-为什么不使用长度相同但原始词=!的排列之一.

不幸的是,每当我的条件语句向后返回单词时.对于最终用户来说并不难:)这是我的加扰代码:

// permWords is a Dictionary<int, List<string>>
String strScrambled= "";

        foreach (List<string> listWords in permWords.Values)
        {
            foreach (string word in listWords)
            {
                if (word.Length == strWord.Length && word != strWord)
                {
                    strScrambled = word;
                }

            }
        }

我已经尝试过strScrambled = word + 1,假设第一个与原始不等式的排列是单词向后.但是,在这种情况下,我认为这并不是真正的可行".特别是考虑到它仍然返回相同的后退单词.

对于如何使用char数组解决Fisher-Yates的问题,已经给出了非常有用的答案,但是我仍然很想知道如何最好地使用类似我发布的内容,只是找到一种方法来确保答案不只是简单地向后拼写单词.我对这种方法很感兴趣,因为排列列表已经存在;我想利用它作为我的解决方案.

解决方案

我想您已经有了一个可以进行改组的方法:

void FisherYatesShuffle(char[] elements)
{
    int N = elements.Count;
    for(int i = 0; i<N-1; i++)
    {
        // exchange elements[i] with a random element in i+1 .. N
    }   
}

要做的所有事情都将您的字符串转换为CharArray,然后将结果转换回字符串:

string shuffle(string input)
{
    var arr = input.ToCharArray();
    FisherYatesShuffle(arr);
    return new String(arr);
}

Right now I am working on a suite of word games as a means of teaching myself (and recreating some of my favorite word games!) With the help of an 'actual' studied programming friend, we have implemented a nice permutation method in one of my classes. It is finding all permutations from 3 letters and up and comparing them to Lists of strings I have containing what is essentially the Scrabble tournament word list.

That's the background, here is my current issue: I now have all of the permutations and have compared them to existing words and created a new List with all possible words combinations in a given string. However, when I present this string to the user, I need it to be scrambled. I found a few C# implementations of the Fisher-Yates shuffle but I was unsuccessful in adapting them to accept a single string (EDIT: Fisher-Yates issue solved with a char[] array). Then I had an idea for a little bit of a hack - why not use one of the permutations that is of the same length but is != the original word.

Unfortunately, every time my conditional statement returns the word backwards. Not that hard for the end user to figure out :) Here is my code for scrambling:

// permWords is a Dictionary<int, List<string>>
String strScrambled= "";

        foreach (List<string> listWords in permWords.Values)
        {
            foreach (string word in listWords)
            {
                if (word.Length == strWord.Length && word != strWord)
                {
                    strScrambled = word;
                }

            }
        }

I have tried strScrambled = word + 1 assuming that the first permutation inequal to the original was the word backwards. However, I don't think that really "works" in this context; especially considering it still returns the same backwards word.

There has been a very useful answer given for how to fix my issue with Fisher-Yates using a char array, but I'm still quite curious to know how it might be best to use something like what I've posted, only finding a way to ensure that the answer is not simply the word spelled backwards. I'm intrigued by this method because the list of permutations already exists; I would like to tap into it as my solution.

解决方案

I suppose you already have a method that does the shuffling :

void FisherYatesShuffle(char[] elements)
{
    int N = elements.Count;
    for(int i = 0; i<N-1; i++)
    {
        // exchange elements[i] with a random element in i+1 .. N
    }   
}

All you need to do it convert your string to CharArray and than convert the result back to string:

string shuffle(string input)
{
    var arr = input.ToCharArray();
    FisherYatesShuffle(arr);
    return new String(arr);
}

这篇关于Fisher-Yates在单个字符串上随机播放还是使用等长排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆