38. Count and Say (String; DP)
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)的更多相关文章
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
- 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 ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- 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 ...
- 【BZOJ-1833】count数字计数 数位DP
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2494 Solved: 1101[Submit][ ...
- 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 ...
- 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- 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 ...
随机推荐
- 选择符API
querySelector() querySelector()方法接收一个CSS选择符,返回与该模式匹配的第一个元素,如果没有找到匹配的元素,返回null. //获得body元素 var body = ...
- String intern()方法详解
执行以下代码 String a1=new String("abc"); String a2=new String("abc"); Sys ...
- 设置redis 密码
redis配置密码 1.通过配置文件进行配置 yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy require ...
- apache 服务器在ubuntu上图片无法显示解决
很简单的一段代码实例: <!DOCTYPE html> <html> <body> <h2>Welcome here!</h2> <i ...
- Maven web项目启动出错
问题描述: 在第一次建立maven项目后,启动测试,结果jsp页面报告404错误 问题解决: 在pom.xml中引入servlet-api 和 jsp-api,maven不会自动的添加,因此,要在首次 ...
- 第5章 进程环境(1)_进程结构(task_struct)
1. 进程的概念和进程结构 1.1 进程 (1)程序(program):是一些保存在磁盘上有序指令的集合,是存放在磁盘文件中的可执行文件.但没有任何执行的概念,它是静态的. (2)进程(process ...
- DIY-组装
DIY:-组装 组装,现在基本什么都可以组装,就像计算机,手机,自己进行定制,同样操作系统可以自己组装,软件开发也要组装,现在就是一个DIY的时代. 大家了解DIY,说白了就是自己定制组装一些东西,比 ...
- bootstrap 教程分享
Bootstrap 教程 Bootstrap 简介 Bootstrap 环境安装 Bootstrap CSS 概览 Bootstrap 网格系统 Bootstrap 排版 Bootstrap 代码 B ...
- php 流程控制switch实例
switch允许对一个标量(表达式)的多个可能结果做选择. 语法: switch (expr) { case result1: statement1 break; case result2: stat ...
- RouterOS 5.16软路由安装图解教程
说明:RouterOS是一种路由器操作系统,它可以安装到普通的个人电脑上面,替代硬件路由器 RouterOS版本:RouterOS 5.16 硬件要求: 1.支持多核CPU 2.内存最大支持到2G 3 ...