2010-12-15 8 views
0

文字列で最長重複部分文字列を検索するためのJava機能が必要文字列中の最も長い複製された部分文字列を見つけるのにJava関数が必要ですか?

For instance, if the input is “banana”,output should be "ana" and we have count the number of times it has appeared in this case it is 2.

溶液を

パブリッククラスTest {
パブリック静的無効メイン(文字列[] args){
以下の通りであります System.out.println(findLongestSubstring( "ike"が好きです));
System.out.println(findLongestSubstring( "madam i am adam"));
System.out.println(findLongestSubstring( "人生がレモネードを手にするときは、レモンを作る"));
System.out.println(findLongestSubstring( "banana")); (lijie用)
}

public static String findLongestSubstring(String value) { 
    String[] strings = new String[value.length()]; 
    String longestSub = ""; 

    //strip off a character, add new string to array 
    for(int i = 0; i < value.length(); i++){ 
     strings[i] = new String(value.substring(i)); 
    } 

    //debug/visualization 
    //before sort 
    for(int i = 0; i < strings.length; i++){ 
     System.out.println(strings[i]); 
    } 

    Arrays.sort(strings); 
    System.out.println(); 

    //debug/visualization 
    //after sort 
    for(int i = 0; i < strings.length; i++){ 
     System.out.println(strings[i]); 
    } 

    Vector<String> possibles = new Vector<String>(); 
    String temp = ""; 
    int curLength = 0, longestSoFar = 0; 

    /* 
    * now that the array is sorted compare the letters 
    * of the current index to those above, continue until 
    * you no longer have a match, check length and add 
    * it to the vector of possibilities 
    */ 
    for(int i = 1; i < strings.length; i++){ 
     for(int j = 0; j < strings[i-1].length(); j++){ 
      if (strings[i-1].charAt(j) != strings[i].charAt(j)){ 
       break; 
      } 
      else{ 
       temp += strings[i-1].charAt(j); 
       curLength++; 
      } 
     } 
     //this could alleviate the need for a vector 
     //since only the first and subsequent longest 
     //would be added; vector kept for simplicity 
     if (curLength >= longestSoFar){ 
      longestSoFar = curLength; 
      possibles.add(temp); 
     } 
     temp = ""; 
     curLength = 0; 
    } 

    System.out.println("Longest string length from possibles: " + longestSoFar); 

    //iterate through the vector to find the longest one 
    int max = 0; 
    for(int i = 0; i < possibles.size();i++){ 
     //debug/visualization 
     System.out.println(possibles.elementAt(i)); 
     if (possibles.elementAt(i).length() > max){ 
      max = possibles.elementAt(i).length(); 
      longestSub = possibles.elementAt(i); 
     } 
    } 
    System.out.println(); 
    //concerned with whitespace up until this point 
    // "lemon" not " lemon" for example 
    return longestSub.trim(); 
} 

}

+1

興味深い質問ですが、何か試しましたか? – khachik

+4

http://stackoverflow.com/questions/2172033/find-the-longest-repeating-string-and-the-number-of-times-it-repeats-in-a-given-s – NPE

+0

@ khachik、私はいませんどのように進行するかを知っている – Deepak

答えて

4

This is a common CS problem with a dynamic programming solution.

編集:

あなたは技術的に正しいです - これはまったく同じ問題ではありません。しかし、これは上のリンクを無関係にするものではなく、提供される両方の文字列が同じであれば同じアプローチ(特に動的プログラミング)を使用できます。対角線に沿ってケースを考慮しないでください。または、他の人が指摘しているように(LaGrandMereなど)、サフィックスツリー(上記のリンクにもあります)を使用します。 (ディーパック用)

編集:

A Java implementation of the Longest Common Substring (using dynamic programming) can be found here。 "対角"(Wikipediaの図を見てください)を無視するように変更する必要があることに注意してください。または、最も長い共通文字列がそれ自身になります。

+0

解答のため+1、aixコメント+1。 – LaGrandMere

+1

問題は_最長の共通部分文字列ではありません。少なくともマッピングは簡単ではありません。 LCS問題は_2_入力文字列の間で最も長い共通部分文字列を取得しているのに対して、この問題には入力文字列が1つしかないことに注意してください。 – lijie

+0

@lijie私をつま先につけてくれてありがとう。私は答えを更新しました。 –

1

Javaの場合:Suffix Tree

解決方法を見つけたおかげで、私は知らなかった。

関連する問題