【问题标题】:How can find all permutations of spinning text in c#如何在 c# 中找到旋转文本的所有排列
【发布时间】:2011-11-30 18:31:43
【问题描述】:

我有一个旋转文本:{T1{M1|{A1|B1}|M2}F1|{X1|X2}}

我的问题是:如何在 C# 中找到所有排列? T1M1F1 T1M2F1 T1A1F1 T1B1F1 X1 X2

有什么建议吗?

编辑: 感谢您的帮助,但 M1、A1、.. 是示例

用可以给出的词: {我的名字是 james vick,我是这个 {forum|website|site} 上的 {member|user|visitor},我很喜欢它 |我是管理员,我是这个{论坛|网站|网站}的{主管|管理员|版主},我很喜欢它}。

我的名字是 james vick,我是这个 {forum|website|site} 上的 {member|user|visitor},我很喜欢它 => 3 * 3 => 9 个排列

我是管理员,我是这个 {forum|website|site} 上的 {supervisor|admin|moderator},我很喜欢它 => 3 * 3 => 9 种排列方式

结果:1​​8 个排列

【问题讨论】:

    标签: c# text permutation spinning


    【解决方案1】:

    生成所有可旋转字符串排列的方法

    我已经实现了一个简单的方法来解决这个问题。 它接受一个包含可旋转文本字符串的 ArrayList 参数。 我用它来生成多个可旋转字符串的所有排列。

    它带有支持可选块的额外功能,由“[]”括号括起来。

    方程: 如果 ArrayList 中有一个字符串对象,其内容为: {一个 | {B1 | B2 } [B 可选] }

    它用所有排列填充数组列表,“提取” 方法调用后的内容: 一种 B1 B1 B 可选 B2 B2 B 可选

    您还可以传递多个字符串作为参数来为所有字符串生成排列: 例如。: 输入: ArraList 有两个字符串 {A1 | A2} {B1 | B2} 调用后的内容: A1 A2 B1 B2

    此实现的工作原理是始终在第一个可旋转部分中找到最里面的括号对,然后将其提取出来。我一直这样做,直到所有特殊的 {}、[] 字符都被删除。

    private void ExtractVersions(ArrayList list)
        {
            ArrayList IndicesToRemove = new ArrayList();
    
            for (int i = 0; i < list.Count; i++)
            {
                string s = list[i].ToString();
                int firstIndexOfCurlyClosing = s.IndexOf('}');
                int firstIndexOfBracketClosing = s.IndexOf(']');
    
                if ((firstIndexOfCurlyClosing > -1) || (firstIndexOfBracketClosing > -1))
                {
    
                    char type = ' ';
                    int endi = -1;
                    int starti = -1;
    
                    if ((firstIndexOfBracketClosing == -1) && (firstIndexOfCurlyClosing > -1))
                    { // Only Curly
                        endi = firstIndexOfCurlyClosing;
                        type = '{';
                    }
                    else
                    {
                        if ((firstIndexOfBracketClosing > -1) && (firstIndexOfCurlyClosing == -1))
                        { // Only bracket
                            endi = firstIndexOfBracketClosing;
                            type = '[';
                        }
                        else
                        {
                            // Both
                            endi = Math.Min(firstIndexOfBracketClosing, firstIndexOfCurlyClosing);
                            type = s[endi];
    
                            if (type == ']')
                            {
                                type = '[';
                            }
                            else
                            {
                                type = '{';
                            }
                        }
                    }
    
                    starti = s.Substring(0, endi).LastIndexOf(type);
    
                    if (starti == -1)
                    {
                        throw new Exception("Brackets are not valid.");
                    }
                    // start index, end index and type found. -> make changes
                    if (type == '[')
                    {
                        // Add two new lines, one with the optional part, one without it
                        list.Add(s.Remove(starti, endi - starti+1));
                        list.Add(s.Remove(starti, 1).Remove(endi-1, 1));
                        IndicesToRemove.Add(i);
                    }
                    else
                        if (type == '{')
                        {
                            // Add as many new lines as many alternatives there are. This must be an in most bracket.
                            string alternatives = s.Substring(starti + 1, endi - starti - 1);
                            foreach(string alt in alternatives.Split('|'))
                            {
                                list.Add(s.Remove(starti,endi-starti+1).Insert(starti,alt));
                            }
                            IndicesToRemove.Add(i);
                        }
                } // End of if( >-1 && >-1)
            } // End of for loop
    
            for (int i = IndicesToRemove.Count-1; i >= 0; i--)
            {
                list.RemoveAt((int)IndicesToRemove[i]);
            }
        }
    

    我希望我有所帮助。 也许这不是最简单和最好的实现,但对我来说效果很好。请反馈,并投票!

    【讨论】:

    • 干得好。支持可选块的好主意
    【解决方案2】:

    在我看来,你应该这样做:

    1. 所有嵌套的选项列表,即 { } 之间的选项都应该“展平”为单个选项列表。就像你的例子一样:

      {M1|{A1|B1}|M2} -> {M1|A1|B1|M2}

    2. 使用递归生成所有可能的组合。例如,从一个空数组开始,首先放置 T1,因为它是唯一的选项。然后从嵌套列表 {M1|A1|B1|M2} 中依次选择每个元素并将其放置在下一个位置,最后是 F1。重复,直到用尽所有可能性。

    这只是一个粗略的提示,你需要填写其余的细节。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2012-04-19
      • 2019-01-29
      • 2013-01-27
      • 2014-04-28
      • 2011-09-13
      相关资源
      最近更新 更多