1
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?

【Dart】競プロ 入出力まとめ

Posted at

はじめに

この記事では,Dart言語を使って競技プログラミングの入力を効率的に受け取るための基本的なテンプレート&メソッドをまとめています.

実際のコード例

以下が本記事で紹介する基本テンプレートです.最初に標準入力をすべて文字列として読み込み,ポインタを動かしながら必要な型を逐次取り出します.

テンプレコード
import 'dart:io';
import 'dart:convert';

void main() {
  final input = _Input();
  int n = input.getInt();

  print(n);
}

class _Input {
  static _Input? _instance;
  final String data;
  int pos = 0;

  _Input._()
      : data = File('/dev/stdin')
          .readAsStringSync(encoding: utf8);

  factory _Input() {
    _instance ??= _Input._();
    return _instance!;
  }

  bool isSpace(String ch) => ch == ' ' || ch == '\n';

  String getString() {
    while (pos < data.length && isSpace(data[pos])) {
      pos++;
    }
    int start = pos;
    while (pos < data.length && !isSpace(data[pos])) {
      pos++;
    }
    return data.substring(start, pos);
  }

  List<String> getStringAsList() => getString().split('');

  List<String> getStrings(int n) {
    return List<String>.generate(n, (_) => getString());
  }

  List<List<String>> getStrings2d(int n, int m) {
    return List<List<String>>.generate(
        n, (_) => getStringAsList());
  }

  int getInt() => int.parse(getString());

  int getInt0idx() => getInt() - 1;

  List<int> getInts(int n) {
    return List<int>.generate(n, (_) => getInt());
  }

  List<int> getInts0idx(int n) {
    return List<int>.generate(n, (_) => getInt0idx());
  }

  double getDouble() => double.parse(getString());

  List<List<int>> getInt2d(int n, int m) {
    return List<List<int>>.generate(n, (_) => getInts(m));
  }
}

void printBool(bool b) {
  print(b ? 'Yes' : 'No');
}

void printList(List list, {String separator = ' '}) {
  print(list.join(separator));
}

セクションごとの使い方

以下では,よくある入力パターン別に,_Inputヘルパーの呼び出し例と代替のDart標準コードを紹介します.

【1行】1文字

入力
abc
final input = _Input();
String s = input.getString();
print(s);  // 'abc'

【1行】1数字

入力
10
int n = input.getInt();
print(n);  // 10

【1行】n文字

入力
abc def ghi
List<String> tokens = input.getStrings(3);
// ['abc', 'def', 'ghi']

【1行】n数字

入力
1 2 3
List<int> nums = input.getInts(3);
// [1, 2, 3]

【n行】1文字/1数字/n文字/n数字

n行1文字

入力
3
abc
def
ghi
int n = input.getInt();
List<String> s = input.getStrings(n);
print(s); // [abc, def, ghi]

n行1数字

入力
3
123
456
789
int n = input.getInt();
List<int> a = input.getInts(n);
print(a); // [123, 456, 789]

Grid的な

入力
4 6
....##
...###
..####
.#####
final [n, m] = input.getInts(2);
List<List<String>> grid = input.getStrings2d(n, m);
[., ., ., ., #, #]
[., ., ., #, #, #]
[., ., #, #, #, #]
[., #, #, #, #, #]

n行n数字

入力
3 4
1 2 3 4
5 6 7 8
9 10 11 12
final [n, m] = input.getInts(2);
List<List<int>> grid = input.getInt2d(n, m);
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

終わり

1
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
1
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?