TopcoderArchiveVisit Topcoder
SRM8 Feb 2005
SRM 230

MultiReplacer

Problem statement, definition, constraints, and public examples.

Problem Statement

Let f be a function that takes a string composed of a's, b's, and c's, and replaces every occurrence of 'a' with arep, 'b' with brep, and 'c' with crep. Your code will compute the length of (quotes for clarity):
	f( f( f( ... f(a) ... ) ) ) : f applied iter times to the string "a"
Since the lengths can grow very large with only a few iterations, you will return the length mod m. For example, if
	arep = abc
	brep = c
	crep = ba
	iter = 3
	m = 7
then you will find the length of
	f(f(f(a))) = f(f(abc)) = f(abccba) = abccbabacabc ,
which is 12. Then you will compute 12 mod 7 = 5, and return 5.

Definition

Class:
MultiReplacer
Method:
length
Parameters:
String, String, String, int, int
Returns:
long
Method signature:
long length(String arep, String brep, String crep, int iter, int m)
(be sure your method is public)

Constraints

  • arep, brep, and crep will contain between 1 and 50 characters inclusive.
  • Each character in arep, brep, and crep will be 'a', 'b', or 'c'.
  • iter will be between 1 and 2000000000 inclusive.
  • m will be between 2 and 2^31-1 inclusive.

Examples

  1. "abc"
    "c"
    "ba"
    3
    7
    Returns: 5
    Same as above.
  2. "b"
    "c"
    "a"
    2000000000
    100
    Returns: 1
  3. "b"
    "c"
    "aa"
    30
    10000
    Returns: 1024
  4. "acabbcabbabac"
    "cababcababcabcabacbacbabcacb"
    "acbacbabcababcabababbabcbcab"
    2000000000
    2000000000
    Returns: 1876480521
Back to all problems