2015-04-07 42 views
2

我需要從文件中讀取內容並找到可以從文件中存在的其他單詞形成的最長單詞。文件中的單詞是空格分開的。例如:Java中的子串操作 - 查找由其他單詞構成的最長單詞

從文件輸入:

This is example an anexample Thisisanexample Thisistheexample 

輸出:

Thisisanexample 

注:形成的最長的單詞是Thisisanexample和不Thisistheexample因爲單詞the不包含作爲單獨的單詞在文件中。

這是可能的通過使用簡單的數組?我也做了以下情況:

try{ 
     File file = new File(args[0]); //command line argument for file path 
     br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 
     String line = null; 
     //array for each word 
     String[] words = new String[] {}; 
     while ((line = br.readLine()) != null){ 
      words = line.split("\\s+"); //splitting the string with spaces 
     } 
     // array to store length of each word 
     int[] wordLength = new int[words.length]; 
     for(int i = 0; i < words.length; i++){ 
      wordLength[i] = words[i].length(); 
     } 

     int currLength = 0; //store length of current word 
     int maxLength = 0; //store length of max word 
     String maxWord = null; 

     //checking each word with others at O(n*n) complexity 
     for (int i = 0; i < words.length; i++){ 
      currLength = 0; 
      for (int j = 0; j < words.length && j != i; j++){ 
       if (words[i].contains(words[j])){ 
        currLength += wordLength[j]; 
       } 
      } 
      System.out.println(currLength); 
      if(currLength > maxLength){ 
       maxLength = currLength; 
       maxWord = words[i]; 
      } 
     } 
     System.out.println(maxWord); 
    } 

但如果有一個子withing一個子這不起作用。它會給以下輸入輸出錯誤:

This is example an anexample Thisisanexample Thisisanexample2 

輸出應該Thisisanexample但它給Thisisanexample2

+2

純粹的代碼寫入請求在堆棧溢出上偏離主題 - 我們期望 這裏的問題與*特定的*編程問題有關 - 但我們 將很高興地幫助您自己編寫它!告訴我們 [你試過的東西](http://whathaveyoutried.com),以及你卡在哪裏。 這也將幫助我們更好地回答你的問題。 –

+0

重複? http://stackoverflow.com/questions/17387218/find-first-longest-word-in-a-string-excluding-symbols –

+0

@ huanfeng,這與你所鏈接的不一樣。 – titan7585

回答

0

在其他堆棧溢出線程的幫助下,我設法通過僅使用數組來完成此操作。

這裏是解決方案:

import java.io.*; 
import java.util.*; 

public class LongestWord implements Comparator<String>{ 
    //compare function to be used for sorting the array according to word length 
    public int compare(String s1, String s2) { 
     if (s1.length() < s2.length()) 
      return 1; 
    else if (s1.length() > s2.length()) 
     return -1; 
    else 
     return 0; 
} 

public static void main(String[] args){ 
    BufferedReader br = null; 
    try{ 
     File file = new File(args[0]); 
     br = new BufferedReader(new InputStreamReader(new  FileInputStream(file))); 
     String line = null; 
     //array for each word 
     String[] words = new String[] {}; 
     while ((line = br.readLine()) != null){ 
      words = line.split("\\s+"); //splitting the string with spaces 
     } 

     //sort the array according to length of words in descending order 
     Arrays.sort(words, new LongestWord()); 

     /* start with the longest word in the array and check if the other words are its substring. 
     * If substring, then remove that part from the superstring. 
     * Finally, if the superstring length is 0, then it is the longest word that can be formed.*/ 
     for (String superString: words) { 
      String current = new String(superString); // to store a copy of the current superstring as we will remove parts of the actual superstring 
      for (String subString: words) { 
       if (!subString.equals(current) && superString.contains(subString)) { // superstring contains substring 
        superString = superString.replace(subString, ""); // remove the substring part from the superstring 
       } 
      } 

      if (superString.length() == 0){ 
       System.out.println(current); 
       break; // since the array is sorted, the first word that returns length 0 is the longest word formed 
      } 
     } 
    } 
    catch(FileNotFoundException fex){ 
     System.out.println("File not found"); 
     return; 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     try { 
      if (br != null){ 
       br.close(); 
      } 
     } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 
1

隨着代碼只需幾行,你可以使用正則表達式來找到候選「組合」的話,那麼簡單的邏輯來找到最長的匹配:

String longest = ""; 
Matcher m = Pattern.compile("(?i)\\b(this|is|an|example)+\\b").matcher(input); 
while (m.find()) 
    if (m.group().length() > longest.length()) 
     longest = m.group(); 

除了從文件中讀取代碼並將字符串賦值給變量input之外,這就是您需要的所有代碼。