Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Postup je zadaný v popise úlohy.
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class NameScores { private static Map<Character, Integer> alphabet; static { alphabet = new HashMap<>(26); alphabet.put('A', 1); alphabet.put('B', 2); alphabet.put('C', 3); alphabet.put('D', 4); alphabet.put('E', 5); alphabet.put('F', 6); alphabet.put('G', 7); alphabet.put('H', 8); alphabet.put('I', 9); alphabet.put('J', 10); alphabet.put('K', 11); alphabet.put('L', 12); alphabet.put('M', 13); alphabet.put('N', 14); alphabet.put('O', 15); alphabet.put('P', 16); alphabet.put('Q', 17); alphabet.put('R', 18); alphabet.put('S', 19); alphabet.put('T', 20); alphabet.put('U', 21); alphabet.put('V', 22); alphabet.put('W', 23); alphabet.put('X', 24); alphabet.put('Y', 25); alphabet.put('Z', 26); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int namesCount = Integer.parseInt(scanner.nextLine()); List<String> namesList = new ArrayList<>(namesCount); for (int i = 0; i < namesCount; i++) { String name = scanner.nextLine(); namesList.add(name); } Collections.sort(namesList); Map<String, Integer> names = new HashMap<>(); for (int i = 1; i <= namesList.size(); i++) { names.put(namesList.get(i - 1), i); } int queries = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < queries; i++) { String query = scanner.nextLine(); int score = nameScore(names, query); System.out.println(score); } scanner.close(); } private static int nameScore(Map<String, Integer> names, String name) { int result = 0; for (int i = 0; i < name.length(); i++) { result += alphabet.get(name.charAt(i)); } result *= names.get(name); return result; } } |