#C11903. Multi-Function Challenge

    ID: 41271 Type: Default 1000ms 256MiB

Multi-Function Challenge

Multi-Function Challenge

You are given a program that must support several functions. Your program will read multiple queries from standard input. Each query specifies a task to perform. The functions to implement are:

  • TWO_SUM: Given a list of integers and a target value, find and output the indices of the two numbers that add up to the target. It is guaranteed that exactly one solution exists.
  • PALINDROME: Given a string, check if it is a palindrome. Consider only alphanumeric characters and ignore cases.
  • INORDER: Given a binary tree represented in level-order (with "null" representing missing nodes), output the inorder traversal of its nodes.
  • SPIRAL: Given an integer n, generate an n x n spiral matrix filled with the integers from 1 to n^2 in spiral order.

The input begins with an integer Q representing the number of queries. Each query starts with a command indicating which function to execute. The commands and their input formats are described below.

inputFormat

The first line of input contains an integer Q (Q ≥ 1) – the number of queries. Each query is given in the following format depending on the type:

  • TWO_SUM:
    • A line with the string TWO_SUM.
    • A line with an integer n — the number of elements in the array.
    • A line with n space-separated integers.
    • A line with an integer target.
  • PALINDROME:
    • A line with the string PALINDROME.
    • A line with the string to check.
  • INORDER:
    • A line with the string INORDER.
    • A line with an integer m – the number of tokens in the level-order representation of the tree.
    • A line with m space-separated tokens. Each token is either an integer or the string null (without quotes) representing a missing node.
  • SPIRAL:
    • A line with the string SPIRAL.
    • A line with a positive integer n, the dimension of the matrix.

outputFormat

For each query, output the result on standard output as described below:

  • TWO_SUM: Output two space-separated integer indices.
  • PALINDROME: Output either True or False.
  • INORDER: Output the inorder traversal of the tree as space-separated integers on one line. If the tree is empty, output an empty line.
  • SPIRAL: Output the spiral matrix: print n lines, each containing n space-separated integers.

The outputs for different queries should appear in the order of the queries.

## sample
4
TWO_SUM
4
2 7 11 15
9
PALINDROME
A man, a plan, a canal: Panama
INORDER
4
1 null 2 3
SPIRAL
3
0 1

True 1 3 2 1 2 3 8 9 4 7 6 5

</p>