星期五, 一月 09, 2009
SQLITE超强select case语法
select case
when red1%7=0 or red2%7=0 or red3%7=0 or red4%7=0 or red5%7=0 or red6%7=0 then 7
when red1%9=0 or red2%9=0 or red3%9=0 or red4%9=0 or red5%9=0 or red6%9=0 then 9
when red1%11=0 or red2%11=0 or red3%11=0 or red4%11=0 or red5%11=0 or red6%11=0 then 11
else term
end as result,rowid,count(*)
from _DBHISTORY group by result
2.标准switch case
select column_value case
when 1 then 1
when 2 then 2
else
3
end as result,rowid
from tableName
星期四, 十二月 25, 2008
文本聚类算法TCUSS(Text clustering using semantic similarity)
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法
动态规划求解最长公共子串问题
算法思想
求字符串str1,str2的最长公共子串的长度。
定义二元函数函数f(m,n):分别以str1[m],str2[n]结尾的连续公共子串的长度
而对于f(m+1,n+1) 有以下两种情况
1.str1[m+1] != str2[n+1],则有f(m+1,n+1) =0
2.str1[m+1] == str2[n+1],则有f(m+1,n+1) = f(m,n) + 1
另外f(0,j) = 0(j>=0)
f(j,0) = 0 (j>=0)
按照上面这个公式,我们用容易写出这个算法的实现
算法实现
1 int commstr(char *str1, char *str2)
2 /* 返回str1,str2的最长公共之串长度*/
3 {
4 int len1=strlen(str1),len2=strlen(str2),row,col,max=0;
5 int **pf = new int*[len1+1];//动态分配一个二维数组作为辅助空间
6 for (row=0; row
7 pf[row] = new int[len2+1];
8
9 //数组赋初值
10 for (row=0; row
11 pf[row][0] = 0;
12 for (col=0; col
13 pf[0][col] = 0;
14
15 for (row=1; row<=len1; row++)
16 for (col=1;col<=len2; col++)
17 {
18 if (str1[row-1] == str2[col-1])
19 {
20 pf[row][col] = pf[row-1][col-1] + 1;
21 max = pf[row][col] > max ? pf[row][col] : max;
22 }
23 else
24 pf[row][col] = 0;
25 }
26 //空间回收
27 for (row=0; row
28 delete[] pf[row];
29 delete[] pf;
30
31 return max;
32 }
程序的输出
字符串"blog.csdn.net"和"csdn.blog"求公共子串时的输出结果
String:
1. blog.csdn.net
2. csdn.blog
c s d n . b l o g
0 0 0 0 0 0 0 0 0 0
b 0 0 0 0 0 0 1 0 0 0
l 0 0 0 0 0 0 0 2 0 0
o 0 0 0 0 0 0 0 0 3 0
g 0 0 0 0 0 0 0 0 0 4
. 0 0 0 0 0 1 0 0 0 0
c 0 1 0 0 0 0 0 0 0 0
s 0 0 2 0 0 0 0 0 0 0
d 0 0 0 3 0 0 0 0 0 0
n 0 0 0 0 4 0 0 0 0 0
. 0 0 0 0 0 5 0 0 0 0
n 0 0 0 0 1 0 0 0 0 0
e 0 0 0 0 0 0 0 0 0 0
t 0 0 0 0 0 0 0 0 0 0
max substr length:5
这是程序的输出结果,请注意红色字体
时间空间复杂度分析
如果用n,m表示两个字符串的长度的话,那么算法的
时间复杂度为O(n*m),空间复杂度也为O(n*m)
附:完整的源程序g++编译通过
#include
#include
void print_table(char *str1,char *str2,int **pf)
{
int i,j,row,col;
row = strlen(str1);
col = strlen(str2);
printf("\t\t");
for (i=0; i
printf("%c\t",str2[i]);
for (i=0; i<=row; i++)
{
for (j=0; j<=col; j++)
{
if (j == 0)
{
printf("\n");
if (i)
printf("%c\t",str1[i-1]);
else
printf("\t");
}
printf("%d\t",pf[i][j]);
}
}
}
int commstr(char *str1, char *str2)
/* 返回str1,str2的最长公共之串长度*/
{
int len1=strlen(str1),len2=strlen(str2),row,col,max=0;
int **pf = new int*[len1+1];//动态分配一个二维数组作为辅助空间
for (row=0; row
pf[row] = new int[len2+1];
//数组赋初值
for (row=0; row
pf[row][0] = 0;
for (col=0; col
pf[0][col] = 0;
for (row=1; row<=len1; row++)
for (col=1;col<=len2; col++)
{
if (str1[row-1] == str2[col-1])
{
pf[row][col] = pf[row-1][col-1] + 1;
max = pf[row][col] > max ? pf[row][col] : max;
}
else
pf[row][col] = 0;
}
print_table(str1,str2,pf);
//空间回收
for (row=0; row
delete[] pf[row];
delete[] pf;
return max;
}
int main(int argc,char **argv)
{
if (argc >= 3)
{
printf("String:\n\t1. %s\n\t2. %s\n",argv[1],argv[2]);
printf("\nmax substr length:%d\n",commstr(argv[1],argv[2]));
}
return 0;
}
星期三, 十二月 24, 2008
数据库自我关联
select a.*,b.* from _dbhistory a left join _dbhistory b on a.rowid=(b.rowid-1)
星期五, 十一月 21, 2008
DhtmlxGrid 错误跟踪
{
//custom code can be placed here
return false;
}
dhtmlxError.catchError("LoadXML", myErrorHandler);
星期三, 十一月 19, 2008
星期一, 十一月 17, 2008
MathType快捷键
插入一个上标:Ctrl+H
插入一个下标:Ctrl+L
Right arrow with upper text slot (Ctrl+T,Shift+Right) , 输入条件推理
Right arrow with lower text slot (Ctrl+T,CtrlAlt+Right) 输入条件推理
Right arrow with upper and lower text slots (Ctrl+T,Right) 输入条件推理
Curly brackets 大括号{
Left-pointing angle brackets 向左的尖括号<
Right-pointing angle bracket from Symbol style (Ctrl+Shift+K,>)
Left white square bracket from Extra Math style (Ctrl+Shift+K,[)
星期四, 十一月 06, 2008
Java JPopupMenu selection 丢失。
简单使用如下方法就可以了:
jpopup.setInvoker(jpopup);
如果想修改selection的颜色,可以使用如下方法,
UIManager.put("MenuItem.selectionBackground", Color.YELLOW);
星期三, 十一月 05, 2008
小窍门:Java如何让一个窗体显示在最上面?
使用这个方法可以做到。
如何在任何地方得到鼠标的的屏幕坐标?
jdk 6.0提供MouseInfo.getPointerInfo() .getLocation() 得到Point.getX(),.getY()
星期六, 十一月 01, 2008
如何选择JTable中任一行和任一列?
table.setModel(dayModel);
table.setCellSelectionEnabled(true);
table.getSelectionModel().setSelectionInterval(2, 2);
table.getColumnModel().getSelectionModel().setSelectionInterval(3, 3);
你可以修改上面的2行,3列为你自己的任一行或列。非常的easy
星期一, 十月 27, 2008
如何提高SQLITE JDBC大批量插入数据的速度?
类似语句如下:
stmt.execute("BEGIN");
for(int i=0;i<commands.length;i++)
{
stmt.addBatch(commands[i]);
}
stmt.executeBatch();
stmt.execute("end");
曾经大约800数据,使用默认方法插入,大概需要141秒,而采用新方法后,使用纯Java的JDBC
速度提高到4秒,而使用JNI的JDBC,速度提高到几乎可以忽略不计的地步。
Very nice.!!!
星期五, 十月 24, 2008
Java JScrollPane getVerticalScrollBar.setValue没有效果解决方法。
toolTip.getContentPane().add(scroll);
toolTip.pack();
//以下代码可以让scroll滚动到top顶端。
SwingUtilities.invokeLater(new Runnable(){public void run()
{
scroll.getVerticalScrollBar().setValue(0);
}
});
星期四, 十月 23, 2008
Servlet download 如何为已知的 MIME 类型激活“文件下载”对话框?
参考如下:
您可以使用 Content-disposition 头来覆盖此默认行为。其格式是:
Content-disposition: attachment; filename=fname.ext
ASP:代码
Response.AddHeader "content-disposition","attachment; filename=fname.ext"
Servlet代码:
realName="yourfiel.n";
response.addHeader("Content-disposition","attachment; filename="+realName);
//realName如果是汉字编码会造成出现不了制定的文件名的情况,需要编码
//把以上代码修改为
//response.addHeader("Content-disposition","attachment; filename="+java.net.URLEncoder.encode(realName,"UTF-8"));
response.setIntHeader("Content-length", (int)rs.getBlob("ATTACH_FILE").length());
InputStream is = rs.getBinaryStream("ATTACH_FILE");
byte[] buf = new byte[3000];
int read = 0;
while ((read = is.read(buf)) > 0)
{
// fos.write(buf, 0, read);
outs.write(buf, 0, read);
}
// fos.close();
is.close();
outs.flush();
// outs.close();
}
星期四, 十月 16, 2008
SQLite自定义函数简介
SQLite最大的特色之一就是可以用户定义函数。用户自定义函数可以像系统内置函数一样可以在注册之后像系统内置函数一样在SQL语句中使用。用户使用自定义函数类似存储过程,方便用户对常见功能的调用,也加快了执行速度。用户自定义函数整体上可以分为两种:简单函数和聚集函数。
简单函数(simple function)
简单函数用在任何表达式中,常见的有max(x,y..), min(x,y..), random(*), last_insert_rowid(), length(x),lower(x), upper(x), round(x,y), round(*), substr(x,y,z), typeof(x)
聚集函数(aggregate function)
聚集函数经常用在select语句中,常见的有avg(x),count(x), count(*), max(x), min(x), sum(x), total(x)
有些函数既是简单函数,又是聚集函数。比如只有一个参数的min()是一个聚集函数,而有多个参数的min()是一个简单函数。
SQLITE比较好的书有两本:
一个是:O'Reilly出版的 Inside SQLite
一个是:Apress.The.Definitive.Guide.to.SQLite.May.2006
星期一, 十月 13, 2008
如何立即刷新Java Swing Jcomponent组件?
如果想立即刷新JTextArea的内容去,需要调用一下语句。
JTextArea sql2= new JTextArea(5,10);
Point pt=sql2.getLocation();
Dimension ds=sql2.getSize();
sql2.paintImmediately((int)pt.getX(),(int)pt.getY(), (int)ds.getWidth(),(int)ds.getHeight());
这个工作的非常好。
星期四, 十月 09, 2008
得到当前运行class的物理位置
URL classFileDir =new yourClassName().getClass().getResource(".");
print结果为:file:....的URL
星期三, 十月 08, 2008
Windows XP SP2手工修改屏幕分辨率
Intel(R) Extreme Graphics 除了在注册表:
HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\VIDEO有引用,
本身在:
[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES\IALM]也有设置,必须删除这个设置
再启动机器后,才能正常进入。
[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES\IALM\DEVICE0]
[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES\IALM\DEVICE0\Mon80861100]
这两个注册表项目要删除掉
星期六, 九月 27, 2008
beanshell Bsh.Console如何在jdk 1.6中使用
必须使用如下语法,
String.format("%3.2f",new Object[]{(float)6/11});
就可以使用了
星期五, 九月 19, 2008
dhtmlXGrid 覆盖函数。
dhtmlXGridCellObject.prototype.getTitle=function()
{
ccolName=this.grid.getColumnId(this.cell._cellIndex);
rowID=this.cell.parentNode.idd;
result=this.cell.innerHTML;
if(ccolName=='RULE_NAME')
result=this.grid.cells(rowID,4).getValue();
return result;
}
修改每个行的颜色
mygrid.setRowColor(rowid,"yellow");
或者更加底层的,可以修改更多属性CSS
mygrid.rowsAr[idrow].style.backgroundColor="yellow"
星期四, 九月 18, 2008
dhtmlXGrid 1.3版本如何修改每个cell的帮助提示呢?Tooltip
dhtmlXGridCellObject.prototype.getTitle=function()
{
return this.cell.innerHTML+"<h1>F1 HELP</H1><BR/><P>..."+this.cell._cellIndex+"|||"+this.grid.getColumnId(this.cell._cellIndex);
}
不过这个innerHTML只承认纯文本,所以HTML语法无法使用了,\nJavascript符号还是认识的。
星期六, 九月 13, 2008
JBOSS JAAS如何编写自己的身份(principal)?
不过经过事件发现只需要重载两个方法就可以了。如果直接从org.jboss.security.auth.spi.DatabaseServerLoginModule扩展,这样可以利用JBOSS已经有的密码加密,编码等其他一些特性,不需要再重写。
类似如下:
public class StandardLoginModule extends DatabaseServerLoginModule
编写自己的定制身份类Principal,可以加入除了名字之外的其他属性.
public class userPropertiesPrincipal extends SimplePrincipal
{
//User's property
private String email;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email=email;
}
//gender
private String gender;
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender=gender;
}
public userPropertiesPrincipal(String name)
{
super(name);
}
}
必须重载的三个方法(红色)
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map sharedState, Map options)
public boolean login() throws LoginException
{
}protected Principal getIdentity()
{可能需要重载的类.
protected Group[] getRoleSets() throws LoginException
{}
具体设置步骤:在login-config.xml里加入,通过principalClass来制定自己定制的身份类.也注意加入了userPropertiesQuery,这个需要在initialize方法中从Map options中取出来,如下:
super.initialize(subject, callbackHandler, sharedState, options);
userPropertiesQuery=(String ) options.get("userPropertiesQuery");
if(userPropertiesQuery==null)
userPropertiesQuery="select * from sysUser where userid=?";
<application-policy name = "seamAC">
<authentication>
<login-module code = "org.security.login.jass.StandardLoginModule"
flag = "required">
<module-option name = "dsJndiName">java:/SecMasterDS</module-option>
<module-option name = "principalsQuery">select password from sysUser where userid=? </module-option>
<module-option name = "userPropertiesQuery">select * from sysUser where userid=? </module-option>
<module-option name = "rolesQuery">SELECT A.roleID as Role,'Roles' FROM sysRoles A,UserRoles B WHERE A.roleID=B.roleID AND B.userId=?</module-option>
<module-option name = "debug">true</module-option>
<module-option name="principalClass">org.security.login.jass.userPropertiesPrincipal</module-option>
<!--Password is md5 encrypt-->
<module-option name="hashAlgorithm">MD5</module-option>
<module-option name ="hashEncoding">HEX</module-option>
</login-module>
</authentication>
</application-policy>
Web.xml中必须指明使用使用JAAS Security Manage RealM
<Realm className="org.jboss.web.tomcat.security.JBossSecurityMgrRealm"
certificatePrincipal="org.jboss.security.auth.certs.SubjectDNMapping"
allRolesMode="authOnly"
debug="99"
/>