字符串连接与所有可能性 [英] string concatenation with all possibilities

查看:80
本文介绍了字符串连接与所有可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

  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 combinations

int combinations = 2 << ArrPattern.Length;


2) Use a loop to combinations 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屋!

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