TopcoderArchive
Archive
Algorithm problem

DesktopTaskPrepare

Problem statement, definition, constraints, and public examples.

Problem Statement

Your desktop contains several icons. Each time you press an arrow key, the focus may switch from one icon to another. You are given a String[] icons, each element of which represents a single icon on the desktop. Each element should be formatted as "Name NameUp NameDown NameLeft NameRight" (quotes for clarity), where Name is the name of an icon. When that icon has the focus and you press up, down, left, or right, focus will switch to the icon with name NameUp, NameDown, NameLeft, or NameRight, respectively. Note that the focus may not always change, so one or more of those four names might be equal to Name itself. In this problem, you are asked to check that the input satisfies the following constraints:
  • Constraint 0: Each element of icons will be formatted as described in problem statement;
    For each icon, Name, NameUp, NameDown, NameLeft, NameRight will contain between 1 and 5 uppercase letters ('A' - 'Z').
  • Constraint 1: All names of icons will be different.
  • Constraint 2: Each of NameUp, NameDown, NameLeft, NameRight will be equal to the name of some icon.


Return the String "VALID" if icons satisfies all of the above constraints. Otherwise, return "INVALID: Constraint X violated", where X is the number of the lowest violated constraint ('0', '1', or '2'). This means you must first check Constraint 0 on every element of icons. If all the elements satisfy that constraint, check Constraint 1, and so on. All quotes are for clarity.

Definition

Class:
DesktopTaskPrepare
Method:
checkConstraints
Parameters:
String[]
Returns:
String
Method signature:
String checkConstraints(String[] icons)
(be sure your method is public)

Constraints

  • icons will contain between 1 and 50 elements, inclusive.
  • Each element of icons will contain between 1 and 50 characters, inclusive.
  • Each character in icons will have ASCII code between 32 and 126, inclusive.

Examples

  1. {"A A A A A"}
    Returns: "VALID"
  2. {"A A A BQWAS A", "BQWAS BQWAS BQWAS CC A", "CC CC CC CC BQWAS"}
    Returns: "VALID"
  3. {"A   A  A B  A", " B B B C A", "C C C C B"}
    Returns: "INVALID: Constraint 0 violated"
    Extra spaces in element 0, and a leading space in element 1.
  4. {"A A A B A", "B B B C A", "C C DC C B"}
    Returns: "INVALID: Constraint 2 violated"
    Element 2 refers to an icon "DC" that does not exist on the desktop.
  5. {"B A A B A", "B B B C A", "C C C C B"}
    Returns: "INVALID: Constraint 1 violated"
    There are two icons with the name "B".
  6. {"A A a B A", "B B B C A", "C C C A B"}
    Returns: "INVALID: Constraint 0 violated"
    There is a non-uppercase letter in element 0.
  7. {"$$####____!! $$$#$%^&*^%$____!!   $$$###____!!", "  B B C C  __B ", "DAC CDSA AFDS CWWE AASA"}
    Returns: "INVALID: Constraint 0 violated"
Back to all problems