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

杭电2549(第一次用java写kmp算法)

时间:2015-04-11 17:58:38      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:java   内存   数组越界   kmp   

点击打开杭电2549

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

注意:主要要注意java里内存的控制,和数组的越界情况。(这里坑死我了!!技术分享

import java.util.*;
class Main{
    static int[] next=new int[50005];
    static String str1,str2;
    static int len1,len2;
    public static void main(String args[]){
        int n,i;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            str2=sc.nextLine();
            str1=sc.nextLine();
            len1=str1.length();
            len2=str2.length();
            n=kmp();
            if(n>0){
                System.out.print(str2.substring(0,n)+" ");//这里要是不用substring,而用for循环,就会超内存。
            }
            System.out.println(n);
        }
    }
    public static void set_next(){
        int i=0,j=-1;
        next[0]=-1;
        while(i<len2){
            if(j==-1||str2.charAt(i)==str2.charAt(j)){
                i++;
                j++;
                next[i]=j;///System.out.print(next[i]+" ");
            }else{
                j=next[j];
            }
        }
        //System.out.println();
    }
    public static int kmp(){
        int i=0,j=0;
        set_next();
        while(i<len1){//System.out.print(j+" ");
            if(j==-1||(j<len2&&str1.charAt(i)==str2.charAt(j))){//c这里不用要j<len2但是java必须要,因为java比较严格,不允许越界情况,而c允许(java通常属于解析型语言,它是一句一句解释,而c是通篇解释)。<span style="font-family: Arial, Helvetica, sans-serif;">同理</span><span style="font-family: Arial, Helvetica, sans-serif;">j==-1一定要放在前面。</span>

                i++;
                j++;//System.out.print("** ");
            }else{
                j=next[j];
            }//System.out.print(j+"* ");
        }//System.out.println();
        return j;
    }
}






杭电2549(第一次用java写kmp算法)

标签:java   内存   数组越界   kmp   

原文地址:http://blog.csdn.net/u011479875/article/details/44996567

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