C # I/O Assistant class

Source: Internet
Author: User

when using C # To solve ACM problems, it is easier to read integers directly from the standard input using a readint32 method. The following is an I/O Assistant class iohelper:

Namespace skyiv {using system; using system. IO; using system. Text; sealed class iohelper: idisposable {static readonly encoding = encoding. ASCII; // or utf8? Static readonly byte [] eols = encoding. getbytes (environment. newline); static readonly byte [] blanks = {9, 10, 13, 32}; // tab, lf, Cr, space static readonly byte EOF = 0; // assume '\ 0' not in input file byte [] Buf = new byte [32]; // For write (int n) byte [] buffer = new byte [64*1024]; int current = 0; int COUNT = 0; binaryreader reader; binarywriter writer; Public iohelper (): This (console. openst Andardinput (), console. openstandardoutput () {} public iohelper (Stream reader, stream writer) {This. reader = new binaryreader (Reader); this. writer = new binarywriter (writer);} byte readbyte () {If (current> = count) {COUNT = reader. read (buffer, current = 0, buffer. length); If (COUNT = 0) return EOF;} return buffer [current ++];} public static byte [] getbytes (string Str) {return encoding. getby TES (STR) ;}public int readint32 () {var n = 0; var OK = false; For (byte B; (B = readbyte ())! = EOF;) {If (array. indexof (blanks, B)> = 0) if (OK) break; else continue; n = N * 10 + (B-'0'); OK = true ;} return N;} public int Readline (byte [] buffer) {var n = 0; while (n <buffer. length) {var B = readbyte (); If (B = eols [0]) {If (eols. length = 2 & readbyte ()! = Eols [1]) throw new invaliddataexception ("invalid EOL"); break;} buffer [n ++] = B;} return N;} public void write (INT N) {If (n = 0) {writer. write (byte) '0'); return;} var I = Buf. length; For (; n> 0; N/= 10) BUF [-- I] = (byte) (n % 10) + '0'); write (BUF, i, Buf. length-I);} public void write (byte [] buffer, int index, int count) {writer. write (buffer, index, count);} public void write (Str Ing Str) {var buffer = encoding. getbytes (STR); writer. write (buffer, 0, buffer. length);} public void writespace () {writer. write (blanks, 3, 1);} public void writeline () {writer. write (eols, 0, eols. length);} public void dispose () {If (reader! = NULL) reader. Close (); If (writer! = NULL) writer. Close ();}}}

The iohelper class also provides the following methods for processing strings:

    • Public int Readline (byte [] buffer)
    • Public void write (byte [] buffer, int index, int count)

Similar to C/C ++, these two methods only process strings as byte arrays and use ASCII codes. Rather than using Unicode as a string in C.

The following is a usage example. For the source of the question, see the essay "I-keyboard.

Namespace skyiv. ben. ACM {using system; // http://www.spoj.pl/problems/IKEYB/ sealed class ikeyb {const int max = 90; static int [,] cost = new int [Max + 1, Max + 1]; static int [,] Price = new int [Max + 1, Max + 1]; static int [,] Index = new int [Max + 1, Max + 1]; static int [] f = new int [Max]; static byte [] keys = new byte [Max]; static byte [] letters = new byte [Max]; static byte [] message1 = iohelper. getbytes ("keypad #"); static byte [] message2 = iohelper. getbytes (":"); static iohelper helper; static void main () {using (helper = new iohelper () {var runner = new ikeyb (); var T = helper. readint32 (); For (VAR n = 1; n <= T; n ++) runner. run (n) ;}} void run (int n) {var K = helper. readint32 (); var L = helper. readint32 (); helper. readline (KEYS); helper. readline (letters); For (VAR I = 0; I <L; I ++) f [I] = helper. readint32 (); initialize (K, L); compute (K, L); helper. write (message1, 0, message1.length); helper. write (n); helper. write (message2, 0, 1); helper. writeline (); output (K, L); helper. writeline ();} void initialize (int K, int L) {for (VAR I = 0; I <= K; I ++) for (VAR j = 1; j <= L; j ++) price [I, j] = int. maxvalue/2; for (VAR I = 1; I <= L; I ++) for (var j = I; j <= L; j ++) cost [I, j] = cost [I, j-1] + (J-I + 1) * f [J-1];} void compute (int K, int L) {for (VAR I = 1; I <= K; I ++) for (var j = I; j <= L; j ++) for (VAR n = 1; n <= J-I + 1; n ++) {var sum = Price [I-1, J-N] + cost [J-n + 1, J]; if (sum <= Price [I, j]) {price [I, j] = sum; index [I, j] = n ;}} void output (int K, int L) {If (k = 0) return; var n = index [k --, l]; output (K, L-N); helper. write (keys, k, 1); helper. write (message2, 0, message2.length); helper. write (letters, L-N, N); helper. writeline ();}}}

If the following input is provided:

 
12 5 * # abcde1024327682147483647987654321

AboveProgramThe following output is generated:

 
Keypad #1: *: ABCD #: E

Note that the above output is actually incorrect. Correct output should be divided into AB and cde groups. However, there is no problem with the program itself, but there is a problem with the input data. Because the frequency of each letter in the original question cannot exceed 100000.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.