题解 | #编码#
编码
http://www.nowcoder.com/practice/6fc8716ee33e4cc59d58d7e18712094e
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.util.Comparator; class CodeComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } } /** * 编码 * @Author zhuyq * @Date 2021-09-18 */ public class Main { private static ArrayList<String> list = new ArrayList<>(); private static String[] strArr = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"}; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(getPos(in.nextLine())); } //装值 public static void setVal(){ //1个 ArrayList<String> list0 = new ArrayList<>(); for(int i=0; i<strArr.length; i++){ list0.add(strArr[i]); list.add(strArr[i]); } //2个 for(int i=0; i<list0.size(); i++){ for(int i1=0; i1<list0.size(); i1++){ list.add(list0.get(i)+list0.get(i1)); } } //3个 for(int i=0; i<list0.size(); i++){ for(int i1=0; i1<list0.size(); i1++){ for(int i2=0; i2<list0.size(); i2++){ list.add(list0.get(i)+list0.get(i1)+list0.get(i2)); } } } //4个 for(int i=0; i<list0.size(); i++){ for(int i1=0; i1<list0.size(); i1++){ for(int i2=0; i2<list0.size(); i2++){ for(int i3=0; i3<list0.size(); i3++){ list.add(list0.get(i)+list0.get(i1)+list0.get(i2)+list0.get(i3)); } } } } } //排序 public static void sort(){ setVal(); Collections.sort(list, new CodeComparator()); } //获取字符串位置 public static int getPos(String str){ sort(); return list.indexOf(str); } }