【问题标题】:Sorting strings in custom lexicographic order按自定义字典顺序对字符串进行排序
【发布时间】:2014-02-08 13:17:42
【问题描述】:

使用自定义排序(abcdefghijklmnopqrstuvwxyz 的排列)根据字典顺序对字符串数组进行排序。这是代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author sabertooth
 */
public class SortString {
    /**
     * @param args the command line arguments
     */
    private static char[] index;
    private static BufferedReader br;

    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        br = new BufferedReader(new InputStreamReader(System.in));
        int testCases = Integer.parseInt(br.readLine());

        for (int i = 0; i < testCases; i++) {
            String dictionary = br.readLine();

            index = new char[dictionary.length()];
            index = dictionary.toCharArray();

            int set = Integer.parseInt(br.readLine());

            String[] unsortedInput = new String[set];
            String[] sortedInput = new String[set];
            for (int j = 0; j < set; j++) {
                unsortedInput[j] = br.readLine();
            }

            if (unsortedInput.length <= 1) {
                System.out.println(unsortedInput[0]);
            } else {
                // merge sort on this array
                sortedInput = mergeSort(unsortedInput);
                for (int k = 0; k < sortedInput.length; k++) {
                    System.out.println(sortedInput[k]);
                }
            }
        }
    }

    private static String[] mergeSort(String[] unsortedInput) {
        if (unsortedInput.length <= 1) {
            return unsortedInput;
        }

        String[] left;
        String[] right;
        int middle = unsortedInput.length / 2;
        if (unsortedInput.length % 2 == 0) {
            left = new String[middle];
            right = new String[middle];
        } else {
            left = new String[middle];
            right = new String[middle + 1];
        }
        System.arraycopy(unsortedInput, 0, left, 0, middle);
        System.arraycopy(unsortedInput, middle, right, 0, unsortedInput.length - middle);

        left = mergeSort(left);
        right = mergeSort(right);
        return merge(left, right);
    }

    private static String[] merge(String[] left, String[] right){
        List<String> leftList = new ArrayList<String>();
        List<String> rightList = new ArrayList<String>();
        List<String> result = new ArrayList<String>();

        leftList.addAll(Arrays.asList(left));
        rightList.addAll(Arrays.asList(right));

        while (leftList.size() > 0 || rightList.size() > 0) {
             if (leftList.size() > 0 && rightList.size() > 0) {
                // my own comparison
                if (compare(leftList.get(0), rightList.get(0)) == -1) {
                    // leftString is less than right string
                    result.add(leftList.get(0));
                    leftList = leftList.subList(1, leftList.size());
                } else
                if (compare(leftList.get(0), rightList.get(0)) == 1) {
                    //left string is greater than right string
                    result.add(rightList.get(0));
                    rightList = rightList.subList(1, rightList.size());
                } else
                if (compare(leftList.get(0), rightList.get(0)) == 0) {
                    // leftString is equal to right string
                    result.add(leftList.get(0));
                    leftList = leftList.subList(1, leftList.size());
                }
            } else
            if (leftList.size() > 0) {
                for (int i = 0; i < leftList.size(); i++) {
                    result.add(leftList.get(i));
                }
                leftList.clear();
            } else
            if (rightList.size() > 0) {
                for (int i = 0; i < rightList.size(); i++) {
                    result.add(rightList.get(i));
                }
                rightList.clear();
            }
        }
        String[] sortedInput = new String[result.size()];
        for (int i = 0; i < result.size(); i++) {
            sortedInput[i] = result.get(i);
        }
        return sortedInput;
    }

    private static int compare(String leftString, String rightString) {
        // return -1 if left string is less than right string else left string is greater than right string return 1

        int min = Math.min(leftString.length(), rightString.length());
        int response = 0;
        for (int i = 0; i < min; i++) {
            if (compareChar(leftString.charAt(i), rightString.charAt(i)) == -1) {
                response = -1;
                break;
            } else
            if (compareChar(leftString.charAt(i), rightString.charAt(i)) == 1) {
                response = 1;
                break;
            } else
            if (compareChar(leftString.charAt(i), rightString.charAt(i)) == 0) {
                response = 0;

            }
        }
        return response;
    }

    private static int compareChar(char x, char y) {
        // returns true if x < y
        int indexofx = 0;
        int indexofy = 0;
        int response = 0;
        for (int i = 0; i < index.length; i++) {
            if (index[i] == x) {
                indexofx = i;
            }
            if (index[i] == y) {
                indexofy = i;
            }
        }
        if (indexofx < indexofy) {
            response = -1;
        } else
        if (indexofx > indexofy) {
            response = 1;
        } else
        if (indexofx == indexofy) {
            response = 0;
        }
        return response;
    }
}

问题是当我对某些输入运行此命令时,输出是正确的,而对于其他输入,则输出不正确。我一直在调试它,但找不到错误。

编辑

Adriana 正在玩英文字母表。当她玩完字母表时,她意识到她把字母的位置弄乱了。现在,给定一组单词,她想知道根据她制作的新字母顺序,这些单词的字典顺序是什么。

