求字符串生成4位字符数组的算法

chenhaoying 2011-06-25 10:32:53
输入个长度大于或等于4的字符串:12345
随机抽取4位生成字符串数组,如:1234,1235,1324……等120个4位字符串。
如输入1234,则生成1234,1324……等24个4位字符串;
如输入1233,则生成1233,1323……等12个4位字符串。
大家给高效率的算法。
...全文
201 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
vrhero 2011-06-26
  • 打赏
  • 举报
回复
你这不叫什么随机抽取,这叫全排列算法...
threenewbee 2011-06-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "12345";
List<string> list = new List<string>();
foreach (var i in Combo(s, 4))
{
list = list.Union(Arrange(i)).ToList();
}
Console.WriteLine(list.Count);
//list.ForEach(x => Console.WriteLine(x));
}


static IEnumerable<string> Arrange(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (source.Length == 1)
{
yield return source;
}
else
{
foreach (var x in Arrange(source.Substring(0, i) + source.Substring(i + 1)))
{
yield return source[i] + x;
}
}
}
}

static IEnumerable<string> Combo(string source, int len)
{
int[] pos = new int[len];
for (int i = 0; i < len; i++) pos[i] = i;
while (pos[0] < source.Length - len)
{
string str = "";
for (int i = 0; i < len; i++) str += source[pos[i]];
for (int i = len - 1; i >= 0; i--)
{
if (pos[i] < source.Length - len + i)
{
pos[i]++;
for (int j = i + 1; j <= len - 1; j++)
{
pos[j] = pos[i] + j - i;
}
break;
}
else
{
continue;
}
}
yield return str;
}
yield return source.Substring(source.Length - len);
}
}
}
md5e 2011-06-26
  • 打赏
  • 举报
回复
这个是去除重复的
如输入1233,则生成1233,1323……等12个4位字符串。


using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Galsun.Nfdw.Web
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
int[] ary = new int[] { 0, 1, 2, 2 };
ary.CopyTo(ary, 0);
int s = 3;
string num = "1233";
char[] ary_num = num.ToCharArray();
int n = ary_num.Length - 1;
int cout = 0;
string txt = string.Empty;
while (s >= 0)
{

loop: if (ary[s] >= n)
{

ary[s] = -1;
s = s - 1;

}
else
{
ary[s] = ary[s] + 1;
for (int i = s - 1; i >= 0; i--)
{
if (ary[s] == ary[i])
{
goto loop;
}
}
if (s == 3 && !txt.Contains(string.Format("{0}{1}{2}{3}", ary_num[ary[0]], ary_num[ary[1]], ary_num[ary[2]], ary_num[ary[3]])))
{
cout++;
txt = txt + "," + string.Format("{0}{1}{2}{3}", ary_num[ary[0]], ary_num[ary[1]], ary_num[ary[2]], ary_num[ary[3]]);
Response.Write(string.Format("{0}{1}{2}{3}<br/>", ary_num[ary[0]], ary_num[ary[1]], ary_num[ary[2]], ary_num[ary[3]]));
}

if (s < 3)
{
s = s + 1;
}

}


}
Response.Write(string.Format("{1}总数为:{0} 个数列", cout, num));
}
}
}
}
风骑士之怒 2011-06-26
  • 打赏
  • 举报
回复
我想到的,LZ应该有想到了,坐等高效
md5e 2011-06-26
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Galsun.Nfdw.Web
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
int[] ary = new int[] { 0, 1, 2, 2 };
ary.CopyTo(ary, 0);
int s = 3;
string num = "12345";
char[] ary_num = num.ToCharArray();
int n = ary_num.Length - 1;
int cout = 0;
while (s >= 0)
{

loop: if (ary[s] >= n)
{

ary[s] = -1;
s = s - 1;

}
else
{
ary[s] = ary[s] + 1;
for (int i = s - 1; i >= 0; i--)
{
if (ary[s] == ary[i])
{
goto loop;
}
}
if (s == 3)
{
cout++;
Response.Write(string.Format("{0}{1}{2}{3}<br/>", ary_num[ary[0]], ary_num[ary[1]], ary_num[ary[2]], ary_num[ary[3]]));
}

if (s < 3)
{
s = s + 1;
}

}


}
Response.Write(string.Format("总数为:{0} 个数列", cout));
}
}
}
}
fk1984316 2011-06-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 caozhy 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)……
[/Quote]
可以的
patrickjiang 2011-06-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 caozhy 的回复:]

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
……
[/Quote]

