问题描述
我有一个旋转的文本:{T1 {M1 | {A1 | B1} | M2} F1 | {X1 | X2}}
I have a spinning text : {T1{M1|{A1|B1}|M2}F1|{X1|X2}}
我的问题是:如何找到C#中的所有排列?T1M1F1T1M2F1T1A1F1T1B1F1X1X2
My question is : How can i find all permutations in C# ?T1M1F1T1M2F1T1A1F1T1B1F1X1X2
有什么建议吗?
谢谢您的帮助,但M1,A1,..是示例
Edit :Thank you for your help but M1,A1, .. are examples
用可能给出的词: {我的名字是james vick,我是这个{forum | website | site}上的{member | user | visitor},我很喜欢|我是管理员,我是这个{forum | website | site}上的{supervisor | admin |主持人},我也很喜欢}.
我的名字是james vick,我是这个{forum | website | site}上的{member | user | visitor},我喜欢它 => 3 * 3 => 9个排列
my name is james vick and i am a {member|user|visitor} on this {forum|website|site} and i am loving it => 3 * 3 => 9 permutations
我是管理员,我是这个{forum | website | site}上的{supervisor | admin |主持人},我喜欢它 => 3 * 3 => 9个排列
i am admin and i am a {supervisor|admin|moderator} on this {forum|website|site} and i am loving it => 3 * 3 => 9 permutations
结果:18个排列
推荐答案
生成可旋转字符串的所有置换的方法
我已经实现了一种简单的方法来解决此问题.它需要一个包含可旋转文本字符串的ArrayList参数.我用它来生成多个可旋转字符串的所有排列.
I've implemented a simple method to solve this problem.It takes an ArrayList argument containing spinnable text string(s).I use it to generate all the permutations of multiple spinnable strings.
它具有支持可选块的额外功能,并由"[]"括号取代.
It comes with extra functionality of support of optional blocks, surronded by "[ ]" brackets.
等式:如果ArrayList中有一个字符串对象,其内容为:{A | {B1 | B2} [B可选]}
Eq.:If you have a single string object in the ArrayList with content of:{A | {B1 | B2 } [B optional] }
它使用提取"的所有排列填充数组列表方法调用后的内容:一种B1B1 B可选B2B2 B可选
It populates the array list with all the permutations, "extracted"Contents after invocation of method:AB1B1 B optionalB2B2 B optional
您还可以将多个字符串作为参数传递,以生成所有字符串的排列:例如.:输入:带两个字符串的ArraList{A1 | A2}{B1 | B2}调用后的内容:A1A2B1B2
You can also pass multiple strings as argument to generate permutations for all of them:Eg.:Input:ArraList with two string{A1 | A2}{B1 | B2}Contents after invocation:A1A2B1B2
此实现的工作原理是始终在第一个可旋转部分中找到最里面的括号对,然后将其提取.直到所有特殊的{},[]字符都被删除为止.
This implementation works by always finding the inner most bracket pair in the first spinnable section, then extract it. I do this until all the special {}, [] characters are removed.
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]);
}
}
我希望能有所帮助.也许这不是最简单,最好的实现,但对我来说效果很好.请反馈并投票!
I hope I've helped.Maybe it is not the simplest and best implementation, but it works well for me. Please feedback, and vote!
这篇关于如何在C#中找到旋转文本的所有排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!