hdu1867(A + B for you again) 杭電java a題真坑

來源:互聯網
上載者:User

標籤:hdu1867

點擊開啟連結

Problem DescriptionGenerally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions. 
InputFor each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty. 
OutputPrint the ultimate string by the book. 
Sample Input
asdf sdfgasdf ghjk
 
Sample Output
asdfgasdfghjk

解題思路:題目就是字串模式比對,注意一些細節問題就可以了。但是java寫的就是無限超記憶體,今天心情不好,再交,就過了,這是無語了啊

import java.util.*;class P1867{    static int[] next=new int[100005];    public static void main(String args[]){        int n,m,i,len1,len2;        String str1,str2;        Scanner sc=new Scanner(System.in);        while(sc.hasNext()){            str1=sc.next();            str2=sc.next();            len1=str1.length();            len2=str2.length();            n=kmp(str1,str2);            m=kmp(str2,str1);            if(n==m){                if(str1.compareTo(str2)>0){                    System.out.println(str2+str1.substring(n,len1));                }else{                    System.out.println(str1+str2.substring(n,len2));                }            }else if(n>m){                System.out.println(str1+str2.substring(n,len2));            }else{                System.out.println(str2+str1.substring(m,len1));            }        }    }    public static void set_next(String str){        int i=0,j=-1;        next[0]=-1;        int len=str.length();        while(i<len){            if(j==-1||str.charAt(i)==str.charAt(j)){                i++;                j++;                next[i]=j;///System.out.print(next[i]+" ");            }else{                j=next[j];            }        }        //System.out.println();    }    public static int kmp(String str1,String str2){        int i=0,j=0;        int len1=str1.length(),len2=str2.length();        set_next(str2);        while(i<len1){//System.out.print(j+" ");            if(j==-1||(j<len2&&str1.charAt(i)==str2.charAt(j))){                i++;                j++;//System.out.print("** ");            }else{                j=next[j];            }//System.out.print(j+"* ");        }//System.out.println();        if(i==len1){            return j;        }        return 0;    }}





著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

hdu1867(A + B for you again) 杭電java a題真坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.