字符串连接与所有可能性 [英] string concatenation with all possibilities
问题描述
大家好,
string pattren = INC | TRUST | COMPANY | 401K;
string [] ArrPattern = pattren.Split(' |');
在字符串数组之上我需要连接下面输出的所有可能性。
ex:
INC PLAN COMPANY 401K
INC PLAN COMPANY空
INC计划空401k
INC计划空空
INC空公司401k
INC空空401k
INC空空空
空计划公司401k
...
...
...
喜欢这16种组合。
请分享您的想法对我来说非常紧急。
问候,
Ram
我不确定,但看看关于排列的这个主题。
起初格兰ce,它看起来很简单。
1)计算可能组合的数量int 组合= 2 << ArrPattern.Length;
2)使用循环组合
获取所有组合
3)在循环中,让各个二进制数字决定您是使用实际单词还是空字符串。for ( int i = 0 ; i < 组合; i ++)
{
StringBuilder singleVariant = new StringBuilder();
for ( int stringIndex = 0 ; stringIndex < ArrPattern.Length; stringIndex ++)
{
if ((stringIndex& i)!= 0 )
{
singleVariant.Append(ArrPattern [stringIndex]);
}
else
{
singleVariant.Append( string 跨度> .empty);
}
}
}
4)添加错误处理,空间填充,错误修复,输入,输出等
Hi All,
string pattren = " INC | TRUST | COMPANY | 401K ";
string[] ArrPattern = pattren.Split('|');
Above the string array i need to concatenate all the posibilities like below output.
ex:
INC PLAN COMPANY 401K
INC PLAN COMPANY empty
INC PLAN empty 401k
INC PLAN empty empty
INC empty COMPANY 401k
INC empty empty 401k
INC empty empty empty
empty PLAN COMPANY 401k
...
...
...
Like these 16 combination.
Please share your ideas its very urgent for me.
Regards,
Ram
I am not sure, but take a look at this thread about permutations.
At first glance, it looks simple to me.
1) Compute number of possible combinationsint combinations = 2 << ArrPattern.Length;
2) Use a loop tocombinations
to get all combinations
3) Within the loop, let the individual binary digits decide whether you use an actual word or an empty string.for ( int i = 0; i < combinations; i++) { StringBuilder singleVariant = new StringBuilder(); for( int stringIndex = 0; stringIndex < ArrPattern.Length; stringIndex++) { if( (stringIndex & i) != 0) { singleVariant.Append(ArrPattern[stringIndex]); } else { singleVariant.Append(string.empty); } } }
4) Add error handling, space stuffing, bug fixing, input, output, etc
这篇关于字符串连接与所有可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!