在列表中打印出最长的回文

时间:2016-03-02 13:54:09

标签: java

我想知道是否有人可以指出我正确的方向。

我有一个超过400,000个单词的外部文本文件,目的是打印出我所做的每个单词的回文,但现在我想弄清楚如何收集10个最长的回文。打印到控制台的所有回文,并通过将它们打印到控制台来分离前10个。

如果有人能让我开始,我会画一个空白!

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Palindrome {

  public static void main(String[] args) {
    // store external text file into a new File object
    File file = new File("dict.txt");

    try {
        // new Scanner object to read external file
        Scanner sc = new Scanner(file);
        while (sc.hasNextLine()) {
            // read each word so long as there is a word on subsequent line
            String word = sc.nextLine();
            // if the word is a palindrome
            if (isPalindrome(word)) {
                // print out the palindrome word to console
                System.out.println(word + " is a palindrome");
            }
        }
    } catch(FileNotFoundException fnfe) {
        // if file is not found, print error to console
        System.out.println(fnfe.toString());
    }
  } // end main

  public static boolean isPalindrome(String word) {
    // if there is no word
    if (word == null || word.length() == 0) {
        // return false
        return false;
    }
    // StringBuilder to hold a variable of the reverse of each word
    String reverse = new StringBuilder(word).reverse().toString();
    // if the reversed word equals the original word
    if (reverse.equals(word)) {
        // it is a palindrome
        return true;
    }

    // return false if no palindrome found
    return false;
  } // end isPalindrome


} // end class Palindrome

提前感谢任何建议!

3 个答案:

答案 0 :(得分:1)

设置和贴图很好,但如果你的单词列表很长,它们的内存很贵。如果你只需要top-n代表低n(比方说,n = 10),那么以下几项计数就更好了:

ul li:hover img {
  transform: scale(1.4) translateZ(0);
  transition: transform 0.6s ease 0s;
  -webkit-margin-top-collapse:discard;
}

查找比集合(// to compare strings by reverse length, and then alphabetically private static final Comparator<String> lengthComparator = new Comparator<String>(){ public int compare(String a, String b) { int c = b.length() - a.length(); return c==0 ? a.compareTo(b) : c; } }; // to update the top-n list. Pass in an empty list at the start public static void updateTop10(String word, ArrayList<String> top, int n) { int index = Collections.binarySearch(top, word, lengthComparator); System.out.println("found at " + index + ": " + word); if (index >= 0) { // word already in list; no need to add it return; } else if (top.size()<n) { // list not full - add it in top.add(-1-index, word); } else if (word.length() > top.get(n-1).length()) { // larger than smallest word in list - insert into position top.remove(n-1); top.add(-1-index, word); } } )更快 - 但你的 n 是10),而不是字典的大小。最坏的情况是在前面插入,但是移动9个元素非常便宜,并且可能比O(log(n)遍历+插入要好得多。

答案 1 :(得分:0)

例如,您可以收集集合中的所有回文并按尺寸分组:

Map<Integer, Set<String>> palindromes = new HashMap<>();

当你找到一个回文添加它时:

palindromes.computeIfAbsent(word.length(), f -> new HashSet<>()).add(word);

在循环结束时,你可以找到地图的最大键,在Set中你可以找到单词。

答案 2 :(得分:0)

可能有几种方法可以实现这一目标,但首先想到的是将所有回文收集到一个集合中,然后按长度对它们进行排序以找到最长的10个。

因此,在对if的{​​{1}}阻止调用中,您可以将isPalindrome添加到您的收藏中。然后在word循环之后,对列表进行排序。我无法记住为默认的Java while方法提供自定义排序规则是多么容易/多难。

相关问题