2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ランクSの文字列収集の問題をJavaで短いコードでやってみる
このランクSの問題、他の問題に比べて簡単な気がします
それにしても Java には Pair みたいのがないので Map.Entry を強引に使うしか手が思いつかなかった...

import java.util.*;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int[] pq = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		List<Map.Entry<String, Integer>> pis = new ArrayList<>();

		for (int i = 0; i < pq[0]; i++) {
			String[] pi = sc.nextLine().split(" ");
			pis.add(new AbstractMap.SimpleEntry<>(pi[0], Integer.parseInt(pi[1])));
		}

		for (int i = 0; i < pq[1]; i++) {
			String q = sc.nextLine();
			System.out.println(pis.stream().filter(e -> e.getKey().startsWith(q)).mapToInt(e -> e.getValue()).sum());
		}
	}
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?