The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

思路:序列问题,用动态规划存储上一个状态,从而能够推导出下一个元素

class Solution {
public:
string countAndSay(int n) {
string previous = "";
string current = "";
string tmp;
stringstream ss;
int times;
for(int i = ; i <= n; i++){ //从头开始推导序列中每个元素
for(int j = ; j < previous.length(); j++){ //遍历前一个元素中的每一位
times = ;
while(j+ < previous.length()&&previous[j+]==previous[j]){
times++;
j++;
}
ss.clear();
ss << times;
ss >> tmp;
current.append(tmp);
ss.clear();
ss << previous[j];
ss >> tmp;
current.append(tmp);
}
previous = current;
current = "";
}
return previous;
}
};

38. Count and Say (String; DP)的更多相关文章

  1. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

  2. leetCode练题——38. Count and Say

    1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...

  3. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  4. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

  5. 115. Distinct Subsequences (String; DP)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  7. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

  8. 38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  9. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

随机推荐

  1. Java-Runoob-高级教程-实例-数组:04. Java 实例 – 数组反转

    ylbtech-Java-Runoob-高级教程-实例-数组:04. Java 实例 – 数组反转 1.返回顶部 1. Java 实例 - 数组反转  Java 实例 以下实例中我们使用 Collec ...

  2. 9-16Jenkins-4节点

    1.Jenkins-系统管理-全局安全配置,设置代理端口和协议类型,保存 2.Jenkins-系统管理-节点管理,建立节点 设置节点名称,节点工作目录.标签.用法.启动方式设置如下: 标签用于管理节点 ...

  3. 【转载】Vmware Vconverter从物理机迁移系统到虚拟机P2V

    本文完整记录了如何从物理服务器,保持所有环境配置信息,纹丝不动的迁移到虚拟机上,俗称 P2V .采用的工具是VMware公司的 VMware vcenter vconverter standalone ...

  4. 被折腾的sql编程

  5. 小项目,吃货联盟,java初级小项目,源代码

    1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...

  6. python 字符串与字节之间的相互转化

    1.将字符串转化成字节 b'fffff' bytes('ffff', encoding='utf-8') 'ffff'.encode('utf-8') 2.将字节转化成字符串 str(data, en ...

  7. apache伪静态失败,但是phpinfo显示有rewrite的时候考虑的情况

    大家知道除了加载loadmodule后 还需要修改http.conf 使apache支持.htaccess 允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All ...

  8. 用多个class选择元素

    注意下面两个的区别:  $(".role-developer.role-designer").css("color","red");  $( ...

  9. shell随笔

    一, case的详细用法:   参考文章(http://blog.csdn.net/dreamtdp/article/details/8048720) 语句实例:由用户从键盘输入一个字符,并判断该字符 ...

  10. zabbix修改中文乱码

    参考网站; https://blog.csdn.net/open_data/article/details/47447029 字体下载网站: http://www.font5.com.cn/zitix ...