KMPËã·¨µÄJavaʵÏÖÀý×ÓÒÔ¼°²âÊÔ·ÖÎö
±³¾°¼ò½é£ºKMPËã·¨ÓÃÀ´´¦Àí×Ö·û´®Æ¥ÅäµÄ¡£¸øÄãA,BÁ½¸ö×Ö·û´®£¬¼ì²éB´®ÊÇ·ñÊÇA´®µÄ×Ó´®£¬ÀàËÆÓÚJavaµÄString.indexOf("")¡£Ö®ËùÒÔ½Ð×öKMP£¬ÊÇÒòΪÕâ¸öËã·¨ÊÇÓÉKnuth¡¢Morris¡¢PrattÈý¸öÌá³öÀ´µÄ£¬È¡ÁËÕâÈý¸öÈ˵ÄÃû×ÖµÄÍ·Ò»¸ö×Öĸ¡£
ÔÀí½éÉÜ£ºÕÒµ½Æ¥Åäʧ°ÜʱµÄ×îºÏÊʵĻØÍËλÖ㬶ø²»ÊǼòµ¥µÄ»ØÍ˵½×Ó´®µÄµÚÒ»¸ö×Ö·û£¨³£¹æµÄö¾Ù²éÕÒ·½Ê½£¬ÊǼòµ¥µÄ»ØÍ˵½×Ó´®µÄµÚÒ»¸ö×Ö·û£¬½ÓÏÂÀ´×¼±¸Ð´Ò»ÆªKMPËã·¨µÄÐÔÄÜ·ÖÎöJavaʵÏÖʵÀý£©£¬¼´¿ÉÌá¸ß²éÕÒµÄЧÂÊ¡£Òò´ËΪÁËÕÒµ½Õâ¸öºÏÊʵÄλÖã¬ÏȶÔ×Ó´®Ô¤´¦Àí£¬´Ó¶øµÃµ½Ò»¸ö»ØÍËλÖõÄÊý×é¡£¹ý¶àµÄÀíÂ۾Ͳ»½éÉÜÁË¡£
×ÜÌå¶øÑԱȽϼòµ¥£¬KMPËãÒ»¸ö¾µäµÄËã·¨Àý×Ó£¬ºÜ¶à±ÊÊÔ¡¢ÃæÊÔÒ²»áÎÊÆð¡£ÏÖ×ܽáһϣ¬·ÅÔÚÕâÀ﹩´ó¼Ò²Î¿¼¡¢½»Á÷£¬Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú£¬ÏÂÃæÖ±½Ó¸ø³öʵÏÖÀý×Ó£¬²âÊÔÓë·ÖÎöÒ²°üº¬ÆäÖС£
Ò»¡¢Ò»¸öÎļþÔ´´úÂë
KMP.java
Ô´´úÂëΪ£º
package algorithm.kmp;
/**
* KMPËã·¨µÄJavaʵÏÖÀý×ÓÓë²âÊÔ¡¢·ÖÎö
* @author ´ÞÎÀ±ø
* @date 2009-3-25
*/
public class KMP {
/**
* ¶Ô×Ó´®¼ÓÒÔÔ¤´¦Àí£¬´Ó¶øÕÒµ½Æ¥Åäʧ°Üʱ×Ó´®»ØÍ˵ÄλÖÃ
* ÕÒµ½Æ¥Åäʧ°ÜʱµÄ×îºÏÊʵĻØÍËλÖ㬶ø²»ÊÇ»ØÍ˵½×Ó´®µÄµÚÒ»¸ö×Ö·û£¬¼´¿ÉÌá¸ß²éÕÒµÄЧÂÊ
* Òò´ËΪÁËÕÒµ½Õâ¸öºÏÊʵÄλÖã¬ÏȶÔ×Ó´®Ô¤´¦Àí£¬´Ó¶øµÃµ½Ò»¸ö»ØÍËλÖõÄÊý×é
* @param B£¬´ý²éÕÒ×Ó´®µÄcharÊý×é
* @return
*/
public static int[] preProcess(char [] B) {
int size = B.length;
int[] P = new int[size];
P[0]=0;
int j=0;
//ÿѻ·Ò»´Î£¬¾Í»áÕÒµ½Ò»¸ö»ØÍËλÖÃ
for(int i=1;i<size;i++){
//µ±ÕÒµ½µÚÒ»¸öÆ¥ÅäµÄ×Ö·ûʱ£¬¼´j>0ʱ²Å»áÖ´ÐÐÕâ¸öÑ»·
//»òÕß˵p2ÖеÄj++»áÔÚp1֮ǰִÐУ¨ÏÞÓÚµÚÒ»´ÎÖ´ÐеÄÌõ¼þÏ£©
//p1
while(j>0 && B[j]!=B[i]){
j=P[j];
}
//p2£¬ÓÉ´Ë¿ÉÒÔ¿´³ö£¬Ö»Óе±×Ó´®Öк¬ÓÐÖظ´×Ö·ûʱ£¬»ØÍ˵ÄλÖòŻᱻÓÅ»¯
if(B[j]==B[i]){
j++;
}
//ÕÒµ½Ò»¸ö»ØÍËλÖÃj£¬°ÑÆä·ÅÈëP[i]ÖÐ
P[i]=j;
}
return P;
&n
Ïà¹ØÎĵµ£º
¹Ø¼ü×Ö: java.e800.com.cn/articles/2007/413/1176400876794405339_1.htm
http://java.e800.com.cn/articles/2007/413/1176400876794405339_1.html
±àÕß°´£ºJavaÄÚ´æй©ÊÇÿ¸öJava³ÌÐòÔ±¶¼»áÓöµ½µÄÎÊÌ⣬³ÌÐòÔÚ±¾µØÔËÐÐÒ»ÇÐÕý³££¬¿ÉÊDz¼Êðµ½Ô¶¶Ë¾Í»á³öÏÖÄÚ´æÎÞÏÞÖƵÄÔö³¤£¬×îºóϵͳ̱»¾£¬ÄÇôÈçºÎ×î¿ì×îºÃµÄ¼ì²â³ÌÐòµÄÎ ......
·½·¨µÄÖØÔØ
£ºÍ¬Ò»¸öÀàÀïÃæ·½·¨µÄÃû×ÖÏàͬ£¬·½·¨µÄ²ÎÊýÏÖ÷ÒªÊDzÎÊýÀàÐÍ£¬²ÎÊý¸öÊý£©
²»Í¬
,
·µ»ØÀàÐÍ¿ÉÄܲ»Í¬¡£
ÖØÔØ·½·¨¿ÉÒÔ¾ßÓв»Í¬µÄ·µ»ØÀàÐÍ£¬µ«·µ»ØÀàÐͱ¾Éí²»×ãÒÔÇø·Ö·½·¨µÄÁ½¸ö°æ
±¾¡£ ......
package test;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class XmlTest {
public static void main(String[] args) {
File xmlFile=new File("test/xml.xml");
DocumentBuilderFactory documentBuilderFactor ......
µÚÒ»£¬Ì¸Ì¸final, finally, finalizeµÄÇø±ð¡£
¡¡¡¡µÚ¶þ£¬Anonymous Inner Class (ÄäÃûÄÚ²¿Àà) ÊÇ·ñ¿ÉÒÔextends(¼Ì³Ð)ÆäËüÀ࣬ÊÇ·ñ¿ÉÒÔimplements(ʵÏÖ)interface(½Ó¿Ú)?
¡¡¡¡µÚÈý£¬Static Nested Class ºÍ Inner ClassµÄ²»Í¬£¬ËµµÃÔ½¶àÔ½ºÃ(ÃæÊÔÌâÓеĺÜÁýͳ)¡£
¡¡¡¡µÚËÄ£ ......