TopcoderArchiveVisit Topcoder
SRM5 Aug 2002
SRM 107

CellLife

Problem statement, definition, constraints, and public examples.

Problem Statement

We are interested in how much biological diversity can be generated by very simple life and death rules. The environment is a line of cells. Each cell may either be alive or dead.

We will define the diversity in a line of cells to be the number of different sizes of alive groups that are present. An alive group is a contiguous set of alive cells. So a line of cells AADADDDADDDAAAADDADDA has diversity equal to 3 since it contains alive groups of sizes 2, 1, and 4.

Create a class CellLife that contains a method diversity that takes a configuration of cells as input and returns its diversity.

Definition

Class:
CellLife
Method:
diversity
Parameters:
String
Returns:
int
Method signature:
int diversity(String line)
(be sure your method is public)

Constraints

  • line contains between 1 and 50 characters, inclusive.
  • Each character in line is 'A' or 'D'.

Examples

  1. "ADDAADD"
    Returns: 2
    This line has a group of size 1 and a group of size 2
  2. "ADDAAADAAAD"
    Returns: 2
    This line has groups of size 1 and size 3
  3. "DDD"
    Returns: 0
Back to all problems