【问题标题】:Substring manipulation in Java - Find the longest word made from the other wordsJava中的子字符串操作 - 查找由其他单词组成的最长单词
【发布时间】:2015-04-07 02:56:31
【问题描述】:

我需要从文件中读取内容并找到可以由文件中存在的其他单词组成的最长单词。文件中的单词以空格分隔。例如:

文件输入:

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);
    }

但是如果有一个带有子字符串的子字符串,这将不起作用。它将为以下输入提供错误的输出:

This is example an anexample Thisisanexample Thisisanexample2

输出应该是Thisisanexample,但它给出了Thisisanexample2

【问题讨论】:

  • 纯代码编写请求在 Stack Overflow 上是题外话——我们希望这里的问题与特定编程问题有关——但我们很乐意帮助您自己编写!告诉我们what you've tried,以及您遇到的问题。这也将有助于我们更好地回答您的问题。
  • @huanfeng,和你链接的不一样。
  • @ElliottFrisch ,我已经添加了我写的代码。
  • @titan7585 我明白了,这是一个算法问题,希望有人能帮助你:)

标签: java substring


【解决方案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,这就是您需要的所有代码。

【讨论】:

    【解决方案2】:

    在其他 Stack Overflow 线程的帮助下,我设法仅通过使用数组来做到这一点。

    解决方法如下:

    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();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多