1、用戶模型文件去重。
抖音上不同的用戶類型我們有不同的用戶模型文件。我們有一個模型配置文件,裏面有很多的不同用戶類型和他們對應的模型文件。我們需要找出每個模型文件對應的是哪些用戶類型。
給定一行輸入,格式爲:
a b
a表示這個用戶的用戶類型,b表示這個用戶對應的模型文件。請你輸出每個模型文件對應的用戶類型。
注意:每個模型文件可能對應多個用戶類型,用戶類型之間用空格作爲切分。
注意2:如果有多個用戶類型輸出,用戶類型之間的排序按照字母表排序。
eg:
輸入:
1
abc 1.txt
輸出如下:
1.txt abc
public class test020 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int lineNum = sc.nextInt();sc.nextLine(); Map<String, Set<String>> map = new TreeMap<>(); for(int i=0; i<lineNum; i++){ String[] line = sc.nextLine().trim().split(" "); if(map.containsKey(line [1])){ map.get(line[1]).add(line[0]); }else { Set<String> set = new TreeSet<>(); set.add(line[0]); map.put(line[1], set); } } for(Map.Entry<String, Set<String>> entry: map.entrySet()){ Set<String> set = entry.getValue(); List<String> strings = new ArrayList<>(set); // Collections.sort(strings); System.out.print(entry.getKey()+" "); for(String s: strings) System.out.print(s+" "); System.out.println(); } } }
2、 貪心算法,補給飲用水
假設你要從0點開始沙漠旅行到終點D,總共D個站點,每走一公里消耗一升的水量,初始攜帶的水量爲W,但是在路途中是可以補給到一些水的,這些站點的位置在pos數組中給出,在sup數組的對應位置給出了在站點能獲得的水量多少。即:在pos[i] 站點能獲得的水量爲sup[i],現在要求最少需要加多少次水,如果在路途中會被渴死,直接返回-1
public class test021 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int D = sc.nextInt(); int W = sc.nextInt(); int[] pos = new int[3]; int[] sup = new int[3]; for(int i=0; i<3; i++){ pos[i] = sc.nextInt(); } for(int i=0; i<3; i++){ sup[i] = sc.nextInt(); } System.out.print(D+" "); System.out.print(W); System.out.println(); for (int j=0; j<3; j++){ System.out.print(pos[j]+" "); } System.out.println(); for (int k=0; k<3; k++){ System.out.print(sup[k]+" "); } System.out.println(); int res = solve(D, W, pos, sup); System.out.println("次數最少爲:"+res+"次"); } public static int solve(int D, int W, int[] pos, int[] sup){ int res = 0; // 用來指示在特定的水站有沒有取過水,一個水站只能取一次水 boolean[] used = new boolean[pos.length]; // 旅行者現在所在的位置 int curPos = 0; while (curPos<D){ // 每次直接跳到能夠着的最大位置,攜帶的水也會被喝光 curPos+=W; W = 0; // 如果已經到達終點,則直接返回加了多少次水 if(curPos>=D) return res; // 標記一下能獲得最多水的水站在pos中的下標 int maxIndex = -1; for(int i=0;i<pos.length;i++){ // 當前還沒到指定的水站,則不能從這些水站取水,直接break if(pos[i]>curPos) break; // 如果還沒從該水站取水,則會看在這裏取水能否得到最大的水量 if(!used[i] && sup[i]>W) { W = sup[i]; maxIndex = i; System.out.println("W="+W); System.out.println("maxIndex="+maxIndex); } } // 沒水了,而且也沒有水站可以取水,可能在路途上被渴死 if(maxIndex==-1) return -1; used[maxIndex] = true; res++; } return res; } }