在一个长字符串中查找"包含所要求多个字符串的最短字符串"的VBscript算法

博客展示了在长字符串中查找包含多个指定字符串的最短字符串的VBscript算法。通过定义GetPos和GetNearest函数,对字符串进行处理和分析,最终找出符合要求的最短字符串,若未找到则给出相应提示。
 1None.gif<%
 2None.giffunction GetPos(start,word)
 3None.gif    dim i,IntIndex,MaxIndex
 4None.gif    dim flag
 5None.gif    flag    =    1
 6None.gif    MaxIndex    =    0
 7None.gif    for i=0 to ubound(ArrWord)
 8None.gif        if i<>cint(word) then
 9None.gif            IntIndex    =    Instr(start+len(ArrWord(word)),StrObj,ArrWord(i))
10None.gif            if IntIndex>0 then
11None.gif                flag    =    flag + 1
12None.gif                'response.write flag & ":" & i & "<br>"
13None.gif                if MaxIndex<(IntIndex+len(ArrWord(i))) then
14None.gif                    MaxIndex    =    IntIndex+len(ArrWord(i))
15None.gif                end if
16None.gif            end if
17None.gif        end if
18None.gif    next    
19None.gif    if flag = ubound(ArrWord)+1 then
20None.gif        GetPos    =    "(" & start & "," & cstr(MaxIndex-start) & ")"
21None.gif    else
22None.gif        GetPos    =    "(1,2,3)"
23None.gif    end if
24None.gif    
25None.gifend function
26None.gif
27None.giffunction GetNearest()
28None.gif    dim i,StrResult,IntIndex,Seq,StrResult1
29None.gif    StrResult    =    ","
30None.gif    StrResult1    =    ","    
31None.gif    for i = 0 to ubound(ArrWord)
32None.gif        Seq            =    1
33None.gif        IntIndex    =    Instr(Seq,StrObj,ArrWord(i))
34None.gif        do until IntIndex<1
35None.gif            StrResult    =    StrResult & IntIndex & ","
36None.gif            StrResult1    =    StrResult1 & i & ","
37None.gif            Seq            =    IntIndex + Len(ArrWord(i))
38None.gif            IntIndex    =    Instr(Seq,StrObj,ArrWord(i))
39None.gif        loop        
40None.gif    next
41None.gif    StrResult    =    left(StrResult,len(StrResult)-1)
42None.gif    StrResult    =    right(StrResult,len(StrResult)-1)
43None.gif    StrResult1    =    left(StrResult1,len(StrResult1)-1)
44None.gif    StrResult1    =    right(StrResult1,len(StrResult1)-1)
45None.gif    GetNearest    =    StrResult & "|" & StrResult1
46None.gifend function
47None.gif
48None.gifdim StrObj,ArrWord,ArrDotList,ArrWordList,ArrAll,i,ArrLen,StrLenList
49None.gif'StrObj="wer张三色单峰驼retert李四李四李四sdf张三色单峰驼rete34王五53赵六543543rt李四sdfwe"
50None.gifStrObj="aaa张三bbbb李四李四李四ccc张三dddddddd王五e赵六f李四ggg王五热土hhhhhhh赵六"
51None.gifArrWord =     Array("张三","李四","王五","赵六","刘七")
52None.gifArrAll        =    split(GetNearest,"|")
53None.gifArrDotList     =     split(ArrAll(0),",")
54None.gifArrWordList    =     split(ArrAll(1),",")
55None.gifStrLenList    =    ""
56None.gif
57None.giffor i=0 to ubound(ArrDotList)
58None.gif    StrLenList    =    StrLenList & GetPos(ArrDotList(i),ArrWordList(i))
59None.gifnext
60None.gif'response.write StrLenList & "<br>"
61None.gifStrLenList    =    left(StrLenList,len(StrLenList)-1)
62None.gifStrLenList    =    right(StrLenList,len(StrLenList)-1)
63None.gif
64None.gifArrLen    =    split(StrLenList,")(")
65None.gif
66None.gifdim MinMarginId,MinMargin,ArrTemp,MinMarginStart
67None.gifMinMargin    =    100000
68None.giffor i=0 to ubound(ArrLen)
69None.gif    ArrTemp    =    split(ArrLen(i),",")
70None.gif    if ubound(ArrTemp)<2 then
71None.gif        if cint(ArrTemp(1))<MinMargin then
72None.gif            MinMargin    =    cint(ArrTemp(1))
73None.gif            MinMarginStart    =    cint(ArrTemp(0))
74None.gif            MinMarginId    =    i
75None.gif        end if
76None.gif    end if
77None.gifnext
78None.gif
79None.gifif MinMargin<>100000 then
80None.gif    response.write "包含所要求多个字符串的最短字符串为:" & mid(StrObj,MinMarginStart,MinMargin)
81None.gifelse
82None.gif    response.write "包含所要求多个字符串的最短字符串为:制定的某字符串在整个字符串中不存在"
83None.gifend if
84None.gif
85None.gif
86None.gif
87None.gif%>
88None.gif

转载于:https://www.cnblogs.com/jzywh/archive/2005/10/26/262238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值