LOGIN/REGISTER
Please Wait...
It is currently Jul 29, 2010, 10:23 am
Post new topic Reply to topic  [ 17 posts ]  Share: Facebook
Message
Post Posted: Dec 25, 2007, 10:51 am • # 1 


Here is the problem:

We are given a string of 2s. We want to replace all possible strings of "222" with "11" and output the number of equivalent "words" or strings.

Example:

The string of 2s is 2222, then the equivalent strings are:

2222
112
211

I know that the replace method will take 2222 and give 112, but how in Java can I get the program to output 211?

I've heard of the replaceAll method, but how do I implement it so that the program outputs both 112 and 211?
 
 
Post Posted: Dec 25, 2007, 2:24 pm • # 2 


Why are you trying to output those strings? You want to output how many there are, which is a completely different problem which can be solved by DP.

There's really no easy way to use any replace methods to do this, since you may want to say 'replace the 222s at positions 3,7,12, and 21', and leave the others as they are. So in this case, you'd just tell it to replace the 222 at position 1, ie s.substring(0,1)+"11"+s.substring(4). Obviously, thats not really what you were asking, but since it completely depends on which set of 222s you want to replace, theres no other choice.

_________________
Stephen
 
 
Post Posted: Dec 25, 2007, 3:05 pm • # 3 


Is it possible to log the points which you make the substitutions at.

For example on the string 22222

First sweep

22222 -> 1122 Log 1

Starts at 22222
Gets to the 2nd place
then is
2112

etc

I'll come up with an implementation in a sec if you want

_________________
Your work is a mess and your maths is invalid
 
 
Post Posted: Dec 26, 2007, 11:06 am • # 4 


If you start out with like, "222222", should your program output "11222" as an equivalent word, even though there's still a "222" in "11222"?
 
 
Post Posted: Dec 26, 2007, 4:31 pm • # 5 


TripleM wrote:
Why are you trying to output those strings? You want to output how many there are, which is a completely different problem which can be solved by DP.


I actually need the how many there are, but I couldn't think of a way to do it. What is DP?

SimonM wrote:
Is it possible to log the points which you make the substitutions at.


I can't think of one right off the top of my head.

h_s_potter2002 wrote:
If you start out with like, "222222", should your program output "11222" as an equivalent word, even though there's still a "222" in "11222"?


The program should output 11222 and 1111 (by changing the 222 to 11 in 11222) because are equivalent 222222.
 
 
Post Posted: Dec 26, 2007, 4:56 pm • # 6 


DP is dynamic programming. Quite similar to induction.