+1 强人
dalmeeme 2011-06-26
  • 打赏
  • 举报
回复
写了一个,没做代码优化。
	int[] sourceIntegerArray = new int[4] { 1, 2, 3, 4};
int[] oneOfIntegerResults = new int[4];
List<int[]> resultIntegerList = new List<int[]>();
protected void Page_Load(object sender, EventArgs e)
{
ArrangeInteger(oneOfIntegerResults.Length, sourceIntegerArray.Length, 0);
Response.Write(resultIntegerList.Count + "项结果<br/>");
for (int i = 0; i < resultIntegerList.Count; i++)
{
for (int j = 0; j < resultIntegerList[i].Length; j++)
Response.Write(resultIntegerList[i][j]);
Response.Write("<br/>");
}
}
private void ArrangeInteger(int takeCount, int totalCount, int depth)
{
if (takeCount > 0)
{
for (int i = 0; i < sourceIntegerArray.Length; i++)
{
bool IsExist = false;
for (int j = 0; j < oneOfIntegerResults.Length; j++)
{
if (sourceIntegerArray[i] == oneOfIntegerResults[j])
{
IsExist = true;
break;
}
}
if (!IsExist)
{
oneOfIntegerResults[depth] = sourceIntegerArray[i];
ArrangeInteger(takeCount - 1, totalCount - 1, depth + 1);
}
for (int j = depth; j < oneOfIntegerResults.Length; j++)
oneOfIntegerResults[j] = int.MinValue;
}
}
else
{
int[] temp = new int[oneOfIntegerResults.Length];
for (int i = 0; i < temp.Length; i++)
temp[i] = oneOfIntegerResults[i];
resultIntegerList.Add(temp);
}
}
}
threenewbee 2011-06-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 caozhy 的回复:]
引用 5 楼 chenhaoying 的回复:
就是全排列算法。谁给好算法。
3楼的在asp.net中运行过不了。不认var和Union。

运行不了应该和asp.net无关。你可能用的C#版本太低了。
自动类型推定和 LINQ 至少需要 C#3/VS2008,如果是早期版本,你需要改写下代码。

程序我测试过,没有问题。
[/Quote]
看了下,你把 var 换成 string

foreach (var i in Combo(s, 4))
{
list = list.Union(Arrange(i)).ToList();
}
=〉

foreach (string i in Combo(s, 4))
{
foreach (string j in Arrange(i))
list.Add(j);
}

应该就可以在VS2005编译了。

(VS2003 还需要改写 yield return)
md5e 2011-06-26
  • 打赏
  • 举报
回复
用回朔吧
1234
1235
1243
1244
1245
....

threenewbee 2011-06-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenhaoying 的回复:]
就是全排列算法。谁给好算法。
3楼的在asp.net中运行过不了。不认var和Union。
[/Quote]
运行不了应该和asp.net无关。你可能用的C#版本太低了。
自动类型推定和 LINQ 至少需要 C#3/VS2008,如果是早期版本,你需要改写下代码。

程序我测试过,没有问题。
chenhaoying 2011-06-26
  • 打赏
  • 举报
回复
就是全排列算法。谁给好算法。
3楼的在asp.net中运行过不了。不认var和Union。
threenewbee 2011-06-25
  • 打赏
  • 举报
回复
mark下

62,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