字符串操作

goodboyrwy 2003-12-23 10:57:02
n个长度不一样的字符串,从其中任意m个字符串中各取一个,得出n*(n-1)*..*m个字符串。比如有"12345678","ABCDEFG","abcdef","~!@#$"四个字符串,从从其中任意3个字符串中各取一个,则结果为"1Aa","1A~","1Ab",....。
如何写算法?
...全文
52 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-12-24
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim X As String
X = getall("12345678,ABCDEFG,abcdef,~!@#$", 4, 3)
MsgBox X, 0, UBound(Split(X, ",")) + 1
End Sub
Function getall(ByVal X As String, ByVal n As Integer, ByVal m As Integer) As String
Dim temp As Variant, temp2() As String
Dim I As Long, J As Long
temp = Split(X, ",")

If n = 2 And m = 1 Then
ReDim Preserve temp2(Len(temp(0) & temp(1)) - 1)
For I = 0 To UBound(temp2)
temp2(I) = Mid(temp(0) & temp(1), I + 1, 1)
Next
getall = Join(temp2, ",")
Erase temp2
End If

If n = 2 And m = 2 Then
ReDim Preserve temp2(1 To Len(temp(0)) * Len(temp(1)))
For I = 1 To Len(temp(0))
For J = 1 To Len(temp(1))
temp2((I - 1) * Len(temp(1)) + J) = Mid(temp(0), I, 1) & Mid(temp(1), J, 1)
Next
Next
getall = Join(temp2, ",")
Erase temp2
End If


If n > 2 And m = 1 Then
getall = Left(temp(n - 1), 1)
For I = 2 To Len(temp(n - 1))
getall = getall & "," & Mid(temp(n - 1), I, 1)
Next
getall = getall(X, n - 1, 1) & "," & getall
End If


If n > m And m >= 2 Then
ReDim temp2(Len(temp(n - 1)) - 1)
For J = 1 To Len(temp(n - 1))
temp2(J - 1) = Replace(getall(X, n - 1, m - 1), ",", Mid(temp(n - 1), J, 1) & ",") & Mid(temp(n - 1), J, 1)
Next
getall = Join(temp2, ",")
Erase temp2
getall = IIf(getall(X, n - 1, m) = "", getall, getall(X, n - 1, m) & "," & getall)
End If


End Function

captainivy 2003-12-24
  • 打赏
  • 举报
回复
不难实现
可惜这台电脑没装vb
无法给你代码
northwolves 2003-12-24
  • 打赏
  • 举报
回复
n个字符串的长度分别为a1,a2,....an
则满足条件的字符串的个数为多项式(1+a1x)(1+a2x)(1+a3x)....(1+anx)的x^m 项的系数。递归可以解决,半小时后我给你贴出代码。
northwolves 2003-12-23
  • 打赏
  • 举报
回复
n*(n-1)*..*m个字符串?
if your strings are a(1),a(2),....a(n)
when m=1
you can only get len(join(a),"")) strings ,not n*(n-1)*..*m strings!!!
onlineboy 2003-12-23
  • 打赏
  • 举报
回复
同意楼上的
不过要遍历的话,就要用回溯算法了
jilate 2003-12-23
  • 打赏
  • 举报
回复
1、通过随机函数取得一个小于字符串长度的数字(如果每个字符串长度不一样的话就每个来一个)。
2、通过这个数字分别在3个字符串中取数。
3、组成一个新的字符串
goodboyrwy 2003-12-23
  • 打赏
  • 举报
回复
n个长度不同的字符串,从其中任意m个字符串中各取一个,得出n*(n-1)*..*m个字符串。比如有"12345678","ABCDEFG","abcdef","~!@#$"四个字符串,从从其中任意3个字符串中各取一个,则结果为"1Aa","1A~","1Ab",....。

如何写算法?
jhzhou882 2003-12-23
  • 打赏
  • 举报
回复
你要的应该是个算法
liuyanghe111 2003-12-23
  • 打赏
  • 举报
回复
你可以看看回溯算法,这里有个例子
http://www.cstc.net.cn/docs/docs.php?id=140
flc 2003-12-23
  • 打赏
  • 举报
回复
关注

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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