换句话说,给定英文字母表 E 和一组单词 S 的排列,您需要根据新字母表 E 输出集合 S 中单词的字典顺序。

输入:

第一行将包含一个整数 T,表示测试用例的数量。 T 线紧随其后。

对于每个测试用例:

第一行将包含一个字符串,E,新的字母顺序,将是abcdefghijklmnopqrstuvwxyz的排列

下一行将包含一个整数 M,集合 S 的大小。接下来是 S 行,每行包含一个单词,包含小写拉丁字符。

输出:对于每个测试用例,输出 S 行,每行包含集合 S 中的一个单词,按字典顺序排列。

约束

1 <= T <= 1000 
1 <= M <= 100 
1 <= |W| <= 50

示例输入:

2
abcdefghijklmnopqrstuvwxyz
2
aa
bb
bacdefghijklmnopqrstuvwxyz
2
aa
bb

样本输出:

aa
bb
bb
aa

【问题讨论】:

  • 可以举个例子,当它不能正常工作时
  • 很难猜测(并从程序代码中破译)您正在尝试做什么。如果您可以提供您想要实现的目标,哪些输入被正确排序(如果这就是您所说的“正确输出”)以及哪些输入没有正确输出?
  • @Horizo​​n 查看编辑内容
  • 请显示一些示例输入和正确/错误输出
  • @AbhishekBansal 检查编辑

标签: java algorithm sorting mergesort


【解决方案1】:

这应该适用于任何给定的排列。当 java 为您提供内置排序功能时,为什么要使用自定义排序功能(除非您必须对自定义类对象进行排序)?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;

class TestClass {
    public static ArrayList<String> res;
    static void sort(String dictionary, String[] words, int count){
        String eng = "abcdefghijklmnopqrstuvwxyz";
        String[] tempArray = new String[count];
        String temp="";
        char ch;
        int index, m=0;
        for(String x : words){
            temp = "";
            for(int l =0 ;l<x.length(); l++){
                ch = x.charAt(l);
                index = dictionary.indexOf(ch);
                temp = temp + eng.charAt(index);
            }
            tempArray[m] = temp;
            m++;
        }

        Arrays.sort(tempArray);
        for(String x : tempArray){
            temp = "";
            for(int l =0 ;l<x.length(); l++){
                ch = x.charAt(l);
                index = eng.indexOf(ch);
                temp = temp + dictionary.charAt(index);
            }
            res.add(temp);

        }
    }
    public static void main(String args[] ) throws Exception {
        res = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        int count;
        String [] words;
        String dictionary;
        for (int i = 0; i < N; i++) {
            dictionary = br.readLine();
            count = Integer.parseInt(br.readLine());
            words = new String[count];
            for(int j =0; j<count; j++){
                words[j] = br.readLine();
            }
            sort(dictionary,words,count);
        }
        for(String x : res)
        System.out.println(x);
    }
}

我想这是某种算法挑战。是吗? :)

【讨论】:

  • 字典字符串可能每次都不一样。可能是 qwertyuiopasdfghjklzxcvbnm 然后我必须根据这个字符串排序,即'q'第一个'w'第二个等等。我解决了这个问题,尽管当两个字符串相等且长度不同时,字符串长度存在一些错误。例如,字符串“q”将出现在字符串“qq”之前。
  • 是的,我的解决方案适用于任何字母顺序排列。 (除非您没有尝试运行提供的代码。)字典字符串不需要一直相同。 :) 每个测试用例的字典将传递给排序方法。(请参阅传递的参数。)提供的字典 eng 用于替换。(意味着如果传递的字典是 qwertyuiopasdfghjklzxcvbnm 则输入说,'q',' qq',程序检查被传递的字典中 q 的索引,并以原始英文字母顺序查找相应的字母。)
  • Drive-by-comment:只写一个自定义比较器不行吗? O:)
【解决方案2】:

字符串比较函数不正确:您只比较最短长度,对于AAB,返回0。当一个字符串是另一个字符串的前缀时,你应该比较长度。

字符比较函数过于复杂,它认为index 之外的所有字符都与index 中的第一个字母相同。您应该只根据index 字符串重新排列小写字母。

    private static int compare(String leftString, String rightString) {
        // return -1 if left string is less than right string else left string is greater than right string return 1

        int len1 = leftString.length();
        int len2 = rightString.length();
        int min = Math.min(len1, len2);
        for (int i = 0; i < min; i++) {
            int cmp = compareChar(leftString.charAt(i), rightString.charAt(i));
            if (cmp != 0)
                return cmp;
        }
        return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
    }

    /* lexicographical comparison with custom order for lowercase letters */
    private static int compareChar(char x, char y) {
        if (x == y)
            return 0;
        int indexofx = index.indexOf(x);
        int indexofy = index.indexOf(y);
        if (indexofx >= 0)
            x = 'a' + indexofx;
        if (indexofy >= 0)
            y = 'a' + indexofy;
        return x < y ? -1 : 1;
    }
}

【讨论】:

    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    相关资源
    最近更新 更多