Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 帮我看看,哪里错了,头晕脑涨了(关于字符匹配度)
pn526





发贴: 6
积分: 0
于 2005-07-26 17:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.

Original subject :
帮我看看,哪里错了,头晕脑涨了


我写个程序关于字符匹配度的.弄不出来.是参考一个pascal的程序来的.
关键词:字符串匹配度
'John' and 'John' = 100%
'John' and 'Jon' = 75%
'Jim' and 'James' = 40%
"Luke Skywalker" and 'Darth Vader' = 0%


function StrSimilar (s1, s2: string): Integer;
var hit: Integer; // Number of identical chars
p1, p2: Integer; // Position count
l1, l2: Integer; // Length of strings
pt: Integer; // for counter
diff: Integer; // unsharp factor
hstr: string; // help var for swapping strings
// Array shows is position is already tested
test: array [1..255] of Boolean;
begin
// Test Length and swap, if s1 is smaller
// we alway search along the longer string
if Length(s1) < Length(s2) then begin
hstr:= s2; s2:= s1; s1:= hstr;
end;
// store length of strings to speed up the function
l1:= Length (s1);
l2:= Length (s2);
p1:= 1; p2:= 1; hit:= 0;
// calc the unsharp factor depending on the length
// of the strings. Its about a third of the length
diff:= Max (l1, l2) div 3 + ABS (l1 - l2);
// init the test array
for pt:= 1 to l1 do test[pt]:= False;
// loop through the string
repeat
// position tested?
if not test[p1] then begin
// found a matching character?
if (s1[p1] = s2[p2]) and (ABS(p1-p2) <= diff) then begin
test[p1]:= True;
Inc (hit); // increment the hit count
// next positions
Inc (p1); Inc (p2);
if p1 > l1 then p1:= 1;
end else begin
// Set test array
test[p1]:= False;
Inc (p1);
// Loop back to next test position if end of the string
if p1 > l1 then begin
while (p1 > 1) and not (test[p1]) do Dec (p1);
Inc (p2)
end;
end;
end else begin
Inc (p1);
// Loop back to next test position if end of string
if p1 > l1 then begin
repeat Dec (p1); until (p1 = 1) or test[p1];
Inc (p2);
end;
end;
until p2 > Length(s2);
// calc procentual value
Result:= 100 * hit DIV l1;
end;


我自己用java写的如下.

<%@ page contentType="text/html; charset=GBK" import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.lang.*"%>
<%!
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
public void jspInit()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:aim");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ex)
{
  System.out.println(ex.toString());
}
}
public void jspDestory()
{
try
  {
   stmt.close();
con.close();
rs.close();
  }
  catch(Exception ex)
  {
System.out.println(ex.toString());
  }
}
%>
<html>
<head>
<title>
result
</title>
</head>
<body bgcolor="#ffffff">
<%
int p1,p2,l2,l1,diff,pt,i=0,num=0;
String swap,s;
boolean test[]=new boolean[255];
s="fdddd";//request.getParameter("txt");
rs=stmt.executeQuery("SELECT * FROM [table]");
while(rs.next())
{
num++;
}
int hit[]=new int[num];
float result[]=new float[num];
String str[]=new String[num];
rs=stmt.executeQuery("SELECT * FROM [table]");
while(rs.next())
{
str[i]=rs.getString("content");
if( s.length()>str[i].length())
{
swap=str[i];
str[i]=s;
s=swap;
}
l2=s.length();l1=str[i].length();
p1=1;p2=1;
for(i=0;i<num;i++)
{hit[i]=0;}
//diff=Math.max(l1,l2)/3+Math.abs(l1-l2);
for(pt=0;pt<l1;pt++)
{test[pt]=false;}
while(p2<l2)
{
if(!test[p1])
{
if(str[i].charAt(p1)==s.charAt(p2))//&&(Math.abs(p1-p2)<=diff))
  {
test[p1]=true;
hit[i]=hit[i]+1;
p1++;p2++;
if(p1>l1){p1=1;}
  }
else
{
test[p1]=false;
p1++;
if(p1>l1)
    {
while(p1>1&&!test[p1])
      {p1--;}
p2++;
    }
}
i++;
}
else
{
if(p1>l1)
   {
while((p1==1)||(test[p1]))
{p2++;}
   }
}
}
/*for(p1=0;p1<l1[i];p1++)
{
for(p2=0;p2<s.length();p2++)
{
if(s.charAt(p2)==str[i].charAt(p1))
{
hit[i]++;
}
}
}
*/
for(i=0;i<num-1;i++)
{
result[i]=hit[i]/l1;
if(result[i]>=result[i+1])
{
out.printlnLight Bulb;
}
}
}
%>

</body>
</html>

帮我看看,弄不出来!3q


why edited on 2005-07-26 19:47


话题树型展开
人气 标题 作者 字数 发贴时间
11632 帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 4739 2005-07-26 17:21
9672 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) why 99 2005-07-26 19:52
9740 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 190 2005-07-26 19:55
9632 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) why 260 2005-07-26 20:35
9829 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 235 2005-07-26 20:41
9755 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 8 2005-07-26 19:56
9732 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 59 2005-07-27 10:01
9542 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) why 334 2005-07-27 11:48
9725 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 25 2005-07-27 17:07
9939 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) ranchgirl 1071 2005-07-28 07:24
9546 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) why 616 2005-07-28 10:15
9647 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) pn526 31 2005-07-28 10:26
10012 Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) lvmingtao 18 2005-07-28 15:52

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923