码迷,mamicode.com
首页 > 编程语言 > 详细

hdu 2594 java实现字符串KMP算法

时间:2015-04-12 00:04:34      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:java   kmp算法 next函数 字符串匹配 h   

Problem Description
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
 

Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
 

Output
Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.
 

Sample Input
clinton homer riemann marjorie
 

Sample Output
0 rie 3
解题思路:
这是很典型的字符串匹配,我们使用KMP算法进行解题,先求出模式串的next[]函数,然后用kmp函数结合next[]函数解题,kmp函数返回一个整型数,然后对这个数进行判断,最后输出相匹配的字符串和长度(即kmp返回的数)。
注意:本体采用java编写,所以特别初一超时和超内存问题。(1):直接对字符串进行操作,不用转换成别的类型;(2)输出的时候不能用for输出,不然绝对超内存,要用str.substring(int index,int length)函数进行输出。
代码实现:
(1):next函数
static int[] getnext(){                          //getnext函数,获得next[]数组的值
    int i=0,j=-1;  
    next[0]=-1;                                     
    while(i<strs.length()){  
        if(j==-1||strs.charAt(i)==strs.charAt(j))    /* 特别注意:j==-1必须在前,不然后面的语句可能会挂掉,
                                                        需要在挂掉之前必须跳出if,或者出现越界的错误。   */
        {                                           //满足匹配或者j(即上个循环的next[j]的值)的值为-1,
            i++;  
            j++;  
            next[i]=j;                              //把j的值赋值给next[i]
        }  
        else 
            j=next[j];                             //寻找j的所在位置的next值
    }
    return next;
  }

(2):kmp函数
static int  kmp(){
  	   int i=0,j=0;  
    while(i<strt.length()&&j<strt.length())  
    {  
        if(j==-1||j<strs.length()&&strs.charAt(j)==strt.charAt(i))  /* 特别注意:j==-1语句和j<strs.length()语句必须在前,
                                                                       不然后面的strs.charAt(j)==strt.charAt(i)语句可能会挂掉,
                                                                       需要在挂掉之前必须跳出if,或者出现越界的错误。       */
        {  
            i++;  
            j++;  
        }  
        else  
            j=next[j];                  //匹配失败,j不回0,而是跳到next[j]所在位置继续匹配,这正是KMP算法比普通算法优化的地方
    }
    return j;
  }

完整代码:
import java.util.Scanner;
class Main{
	  static  String strs;
	  static  String strt;
	  static int[]  next=new int [50001];
    public static void main(String agrs[]){
       Scanner sc=new Scanner(System.in);
       while(sc.hasNext()){
      	   strs=sc.nextLine();
      	   strt=sc.nextLine();
      	  getnext();
      	 int m=kmp();  
		    if(m==0)  
		        System.out.println("0");  
		    else  
		    {  
      	 	  System.out.println(strs.substring(0,m)+" "+m);        //使用strs.substring()函数,优点是为了防止超内存
      	}
      }
   }
   static int[] getnext(){                          //getnext函数,获得next[]数组的值
    int i=0,j=-1;  
    next[0]=-1;                                     
    while(i<strs.length()){  
        if(j==-1||strs.charAt(i)==strs.charAt(j))    /* 特别注意:j==-1必须在前,不然后面的语句可能会挂掉,
                                                        需要在挂掉之前必须跳出if,或者出现越界的错误。   */
        {                                           //满足匹配或者j(即上个循环的next[j]的值)的值为-1,
            i++;  
            j++;  
            next[i]=j;                              //把j的值赋值给next[i]
        }  
        else 
            j=next[j];                             //寻找j的所在位置的next值
    }
    return next;
  }
  static int  kmp(){
  	   int i=0,j=0;  
    while(i<strt.length()&&j<strt.length())  
    {  
        if(j==-1||j<strs.length()&&strs.charAt(j)==strt.charAt(i))  /* 特别注意:j==-1语句和j<strs.length()语句必须在前,
                                                                       不然后面的strs.charAt(j)==strt.charAt(i)语句可能会挂掉,
                                                                       需要在挂掉之前必须跳出if,或者出现越界的错误。       */
        {  
            i++;  
            j++;  
        }  
        else  
            j=next[j];                  //匹配失败,j不回0,而是跳到next[j]所在位置继续匹配,这正是KMP算法比普通算法优化的地方
    }
    return j;
  }
}


hdu 2594 java实现字符串KMP算法

标签:java   kmp算法 next函数 字符串匹配 h   

原文地址:http://blog.csdn.net/xionghui2013/article/details/45000645

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!