paizaラーニングレベルアップ問題集の「辞書」の問題をやってみました。
問題
辞書の基本
辞書のデータ更新
辞書データの順序
辞書
C
辞書の基本
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char S[n][11];
int A[n];
for (int i = 0; i < n; i++) {
scanf("%s %d", S[i], &A[i]);
}
char s[11];
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (!strcmp(S[i], s)) {
printf("%d\n", A[i]);
}
}
return 0;
}
辞書のデータ更新
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char S[n][11];
int A[n];
for (int i = 0; i < n; i++) {
scanf("%s", S[i]);
A[i] = 0;
}
int m;
scanf("%d", &m);
while (m--) {
char p[11];
int a;
scanf("%s %d", p, &a);
for (int i = 0; i < n; i++) {
if (!strcmp(S[i], p)) {
A[i] += a;
}
}
}
char s[11];
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (!strcmp(S[i], s)) {
printf("%d\n", A[i]);
}
}
return 0;
}
辞書データの順序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* a, const void* b) {
return strcmp((const char*)a, (const char*)b);
}
int main() {
int n;
scanf("%d", &n);
char S[n][11];
for (int i = 0; i < n; i++) {
scanf("%s", S[i]);
}
qsort(S, n, sizeof(S[0]), cmp);
int A[n];
memset(A, 0, sizeof(A));
int m;
scanf("%d", &m);
while (m--) {
char p[11];
int a;
scanf("%s %d", p, &a);
for (int i = 0; i < n; i++) {
if (!strcmp(S[i], p)) {
A[i] += a;
}
}
}
for (int i = 0; i < n; i++) {
printf("%d\n", A[i]);
}
return 0;
}
辞書
#include <stdio.h>
int main() {
int P, Q, R;
scanf("%d %d %d", &P, &Q, &R);
int F[P];
for (int p = 0; p < P; p++) {
int i, j;
scanf("%d %d", &i, &j);
F[i-1] = j-1;
}
int G[Q];
for (int q = 0; q < Q; q++) {
int j, k;
scanf("%d %d", &j, &k);
G[j-1] = k-1;
}
for (int p = 0; p < P; p++) {
printf("%d %d\n", p+1, G[F[p]]+1);
}
return 0;
}
C++
辞書の基本
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> dict;
while (n--) {
string s;
int a;
cin >> s >> a;
dict[s] = a;
}
string S;
cin >> S;
cout << dict[S] << endl;
return 0;
}
辞書のデータ更新
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> dict;
while (n--) {
string s;
cin >> s;
dict[s] = 0;
}
int m;
cin >> m;
while (m--) {
string p;
int a;
cin >> p >> a;
dict[p] += a;
}
string S;
cin >> S;
cout << dict[S] << endl;
return 0;
}
辞書データの順序
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> dict;
while (n--) {
string s;
cin >> s;
dict[s] = 0;
}
int m;
cin >> m;
while (m--) {
string p;
int a;
cin >> p >> a;
dict[p] += a;
}
for (const auto& pair : dict) {
cout << pair.second << endl;
}
return 0;
}
辞書
#include <iostream>
#include <map>
using namespace std;
int main() {
int P, Q, R;
cin >> P >> Q >> R;
map<int, int> F;
for (int p = 0; p < P; p++) {
int i, j;
cin >> i >> j;
F[i] = j;
}
map<int, int> G;
for (int q = 0; q < Q; q++) {
int j, k;
cin >> j >> k;
G[j] = k;
}
for (const auto& pair : F) {
cout << pair.first << " " << G[pair.second] << endl;
}
return 0;
}
C#
辞書の基本
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
Dictionary<string, int> dict = new Dictionary<string, int>();
for (int i = 0; i < n; i++) {
string[] sa = Console.ReadLine().Split();
dict[sa[0]] = int.Parse(sa[1]);
}
string s = Console.ReadLine();
Console.WriteLine(dict[s]);
}
}
辞書のデータ更新
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
Dictionary<string, int> dict = new Dictionary<string, int>();
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++) {
dict.Add(Console.ReadLine(), 0);
}
int m = int.Parse(Console.ReadLine());
for (int i = 0; i < m; i++) {
string[] pa = Console.ReadLine().Split();
dict[pa[0]] += int.Parse(pa[1]);
}
Console.WriteLine(dict[Console.ReadLine()]);
}
}
辞書データの順序
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++) {
dict.Add(Console.ReadLine(), 0);
}
int m = int.Parse(Console.ReadLine());
for (int i = 0; i < m; i++) {
string[] pa = Console.ReadLine().Split();
dict[pa[0]] += int.Parse(pa[1]);
}
foreach (string key in dict.Keys) {
Console.WriteLine(dict[key]);
}
}
}
辞書
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
int[] pqr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int P = pqr[0], Q = pqr[1];
SortedDictionary<int, int> F = new SortedDictionary<int, int>();
for (int p = 0; p < P; p++) {
int[] ij = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
F[ij[0]] = ij[1];
}
Dictionary<int, int> G = new Dictionary<int, int>();
for (int q = 0; q < Q; q++) {
int[] jk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
G[jk[0]] = jk[1];
}
foreach (int i in F.Keys) Console.WriteLine("" + i + ' ' + G[F[i]]);
}
}
Go
辞書の基本
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
dict := make(map[string]int)
for i := 0; i < n; i++ {
var s string
var a int
fmt.Scan(&s, &a)
dict[s] = a
}
var S string
fmt.Scan(&S)
fmt.Println(dict[S])
}
辞書のデータ更新
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
dict := make(map[string]int)
for i := 0; i < n; i++ {
var key string
fmt.Scan(&key)
dict[key] = 0
}
var m int
fmt.Scan(&m)
for i := 0; i < m; i++ {
var p string
var a int
fmt.Scan(&p, &a)
dict[p] += a
}
var s string
fmt.Scan(&s)
fmt.Println(dict[s])
}
辞書データの順序
package main
import (
"fmt"
"sort"
)
func main() {
var n int
fmt.Scan(&n)
dict := make(map[string]int)
keys := make([]string, n)
for i := 0; i < n; i++ {
var key string
fmt.Scan(&key)
dict[key] = 0
keys[i] = key
}
var m int
fmt.Scan(&m)
for i := 0; i < m; i++ {
var p string
var a int
fmt.Scan(&p, &a)
dict[p] += a
}
sort.Strings(keys)
for _, key := range keys {
fmt.Println(dict[key])
}
}
辞書
package main
import (
"fmt"
"sort"
)
func main() {
var P, Q, R int
fmt.Scan(&P, &Q, &R)
F := make(map[int]int)
K := make([]int, P);
for p := 0; p < P; p++ {
var i, j int
fmt.Scan(&i, &j)
F[i] = j
K[p] = i
}
G := make(map[int]int)
for q := 0; q < Q; q++ {
var j, k int
fmt.Scan(&j, &k)
G[j] = k
}
sort.Ints(K)
for _, i := range(K) {
fmt.Println(i, G[F[i]])
}
}
Java
辞書の基本
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(sc.next(), sc.nextInt());
}
String s = sc.next();
sc.close();
System.out.println(map.get(s));
}
}
辞書のデータ更新
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Integer> map = new HashMap<>();
for (int i = sc.nextInt(); i > 0; i--) {
map.put(sc.next(), 0);
}
for (int i = sc.nextInt(); i > 0; i--) {
String p = sc.next();
int a = sc.nextInt();
map.put(p, map.get(p) + a);
}
String s = sc.next();
System.out.println(map.get(s));
}
}
辞書データの順序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Integer> map = new TreeMap<>();
for (int i = sc.nextInt(); i > 0; i--) {
map.put(sc.next(), 0);
}
for (int i = sc.nextInt(); i > 0; i--) {
String p = sc.next();
int a = sc.nextInt();
map.put(p, map.get(p) + a);
}
for (String key : map.keySet()) {
System.out.println(map.get(key));
}
}
}
辞書
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int P = sc.nextInt();
int Q = sc.nextInt();
int R = sc.nextInt();
Map<Integer, Integer> F = new TreeMap<>();
for (int p = 0; p < P; p++) {
F.put(sc.nextInt(), sc.nextInt());
}
Map<Integer, Integer> G = new HashMap<>();
for (int q = 0; q < Q; q++) {
G.put(sc.nextInt(), sc.nextInt());
}
sc.close();
for (int i : F.keySet()) System.out.println("" + i + ' ' + G.get(F.get(i)));
}
}
JavaScript
辞書の基本
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
let n = Number(lines.shift());
const D = {};
while (n--) {
const [s, a] = lines.shift().split(" ");
D[s] = Number(a);
}
const S = lines.shift();
console.log(D[S]);
辞書のデータ更新
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
let n = Number(lines.shift());
const D = {};
while (n--) {
D[lines.shift()] = 0;
}
let m = Number(lines.shift());
while (m--) {
const [p, a] = lines.shift().split(" ");
D[p] += Number(a);
}
console.log(D[lines.shift()]);
辞書データの順序
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
let n = Number(lines.shift());
const D = {};
while (n--) {
D[lines.shift()] = 0;
}
let m = Number(lines.shift());
while (m--) {
const [p, a] = lines.shift().split(" ");
D[p] += Number(a);
}
for (var s of Object.keys(D).sort()) console.log(D[s]);
辞書
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
const [P, Q, R] = lines.shift();
const F = {};
for (var p = 0; p < P; p++) {
const [i, j] = lines.shift();
F[i] = j;
}
const G = {};
for (var q = 0; q < Q; q++) {
const [j, k] = lines.shift();
G[j] = k;
}
for (var i of Object.keys(F).sort()) console.log(i, G[F[i]]);
Kotlin
辞書の基本
fun main() {
val map = mutableMapOf<String, Int>()
repeat(readln().toInt()) {
val (s, a) = readLine()!!.split(" ")
map[s] = a.toInt()
}
println(map[readln()])
}
辞書のデータ更新
fun main() {
val map = mutableMapOf<String, Int>()
repeat(readln().toInt()) {
map[readln()] = 0
}
repeat(readln().toInt()) {
val (p, a) = readln().split(' ')
map[p] = map.getOrDefault(p, 0) + a.toInt()
}
println(map[readln()])
}
辞書データの順序
fun main() {
val map = sortedMapOf<String, Int>()
repeat(readln().toInt()) {
map[readln()] = 0
}
repeat(readln().toInt()) {
val (p, a) = readln().split(' ')
map[p] = map.getOrDefault(p, 0) + a.toInt()
}
for ((_, v) in map) {
println(v)
}
}
辞書
fun main() {
val (p, q, r) = readln().split(' ').map { it.toInt() }
val F = sortedMapOf<Int, Int>()
repeat (p) {
val (i, j) = readln().split(' ').map { it.toInt() }
F[i] = j
}
val G = mutableMapOf<Int, Int>()
repeat(q) {
val (j, k) = readln().split(' ').map { it.toInt() }
G[j] = k
}
for ((u, v) in F) {
println("" + u + ' ' + G[v])
}
}
PHP
辞書の基本
<?php
$n = intval(fgets(STDIN));
$dict = [];
for ($i = 0; $i < $n; $i++) {
[$s, $a] = explode(' ', fgets(STDIN));
$dict[$s] = intval($a);
}
echo $dict[trim(fgets(STDIN))], PHP_EOL;
?>
辞書のデータ更新
<?php
$n = intval(fgets(STDIN));
$dict = [];
for ($i = 0; $i < $n; $i++) {
$dict[trim(fgets(STDIN))] = 0;
}
$m = intval(fgets(STDIN));
for ($i = 0; $i < $m; $i++) {
[$p, $a] = explode(' ', fgets(STDIN));
$dict[$p] += intval($a);
}
echo $dict[trim(fgets(STDIN))], PHP_EOL;
?>
辞書データの順序
<?php
$n = intval(fgets(STDIN));
$dict = [];
for ($i = 0; $i < $n; $i++) {
$dict[trim(fgets(STDIN))] = 0;
}
$m = intval(fgets(STDIN));
for ($i = 0; $i < $m; $i++) {
[$p, $a] = explode(' ', fgets(STDIN));
$dict[$p] += intval($a);
}
ksort($dict);
foreach ($dict as $value) {
echo $value, PHP_EOL;
}
?>
辞書
<?php
[$P, $Q, $R] = array_map("intval", explode(' ', fgets(STDIN)));
$F = [];
for ($p = 0; $p < $P; $p++) {
[$i, $j] = array_map("intval", explode(' ', fgets(STDIN)));
$F[$i] = $j;
}
ksort($F);
$G = [];
for ($q = 0; $q < $Q; $q++) {
[$j, $k] = array_map("intval", explode(' ', fgets(STDIN)));
$G[$j] = $k;
}
foreach (array_keys($F) as $i) echo "$i {$G[$F[$i]]}", PHP_EOL;
?>
Perl
辞書の基本
my %hash;
for (1..int(<STDIN>)) {
my ($s, $a) = split ' ', <STDIN>;
$hash{$s} = int($a);
}
chomp (my $S = <STDIN>);
print "$hash{$S}$/";
辞書のデータ更新
my %hash;
for (1..int(<STDIN>)) {
chomp(my $s = <STDIN>);
$hash{$s} = 0;
}
for (1..int(<STDIN>)) {
my ($p, $a) = split ' ', <STDIN>;
$hash{$p} += int($a);
}
chomp (my $S = <STDIN>);
print "$hash{$S}$/";
辞書データの順序
my %hash;
for (1..int(<STDIN>)) {
chomp(my $s = <STDIN>);
$hash{$s} = 0;
}
for (1..int(<STDIN>)) {
my ($p, $a) = split ' ', <STDIN>;
$hash{$p} += int($a);
}
for my $k (sort keys %hash) {
print "$hash{$k}$/";
}
辞書
my ($p, $q, $r) = map { int($_) } split ' ', <STDIN>;
my %F;
for (1..$p) {
my ($i, $j) = map { int($_) } split ' ', <STDIN>;
$F{$i} = $j;
}
my %G;
for (1..$q) {
my ($j, $k) = map { int($_) } split ' ', <STDIN>;
$G{$j} = $k;
}
for my $i (sort keys %F) {
print "$i $G{$F{$i}}$/";
}
Python3
辞書の基本
print({s: int(a) for s, a in [input().split() for _ in range(int(input()))]}[input()])
辞書のデータ更新
D = {input(): 0 for _ in range(int(input()))}
for _ in range(int(input())):
p, a = input().split()
D[p] += int(a)
print(D[input()])
辞書データの順序
D = {input(): 0 for _ in range(int(input()))}
for _ in range(int(input())):
p, a = input().split()
D[p] += int(a)
for s in sorted(D):
print(D[s])
辞書
p, q, r = map(int, input().split())
F = {i: j for i, j in [map(int, input().split()) for _ in range(p)]}
G = {j: k for j, k in [map(int, input().split()) for _ in range(q)]}
for i in sorted(F):
print(i, G[F[i]])
Ruby
辞書の基本
hash = {}
gets.to_i.times do
s, a = gets.split
hash[s] = a.to_i
end
p hash[gets.chomp]
辞書のデータ更新
hash = {}
gets.to_i.times do
hash[gets.chomp] = 0
end
gets.to_i.times do
p, a = gets.split
hash[p] += a.to_i
end
p hash[gets.chomp]
辞書データの順序
hash = {}
gets.to_i.times do
hash[gets.chomp] = 0
end
gets.to_i.times do
p, a = gets.split
hash[p] += a.to_i
end
hash.keys.sort.each do |k|
p hash[k]
end
辞書
P, Q, R = gets.split.map(&:to_i)
f = {}
P.times do
i, j = gets.split.map(&:to_i)
f[i] = j
end
g = {}
Q.times do
j, k = gets.split.map(&:to_i)
g[j] = k
end
f.keys.sort.each do |i|
print i, ' ', g[f[i]], "\n"
end
Scala
辞書の基本
import scala.io.StdIn._
object Main extends App {
val map = (0 until readInt()).map { _ =>
val Array(k, v) = readLine().split(" ")
k -> v.toInt
}.toMap
println(map(readLine()))
}
辞書のデータ更新
import scala.io.StdIn._
import scala.collection.mutable
object Main extends App {
val map = mutable.Map[String, Int]()
for (_ <- 0 until readInt()) {
map(readLine()) = 0
}
for (_ <- 0 until readInt()) {
val Array(p, a) = readLine().split(" ")
map(p) += a.toInt
}
println(map(readLine()))
}
辞書データの順序
import scala.io.StdIn._
import scala.collection.mutable
object Main extends App {
val map = mutable.Map[String, Int]()
for (_ <- 0 until readInt()) {
map(readLine()) = 0
}
for (_ <- 0 until readInt()) {
val Array(p, a) = readLine().split(" ")
map(p) += a.toInt
}
for (k <- map.keys.toList.sorted) {
println(map(k))
}
}
辞書
import scala.io.StdIn._
import scala.collection.mutable
object Main extends App{
val Array(p, q, r) = readLine().split(' ').map { _.toInt }
val F = mutable.Map[Int, Int]()
for (_ <- 0 until p) {
val Array(i, j) = readLine().split(' ').map { _.toInt }
F(i) = j
}
val G = mutable.Map[Int, Int]()
for (_ <- 0 until q) {
val Array(j, k) = readLine().split(' ').map { _.toInt }
G(j) = k
}
for (i <- F.keys.toList.sorted) {
println("" + i + ' ' + G(F(i)))
}
}
Swift
辞書の基本
let n = Int(readLine()!)!
var dict = [String: Int]()
for _ in 0..<n {
let sa = readLine()!.split(separator: " ")
let (s, a) = (String(sa[0]), Int(sa[1])!)
dict[s] = a
}
let key = readLine()!
if let value = dict[key] {
print(value)
}
辞書のデータ更新
var dict = [String: Int]()
for _ in 0..<Int(readLine()!)! {
dict[readLine()!] = 0
}
for _ in 0..<Int(readLine()!)! {
let pa = readLine()!.split(separator: " ")
let (p, a) = (String(pa[0]), Int(pa[1])!)
dict[p, default: 0] += a
}
let key = readLine()!
if let value = dict[key] {
print(value)
}
辞書データの順序
var dict = [String: Int]()
for _ in 0..<Int(readLine()!)! {
dict[readLine()!] = 0
}
for _ in 0..<Int(readLine()!)! {
let pa = readLine()!.split(separator: " ")
let (p, a) = (String(pa[0]), Int(pa[1])!)
dict[p, default: 0] += a
}
for s in dict.keys.sorted() {
print(dict[s]!)
}
辞書
let pqr = readLine()!.split(separator: " ").compactMap { Int($0) }
let (p, q) = (pqr[0], pqr[1])
var f = [Int: Int]()
for _ in 0..<p {
let ij = readLine()!.split(separator: " ").compactMap { Int($0) }
let (i, j) = (ij[0], ij[1])
f[i] = j
}
var g = [Int: Int]()
for _ in 0..<q {
let jk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (j, k) = (jk[0], jk[1])
g[j] = k
}
for i in f.keys.sorted() {
print(i, g[f[i]!]!)
}