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. ASP.NET Web Pages:全局页面

    ylbtech-.Net-ASP.NET Web Pages:全局页面 1.返回顶部 1. ASP.NET Web Pages - 全局页面 本章介绍全局页面 AppStart 和 PageStart ...

  2. shell脚本判断语句和循环语句

    if判断语句 exit跳出判读语句 不加exit的结果 read -n(不换行) 判断是否输入的是数字 read age[[ $age =~ ^[0-9]+$ ]]if [ $? -ne 0 ]; t ...

  3. MySQL 安装方法

    所有平台的Mysql下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. Linux/UNIX上安装Mysql Linux平台上推荐使用RP ...

  4. spark sql 的性能调优

    Caching Data in Memory 其他调优参数

  5. json化的必要性

    参考文章:http://www.cnblogs.com/SanMaoSpace/p/3139186.html http://www.oschina.net/question/100267_61459

  6. 华为手机如何查看WiFi密码

    2017.8.26 (补充) 博主更新华为手机系统之后,发现下面的教程已经不适用了,新系统在备份WLAN时强制要求设置密码,无法跳过,所以下面的教程仅适用于备份时可以跳过设置密码的系统. 有时候手机用 ...

  7. django中使用Form组件

    内容: 1.Form组件介绍 2.Form组件常用字段 3.Form组件校验功能 4.Form组件内置正则校验 参考:https://www.cnblogs.com/liwenzhou/p/87478 ...

  8. rhel7配置ELK过程

    参考网站:https://www.cnblogs.com/hongdada/p/7887455.html https://my.oschina.net/codingcloud/blog/1615013 ...

  9. Tomcat 多项目管理

    有时我们会在一个服务下存在多个项目的情况,为了统一管理和服务运行安全考虑,有时候我们需要重新部署一个项目但又不能停掉其它项目,就可以用manager进行管理. Tomcat manager和Root访 ...

  10. event bManualResult

    MSDN: bManualReset [in] If this parameter is TRUE, the function creates a manual-reset event object, ...