Task
In stations and airports you often see this type of screen:
Have you ever asked yourself how it might be possible to simulate this display on a good old terminal? We have: with ASCII art!
Your mission is to write a program that can display a line of text in ASCII art in a style you are given as input.
Input
Line 1: the width
L of a letter represented in ASCII art. All letters are the same width.
Line 2: the height
H of a letter represented in ASCII art. All letters are the same height.
Line 3: the line of text
T, composed of N ASCII characters.
Following
H lines: the string of characters ABCDEFGHIJKLMNOPQRSTUVWXYZ? Represented in ASCII art.
Output
The text
T in ASCII art.
The characters a to z are shown in ASCII art by their equivalent in upper case.
The characters that are not in the intervals [a-z] or [A-Z] will be shown as a question mark in ASCII art.
Tests
Input | Output |
---|---|
1 1 Encoding using ASCII? Hah! ?ZYXWVUTSRQPONMLKJIHGFEDCBA |
WNYMXSNUAGISNUA?IYSSAAT?TA |
Codes of the program
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 |
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { short Lwidth, Lheight; cin >> Lwidth >> Lheight; cin.ignore(); string Text; char **alphabet = new char*[256]; //initializing alphabet for (char ltr = 'a'; ltr < 'z'+1; ++ltr) { alphabet[ltr] = new char[Lwidth]; } for (char ltr = 'A'; ltr < 'Z'+1; ++ltr) { alphabet[ltr] = new char[Lwidth]; } alphabet['?'] = new char[Lwidth]; getline(cin, Text); for (short h = 0; h < Lheight; ++h) { char ltr; for (ltr = 'a'; ltr < 'z'+1; ++ltr) //reading letters { for (short w = 0; w < Lwidth; ++w) { scanf("%c", &alphabet[ltr][w]); alphabet[ltr-('a'-'A')][w] = alphabet[ltr][w]; } } for (short w = 0; w < Lwidth; ++w) //reading ? { scanf("%c", &alphabet['?'][w]); } scanf("%c", <r); //reading endl if (ltr != '\n') cerr << "ERROR: INVALID TESTS PROVIDED\n"; //Putting result out. for (short i = 0; i < Text.size(); ++i) { if //it is a letter ( (('a'-1 < Text[i]) && (Text[i] < 'z'+1)) || (('A'-1 < Text[i]) && (Text[i] < 'Z'+1)) ) //then print it cout << alphabet[Text[i]]; else //it is definitely something weird cout << alphabet['?']; } cout << '\n'; } } |
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 |
import java.util.*; import java.io.*; class Solution { public static void main(String args[]) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); short Lwidth = in.nextShort(), Lheight = in.nextShort(); Map alphabet = new HashMap(); if (in.hasNextLine()) { in.nextLine(); } String Text = in.nextLine(); for (short h = 0; h < Lheight; ++h) { String alphabet_line = in.nextLine(); short i = 0; for(char sym = 'a', Sym = 'A'; sym <= 'z'; ++sym, ++Sym, ++i) { String letter = alphabet_line.substring(i*Lwidth, (i+1)*Lwidth); alphabet.put(sym, letter); alphabet.put(Sym, letter); } alphabet.put('?', alphabet_line.substring(i*Lwidth, (i+1)*Lwidth)); for(i = 0; i < Text.length(); ++i) { if //it is a letter ( (('a' <= Text.charAt(i)) && (Text.charAt(i) <= 'z')) || (('A' <= Text.charAt(i)) && (Text.charAt(i) <= 'Z')) ) out.print(alphabet.get(Text.charAt(i))); else //it is definitely something weird out.print(alphabet.get('?')); } out.print('\n'); } out.flush(); } } |
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 |
<?php fscanf(STDIN, "%d", $Lwidth); fscanf(STDIN, "%d", $Lheight); $alphabet = array(); $Text = stream_get_line(STDIN, 1024, "\n"); for ($h = 0; $h < $Lheight; ++$h) { $ltr = 'a'; $ltr2 = 'A'; while(true) //reading letters { $alphabet[$ltr] = stream_get_line(STDIN, $Lwidth); $alphabet[$ltr2] = $alphabet[$ltr]; if ($ltr == 'z') break; ++$ltr; ++$ltr2; } //reading '?' $alphabet['?'] = stream_get_line(STDIN, $Lwidth); //reading endl fscanf(STDIN, "%c", $useless); for ($i = 0; $i < strlen($Text); ++$i) { if //it is a letter ( (('a' <= $Text[$i]) and ($Text[$i] <= 'z')) or (('A' <= $Text[$i]) and ($Text[$i] <= 'Z')) ) //then print it echo $alphabet[$Text[$i]]; else //it is definitely something weird echo $alphabet['?']; } echo "\n"; } ?> |
Solution of the task
After saving the string, we can convert the task to «semi-stream processing». After we read and save the first line of ASCII characters into alphabet array, we start to print the tops of the ASCII letters. Then the same procedure is repeated on the following lines, and new parts of ASCII letters are read over the old ones into alphabet.
Links
- Statement of the task;
- Solution (C++) on ideone.com;
- Solution (Java) on ideone.com;
- Solution (PHP) on ideone.com;
- Solution (C++) on codingame.com;
- Solution (Java) on codingame.com;
- Solution (PHP) on codingame.com.