Given 3 characters a, b, c, find the number of strings of length n that can be formed from these 3 characters given that; we can use ‘a’ as many times as we want, ‘b’ maximum once, and ‘c’ maximum twice

Before contest

Codeforces Round #492 (Div. 1) [Thanks, uDebug!]

03:41:27

Register now »

Codeforces Round #492 (Div. 1) [Thanks, uDebug!]

03:41:27

Register now »

*has extra registration

Before contest

Codeforces Round #492 (Div. 2) [Thanks, uDebug!]

03:41:27

Register now »

Codeforces Round #492 (Div. 2) [Thanks, uDebug!]

03:41:27

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3434 |

2 | Petr | 3353 |

3 | OO0OOO00O0OOO0O0…O | 3314 |

4 | fateice | 3306 |

5 | Um_nik | 3286 |

6 | Syloviaely | 3274 |

7 | dotorya | 3145 |

8 | LHiC | 3114 |

9 | Radewoosh | 3098 |

10 | mnbvmar | 3096 |

# | User | Contrib. |
---|---|---|

1 | tourist | 178 |

2 | rng_58 | 165 |

3 | Petr | 155 |

3 | csacademy | 155 |

5 | Swistakk | 149 |

5 | lewin | 149 |

7 | Um_nik | 142 |

8 | Errichto | 140 |

9 | Vovuh | 138 |

9 | matthew99 | 138 |

Given 3 characters a, b, c, find the number of strings of length n that can be formed from these 3 characters given that; we can use ‘a’ as many times as we want, ‘b’ maximum once, and ‘c’ maximum twice

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/24/2018 15:53:33 (d1).

Desktop version, switch to mobile version.

User lists

Name |
---|

Can you describe what is your approach to solve this problem?

I tried to solve it recursivly by generating all possible strings and then discarding those strings which doesn't follow above rule?

Can you tell me more efficient approach?

How about using permutaions?

Number of strings containing only a's.

Number of strings containing 1 b and rest a's.

Number of strings containing 1c and rest a's.

. . .

and add the sum of all the above points to get the final answer.

Suppose IF we need to print the strings then?

then how to print them in minimum complexity?

The best you can do is where

Mis the number of possibilities. To do it efficiently, make some string of lengthn, and recursively fix the character. You'd do this from left to right, and your recursive function should pass down information on whether or not you've fixed the letter`b`

, and how many`c`

's you've fixed so far.thanks a lot sir.Can you write pseudo code?

Not too hard:

thnx sir

I assume you're solving this, since N<=20 you can use:

Spoiler(cnk = number of combinations)

thnx sir

There aren't many possible cases if we care only about 'b' and 'c'. Let's focus only on placing 'b' and 'c', the remaining will be 'a':

1) no 'b' and 'c': 1 option.

2) only 1 'b': n options.

3) only 1 'c': n options.

4) 2 'c': n(n — 1) / 2 options.

5) 1 'b' and 1 'c': n(n — 1) options.

6) 1 'b' and 2 'c': n(n — 1)(n — 2) / 6 * 3 = n(n — 1)(n — 2) / 2 options.

so to sum it up, given n you need to output: 1 + 2n + 3n(n — 1) / 2 + n(n — 1)(n — 2) / 2.

this is O(1) which means it's slightly faster than an exponential complexity by using recursion.

here is the code i wrote in C language for this problem if there are some suggestions , i am open to them ;)

https://www.ideone.com/GWm4T3