LINTCODE——Number of Subsequences of Form a^i b^j c^k
来源:互联网 发布:灰度共生矩阵14个特征 编辑:程序博客网 时间:2024/06/07 06:56
思路:这道题目还是很有意思的,统计情况,如果要遍历情况的话可以用回溯法,不过超时是必然的,我们可以用数学的思路来分析这个问题:
分析:abc必须按照顺序排列才能满足我们的情况,所以,我们从后往前考虑,不做任何的推理,光凭逻辑我们应该可以知道,当str[i] = ‘c’的时候,此时字符串的排列情况,应该与i之前的c出现的次数,以及b有关;同样的我们可以知道在i之前的j当str[j] = ‘b’的时候,此时字符串的排列情况,应该与j之前的b出现的次数,以及a有关;
而单单只分析a出现的次数的话,即假设str形式如(aaa…aaabc)这种情况,那么此时最后统计的结果应该是2^a_count(a出现的次数)-1,等价于从前往后遍历字符串a_count = a_count*2+1;
然后我们就可以知道,b_count = a_count + b_count*2;
然后c_count = b_count+c_count*2
而最后的结果就是c_count;
ps:说了这么一大堆,我自己也被绕糊涂了,文笔有限,还请见谅
class Solution {public: /* * @param : the input string * @return: the number of subsequences */ int countSubsequences(string &source) { // write your code here int a_count = 0, b_count = 0, c_count = 0; for(auto x : source) { if(x == 'a') a_count = 1 + 2*a_count; else if (x == 'b') b_count = a_count + 2*b_count; else if(x == 'c') c_count = b_count + 2*c_count; } return c_count; }};
阅读全文
0 0
- LINTCODE——Number of Subsequences of Form a^i b^j c^k
- lintcode 734. Number of Subsequences of Form a^i b^j c^k
- 形式为a^i b^j c^k的子序列数量-LintCode
- LintCode 练习-734. 形式为a^i b^j c^k的子序列数量
- 三个已排序的数组a,b,c,求i,j,k使a[i]=b[j]=c[k]
- [Lintcode]Letter Combinations of a Phone Number
- 关于Lintcode734形式为a^i b^j c^k的子序列数量
- [Lintcode]Number of Islands
- cuda计算C[i][j]=A[i][j]+B[i][j]
- 2012年成都Onsite (A,B,I,J,K)
- hrbust1996 是否能找到三个数满足等式A[i]+B[j]+C[k]=X【hash模板】
- [Lintcode]Count of Smaller Number
- LintCode 433: Number of Islands
- lintcode: Count of Smaller Number
- B - Number of Ways
- the minimum value of |A - a| + |B - b| + |C - c|
- Print the numbers of form 2^i.5^j in increasing order -- Google
- CareerCup Find 3 indexes i,j,k such that, i<j<k and a[i] < a[j] < a[k]
- HTML5基础知识2
- idea创建spring boot项目
- 深入理解javascript原型和闭包(13)-【作用域】和【上下文环境】
- WEB项目部署到服务器,验证码无法刷新
- Rosonblatt线性感知器
- LINTCODE——Number of Subsequences of Form a^i b^j c^k
- ucgui按键工作的流程
- RK3399(2)
- Android Studio 导入 Sweet-alert-dialog
- SpringCloud 教程 | 第二篇: 服务消费者(rest+ribbon)
- iOS蓝牙原生封装,助力智能硬件开发
- [LC][array] Add to List 561. Array Partition I
- VS版Eigen库求解大型稀疏线性方程组
- android 性能优化笔记(3)