CareerCup Output the amount of all possible strings of length N that don't of have consecutive a,b,c

suppose a string is consists of a, b, and c 
Now given a integer N, output the amount of all possible strings of length N that don't of have consecutive a,b,c. 

e.g. given N=5, string bacca is invalid since the first 3 letters have consecutive a,b,c. and bbbbb is valid.

-----------------------------------------------------------


f1(n) is the number of strings with the last two char same; 
f2(n) is the number of strings with the last two char different; 
f1(n)=f1(n-1)+f2(n-1); 
f2(n)=2*f1(n-1)+f2(n-1); 
f1(2)=3; 
f2(2)=6;


f1(n) is the number of strings with the last two char same; 
f2(n) is the number of strings with the last two char different; 
f1(n)=f1(n-1)+f2(n-1);------------------1 
f2(n)=2*f1(n-1)+f2(n-1);----------------2 
f1(2)=3; 
f2(2)=6; 


Now, 
f(n)=f1(n)+f2(n) 
=>f(n-1)=f1(n-1)+f2(n-1)=f1(n)=========from 1------------3 

=>f(n)=f(n-1)+f2(n) 
=>f(n)=f(n-1)+2*f1(n-1)+f2(n-1)=======from 2 
=>f(n)=f(n-1)+(f1(n-1)+f2(n-1))+f1(n-1) 
=>f(n)=f(n-1)+f(n-1)+f1(n-1) 
=>f(n)=f(n-1)+f(n-1)+f(n-2) 
=>f(n=)2*f(n-1)+f(n-2) 

Also, 
f(2)=f1(2)+f2(2)=3+6=9 
f(1)=f1(2)=3 

This final solution is f(n)=2*f(n-1)+f(n-2), f(1)=3, f(2)=9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值