java 按字母顺序排序-姓名按字母排序
发布时间:2023-02-10 10:39 浏览次数:次 作者:佚名
根据字典顺序对字符串数组进行排序并使用自定义排序(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 leftList = new ArrayList();
List rightList = new ArrayList();
List result = new ArrayList();
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 的排列和单词集合 Sjava 按字母顺序排序,您需要根据新字母 E 输出集合 S 中的单词的字典顺序。
进入:
第一行将包含一个整数 T,表示测试用例的数量。
对于每个测试用例:
第一行将包含字符串 E,新的字母顺序,它将是 abcdefghijklmnopqrstuvwxyz 的排列
下一行将包含一个整数 M,大小为 S。接下来是 S 行java 按字母顺序排序,每行包含一个单词,包含小写拉丁字符。
输出:对于每个测试用例,输出 S 行,每行包含集合 S 中的一个单词,按字典顺序排列。
约束条件
示例输入:
2
abcdefghijklmnopqrstuvwxyz
2
aa
bb
bacdefghijklmnopqrstuvwxyz
2
aa
bb
示例输出:
aa
bb
bb
aa