Here are some steps to get started with (not a full solution, I'll leave the main part to you):

Define f(n) = the number of strings equivalent to the last n digits of your initial string.
So f(0) = 1, since theres one string (the empty string) equivalent to the empty string.

If the string was 111111222, then f(3) = 2, since there are two strings equivalent to 222 (namely, 222 and 11).

Now, assume you have already calculated f(0), f(1), ... f(n-1).

How can you calculate f(n)? (There are two different cases to consider).

_________________
Stephen
 
 
Post Posted: Dec 26, 2007, 8:26 pm • # 7 


I don't think you really need DP if you have to output every possible string.
 
 
Post Posted: Dec 26, 2007, 9:42 pm • # 8 


h_s_potter2002 wrote:
I don't think you really need DP if you have to output every possible string.


You don't, that was the point.

_________________
Stephen
 
 
Post Posted: Dec 27, 2007, 9:37 am • # 9 


TripleM wrote:
DP is dynamic programming. Quite similar to induction.

Here are some steps to get started with (not a full solution, I'll leave the main part to you):

Define f(n) = the number of strings equivalent to the last n digits of your initial string.
So f(0) = 1, since theres one string (the empty string) equivalent to the empty string.

If the string was 111111222, then f(3) = 2, since there are two strings equivalent to 222 (namely, 222 and 11).

Now, assume you have already calculated f(0), f(1), ... f(n-1).

How can you calculate f(n)? (There are two different cases to consider).


OK, so the two cases are

Case 1. The string begins with a 1.
Case 2. The string begins with a 2.

Case 1:

Two subcases:
11_ _ _ _ _....
12_ _ _ _ _.... -> This will give f(n) = f(n-1), because we can't make any substitutions with the 12

Case 2:

Four subcases:
211_ _ _ _ _.... -> This will give f(n) = f(n-1), because we can't do any more substitutions
212_ _ _ _ _.... -> Same as 211 case
221_ _ _ _ _.... -> Same as 211 case
222_ _ _ _ _....

Now I am stuck with the 11_ _ _ _ _.... and 222_ _ _ _ _.... cases. Any ideas?

For example, for the string, 11 221111 which can give 222 221111, but then we have a 2 22 place where we can substitute but was not counted in the f(n-1) equivalent words.
 
 
Post Posted: Dec 27, 2007, 3:44 pm • # 10 


Oh wait, so you're allowed to replace 11 with 222 as well? (Originally it was just 222->11) OK, thats a different problem altogether, let me think about that..

_________________
Stephen
 
 
Post Posted: Dec 27, 2007, 5:56 pm • # 11 


So, just to clarify, are you counting 222222 and 12221 equivalent? (222 222 -> 11 11 -> 1 11 1 -> 1 222 1).

_________________
Stephen
 
 
Post Posted: Dec 28, 2007, 10:11 am • # 12 


TripleM wrote:
So, just to clarify, are you counting 222222 and 12221 equivalent? (222 222 -> 11 11 -> 1 11 1 -> 1 222 1).


Yeah, 222 can be replaced by 11 and vice versa.
 
 
Post Posted: Dec 30, 2007, 1:56 pm • # 13 


Hmm.. what are the constraints on the number of 2s you start with?

Out of interest, where did this question come from? It would be nice to see some original wording just so I can make sure I am solving the right problem :P

_________________
Stephen
 
 
Post Posted: Dec 31, 2007, 5:05 pm • # 14 


If we change all the 1s to 3s, we can think of this as a sort of action on the set of strings composed of 2s and 3s summing to n; then the broadest question is to find the orbits of this action (although actually we're only asked to find the size of the orbit of a given element). Here's a "mathematician's algorithm" (i.e., a process that certainly works but may not be implementable or efficient) for doing this recursively:

For n = 2, 3, 4 it is easy (there is only one element, and it is its own orbit). I'm going to make up a notation and describe these situations by \{(2)\}, \{(3)\} and \{(22)\}, while for n = 7 I'd write \{(223), (232), (322)\} and for n = 8, \{(2222, 332, 233), (323)\}. Suppose we've calculated these orbits up to n - 1. To calculate the orbits for n, start by taking the orbits for n - 2 and prepending a 2 onto each element of every orbit, and taking the orbits for n - 3 and prepending a 3 onto each element of every orbit. For example, for n = 10 I begin with \{(22222, 2332, 2233), (2323)\} \cup \{(3223), (3232), (3322)\}. Now, what I have is a refinement of the set of orbits, so I need to check when two of these can actually be joined together. To do this, consider the orbits that came from n - 3. For any element that begins with 33 (after we've prepended a 3), switch that out for a 222 and join the orbit of what you've started with to the orbit of what you get. In our example with n = 10, we have the element 3322 that now begins with 33; we switch it to a 222 to get 22222 and join the two orbits together to get our final result, \{(22222, 2332, 2233,3322), (2323), (3223), (3232)\}. Rinse and repeat.

Since the number of elements grows exponentially fast, this probably requires exponential time and huge amounts of storage space. Actually, on second thought, you only need to run the "look for a 33, switch to a 222, join the two orbits" step for a single element of each orbit, and the number of orbits most likely grows much more slowly than the number of elements, so the running time may be less miserable (though you still have to store everything).

_________________
Joel
Hi Deeps! <3
 
 
Post Posted: Jan 06, 2008, 1:22 pm • # 15 


Further analysis: let a_n be the number of ways to write a string of 2s and 3s whose sum is n. Then this sequence satisfies the recursion a_n = a_{n - 2} + a_{n - 3}. The number of these strings beginning with a 3 is a_{n - 3}, so satisfies the same recursion. The growth rate of this is c_1 r_1^n for some constant c_1, where r_1 is the root of largest absolute value of the equation x^3 - x^2 - 1 = 0, r_1 \approx 1.46557\ldots.

Now, I suggested at the end of my previous post that one need only consider a single element of each orbit and that this might make everything more efficient. Unfortunately, the number of orbits also grows exponentially: let b_n be the number of strings of 2s and 3s summing to n with no two consecutive 3s and no three consecutive 2s such that the string begins with a 2 but not a 22; let c_n be the number of such strings beginning with a 3; and let d_n be the number of such strings beginning with a 22. Then
\begin{align*} a_n & = b_{n - 2} \\
b_n & = a_{n - 3} + c_{n - 3} \\
c_n & = a_{n - 2} \end{align*}and so (plugging the first and third of these equations into the second) b_n = b_{n - 5} + b_{n - 7}.* Thus b_n \sim c_2 r_2^n for some constant c_2, where r_2 is the root of largest absolute value of the equation x^7 - x^2 - 1 = 0, r_2 \approx 1.12373\ldots. Note that every such string is its own orbit, so we have exponentially many orbits (albeit with a smaller base for the exponential).

* Actually, b_n satisfies the simpler recursion b_n + b_{n - 1} = b_{n - 3} + b_{n - 4} + b_{n - 5}, and x^7 - x^2 - 1 = (x^2 - x + 1)(x^5 + x^4 - x^2 - x - 1).


For the curious, here's some raw data (and the ugly Mathematica code used to generate it -- suggestions for improvements welcome):
Hidden Text

_________________
Joel
Hi Deeps! <3
 
 
Post Posted: Jan 23, 2008, 8:37 am • # 16 


I've placed two relevant sequences in the OEIS, numbers A133925 and A133926.

_________________
Joel
Hi Deeps! <3
 
 
Post Posted: Feb 04, 2008, 12:34 pm • # 17 


actually I think using regexes is the most elegant way to solve the problem.
it is the typical spot where you would use regexes.
 
 
Display posts from previous:  Sort by  

All times are UTC - 8 hours [ DST ]

Share: Facebook

Moderators: nr1337, nebula42

Post new topic Reply to topic  [ 17 posts ] 

Login

Username:   Password:   Log me on automatically each visit  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum