命题:查找一组关键词的最短的距离 (388分)

hello晨光 2005-10-25 01:51:13
命题:查找一组关键词的最短的距离
条件:
1、有一组词,存在一编文章里的不同位置。
2、这组词出现的频率没有固定。
3、这编文章存于text字段中。
具体要求:

有一组词(张三、李四,王五,赵六,田七,王八...),当中“张三”可能出现15次,“李四”可能出现32次,“王五”可能出现12次....。现在要把文章中出现“张三”、“李四”、王五、超六最近的距离找出来。词出现的顺序不受限制。

本题分数:388

另外300分再开贴送出。

==================================
可参考:http://community.csdn.net/Expert/topic/4348/4348339.xml?temp=.3764917
上面是两个词的情况
...全文
437 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello晨光 2005-10-31
  • 打赏
  • 举报
回复
呵呵,来结贴了哈。
jzywh 2005-10-30
  • 打赏
  • 举报
回复
等你结帖头发都等白了!

不过我还是升双星了
kevin_gao 2005-10-26
  • 打赏
  • 举报
回复
头晕了,不过偶拥护江大鱼。
LifeForCode 2005-10-26
  • 打赏
  • 举报
回复
mark
deiphi 2005-10-26
  • 打赏
  • 举报
回复
mark
jzywh 2005-10-26
  • 打赏
  • 举报
回复
顶一下!
jzywh 2005-10-26
  • 打赏
  • 举报
回复
有问题么?
jzywh 2005-10-25
  • 打赏
  • 举报
回复
上面已经加了注释,可能表述得不是很清楚,仔细读代码也可以看出大概思路!
jzywh 2005-10-25
  • 打赏
  • 举报
回复
<%
function GetPos(start,word)'获取位置为start以word字符串开始包含所有指定字符串的最短字符串的信息(包括起始位置和长度)
dim i,IntIndex,MaxIndex
dim flag
flag = 1
MaxIndex = 0
for i=0 to ubound(ArrWord)
if i<>cint(word) then
IntIndex = Instr(start+len(ArrWord(word)),StrObj,ArrWord(i))
if IntIndex>0 then
flag = flag + 1
'response.write flag & ":" & i & "<br>"
if MaxIndex<(IntIndex+len(ArrWord(i))) then
MaxIndex = IntIndex+len(ArrWord(i))
end if
end if
end if
next
if flag = ubound(ArrWord)+1 then
GetPos = "(" & start & "," & cstr(MaxIndex-start) & ")" '返回最短字符串的起始位置和长度
else
GetPos = "(1,2,3)" '如果找不到,返回一个指定的字符串
end if

end function

function GetNearest() '获取字符串组中的字符串在总的字符串中出现的位置的列表.StrResult:出现的位置列表(格式如:3,5,8,24),StrResult1:对应的字符串列表(格式如:张三,张三,李四,王五),此函数返回StrResult&"|"&StrResult1
dim i,StrResult,IntIndex,Seq,StrResult1
StrResult = ","
StrResult1 = ","
for i = 0 to ubound(ArrWord)
Seq = 1
IntIndex = Instr(Seq,StrObj,ArrWord(i))
do until IntIndex<1
StrResult = StrResult & IntIndex & ","
StrResult1 = StrResult1 & i & ","
Seq = IntIndex + Len(ArrWord(i))
IntIndex = Instr(Seq,StrObj,ArrWord(i))
loop
next
StrResult = left(StrResult,len(StrResult)-1)
StrResult = right(StrResult,len(StrResult)-1)
StrResult1 = left(StrResult1,len(StrResult1)-1)
StrResult1 = right(StrResult1,len(StrResult1)-1)
GetNearest = StrResult & "|" & StrResult1
end function

dim StrObj,ArrWord,ArrDotList,ArrWordList,ArrAll,i,ArrLen,StrLenList
'StrObj="wer张三色单峰驼retert李四李四李四sdf张三色单峰驼rete34王五53赵六543543rt李四sdfwe"
StrObj="aaa张三bbbb李四李四李四ccc张三dddddddd王五e赵六f李四ggg王五热土hhhhhhh赵六"
ArrWord = Array("张三","李四","王五","赵六","刘七")
ArrAll = split(GetNearest,"|")
ArrDotList = split(ArrAll(0),",") '将字符串组中的字符串在总的字符串中出现的位置赋给数组ArrDotList
ArrWordList = split(ArrAll(1),",") '将字符串组中的字符串在总的字符串中出现的位置列表对应的字符串赋给数组ArrWordList
StrLenList = ""

for i=0 to ubound(ArrDotList)
StrLenList = StrLenList & GetPos(ArrDotList(i),ArrWordList(i)) '
next
'response.write StrLenList & "<br>"
StrLenList = left(StrLenList,len(StrLenList)-1)
StrLenList = right(StrLenList,len(StrLenList)-1)

ArrLen = split(StrLenList,")(")

dim MinMarginId,MinMargin,ArrTemp,MinMarginStart
MinMargin = 100000
for i=0 to ubound(ArrLen) '比较长度,获取最小
ArrTemp = split(ArrLen(i),",")
if ubound(ArrTemp)<2 then
if cint(ArrTemp(1))<MinMargin then
MinMargin = cint(ArrTemp(1))
MinMarginStart = cint(ArrTemp(0))
MinMarginId = i
end if
end if
next

if MinMargin<>100000 then
response.write "包含所要求多个字符串的最短字符串为:" & mid(StrObj,MinMarginStart,MinMargin) '输出最小字符串
else
response.write "包含所要求多个字符串的最短字符串为:制定的某字符串在整个字符串中不存在" '输出例外
end if
%>
BlueDestiny 2005-10-25
  • 打赏
  • 举报
回复
用正则不是简单多了吗?

<script language="vbs">
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全局替换。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 遍历 Matches 集合。
RetStr = RetStr & "Match " & I & " found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is "'
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
</script>
hello晨光 2005-10-25
  • 打赏
  • 举报
回复
TO:jzywh(江大鱼)

不得不佩服你哈。
加上一些必要的说明吧,另外我在测试一下,没问题的话,明天结贴。
zhanghongwen 2005-10-25
  • 打赏
  • 举报
回复
头晕.帮顶.
jzywh 2005-10-25
  • 打赏
  • 举报
回复
加了判断

再看看



<%
function GetPos(start,word)
dim i,IntIndex,MaxIndex
dim flag
flag = 1
MaxIndex = 0
for i=0 to ubound(ArrWord)
if i<>cint(word) then
IntIndex = Instr(start+len(ArrWord(word)),StrObj,ArrWord(i))
if IntIndex>0 then
flag = flag + 1
'response.write flag & ":" & i & "<br>"
if MaxIndex<(IntIndex+len(ArrWord(i))) then
MaxIndex = IntIndex+len(ArrWord(i))
end if
end if
end if
next
if flag = ubound(ArrWord)+1 then
GetPos = "(" & start & "," & cstr(MaxIndex-start) & ")"
else
GetPos = "(1,2,3)"
end if

end function

function GetNearest()
dim i,StrResult,IntIndex,Seq,StrResult1
StrResult = ","
StrResult1 = ","
for i = 0 to ubound(ArrWord)
Seq = 1
IntIndex = Instr(Seq,StrObj,ArrWord(i))
do until IntIndex<1
StrResult = StrResult & IntIndex & ","
StrResult1 = StrResult1 & i & ","
Seq = IntIndex + Len(ArrWord(i))
IntIndex = Instr(Seq,StrObj,ArrWord(i))
loop
next
StrResult = left(StrResult,len(StrResult)-1)
StrResult = right(StrResult,len(StrResult)-1)
StrResult1 = left(StrResult1,len(StrResult1)-1)
StrResult1 = right(StrResult1,len(StrResult1)-1)
GetNearest = StrResult & "|" & StrResult1
end function

dim StrObj,ArrWord,ArrDotList,ArrWordList,ArrAll,i,ArrLen,StrLenList
'StrObj="wer张三色单峰驼retert李四李四李四sdf张三色单峰驼rete34王五53赵六543543rt李四sdfwe"
StrObj="aaa张三bbbb李四李四李四ccc张三dddddddd王五e赵六f李四ggg王五热土hhhhhhh赵六"
ArrWord = Array("张三","李四","王五","赵六","刘七")
ArrAll = split(GetNearest,"|")
ArrDotList = split(ArrAll(0),",")
ArrWordList = split(ArrAll(1),",")
StrLenList = ""

for i=0 to ubound(ArrDotList)
StrLenList = StrLenList & GetPos(ArrDotList(i),ArrWordList(i))
next
'response.write StrLenList & "<br>"
StrLenList = left(StrLenList,len(StrLenList)-1)
StrLenList = right(StrLenList,len(StrLenList)-1)

ArrLen = split(StrLenList,")(")

dim MinMarginId,MinMargin,ArrTemp,MinMarginStart
MinMargin = 100000
for i=0 to ubound(ArrLen)
ArrTemp = split(ArrLen(i),",")
if ubound(ArrTemp)<2 then
if cint(ArrTemp(1))<MinMargin then
MinMargin = cint(ArrTemp(1))
MinMarginStart = cint(ArrTemp(0))
MinMarginId = i
end if
end if
next

if MinMargin<>100000 then
response.write "包含所要求多个字符串的最短字符串为:" & mid(StrObj,MinMarginStart,MinMargin)
else
response.write "包含所要求多个字符串的最短字符串为:制定的某字符串在整个字符串中不存在"
end if



%>
hello晨光 2005-10-25
  • 打赏
  • 举报
回复
没有搞出错处理
把ArrWord设置改一下就出错了。

ArrWord = Array("张三","李四","王六")
jzywh 2005-10-25
  • 打赏
  • 举报
回复

<%
function GetPos(start,word)
dim i,IntIndex,MaxIndex
dim flag
flag = 1
MaxIndex = 0
for i=0 to ubound(ArrWord)
if i<>cint(word) then
IntIndex = Instr(start+len(ArrWord(word)),StrObj,ArrWord(i))
if IntIndex>0 then
flag = flag + 1
'response.write flag & ":" & i & "<br>"
if MaxIndex<(IntIndex+len(ArrWord(i))) then
MaxIndex = IntIndex+len(ArrWord(i))
end if
end if
end if
next
if flag = ubound(ArrWord)+1 then
GetPos = "(" & start & "," & cstr(MaxIndex-start) & ")"
else
GetPos = "(1,2,3)"
end if

end function

function GetNearest()
dim i,StrResult,IntIndex,Seq,StrResult1
StrResult = ","
StrResult1 = ","
for i = 0 to ubound(ArrWord)
Seq = 1
IntIndex = Instr(Seq,StrObj,ArrWord(i))
do until IntIndex<1
StrResult = StrResult & IntIndex & ","
StrResult1 = StrResult1 & i & ","
Seq = IntIndex + Len(ArrWord(i))
IntIndex = Instr(Seq,StrObj,ArrWord(i))
loop
next
StrResult = left(StrResult,len(StrResult)-1)
StrResult = right(StrResult,len(StrResult)-1)
StrResult1 = left(StrResult1,len(StrResult1)-1)
StrResult1 = right(StrResult1,len(StrResult1)-1)
GetNearest = StrResult & "|" & StrResult1
end function

dim StrObj,ArrWord,ArrDotList,ArrWordList,ArrAll,i,ArrLen,StrLenList
StrObj="wer张三色单峰驼retert李四李四李四sdf张三色单峰驼rete34王五53赵六543543rt李四sdfwe"
ArrWord = Array("张三","李四","王五","赵六")
ArrAll = split(GetNearest,"|")
ArrDotList = split(ArrAll(0),",")
ArrWordList = split(ArrAll(1),",")
StrLenList = ""

for i=0 to ubound(ArrDotList)
StrLenList = StrLenList & GetPos(ArrDotList(i),ArrWordList(i))
next
'response.write StrLenList & "<br>"
StrLenList = left(StrLenList,len(StrLenList)-1)
StrLenList = right(StrLenList,len(StrLenList)-1)

ArrLen = split(StrLenList,")(")

dim MinMarginId,MinMargin,ArrTemp,MinMarginStart
MinMargin = 100000
for i=0 to ubound(ArrLen)
ArrTemp = split(ArrLen(i),",")
if ubound(ArrTemp)<2 then
if cint(ArrTemp(1))<MinMargin then
MinMargin = cint(ArrTemp(1))
MinMarginStart = cint(ArrTemp(0))
MinMarginId = i
end if
end if
next



response.write "包含所要求多个字符串的最短字符串为:" & mid(StrObj,MinMarginStart,MinMargin)
%>

看看这个,应该满足你的要求吧?

欢迎测试!
bbsad 2005-10-25
  • 打赏
  • 举报
回复
顶一下..好问题..
meizz 2005-10-25
  • 打赏
  • 举报
回复
最简单的就是使用正则表达式,它不仅可以找出个数,也可以提供每个词在原文中的索引。
wanghui0380 2005-10-25
  • 打赏
  • 举报
回复
呵呵,不是最短而是出现机率最高
hello晨光 2005-10-25
  • 打赏
  • 举报
回复
哪位兄弟,快把388分领走哈。
hello晨光 2005-10-25
  • 打赏
  • 举报
回复
"包含所要求多个字符串的最短字符串"
正解!~简单明了
文字描述有时描述不过来。
加载更多回复(15)

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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