PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分)
题干
Look-and-say sequence is a sequence of integers as the following:
D, D1, D111, D113, D11231, D112213111, ...
where D
is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D
in the 1st number, and hence it is D1
; the 2nd number consists of one D
(corresponding to D1
) and one 1 (corresponding to 11), therefore the 3rd number is D111
; or since the 4th number is D113
, it consists of one D
, two 1's, and one 3, so the next number must be D11231
. This definition works for D
= 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D
.
Input Specification:
Each input file contains one test case, which gives D
(in [0, 9]) and a positive integer N (≤ 40), separated by a space.
Output Specification:
Print in a line the Nth number in a look-and-say sequence of D
.
Sample Input:
1 8
Sample Output:
1123123111
思路
一道经典的滑动窗口题目。而我遇到了一个坑点。
ans = ans + d[q] + to_string(p - q);
ans += d[q] + to_string(p - q);
咋一看这两句是同一个意思,但是第一句在PAT测试点3是超时的。
教训就是,别闲着没事乱写,老老实实写+=
,明明就更简单是不是?
具体原因不太清楚,反正遇上了,就试试换成+=
看对不对?
code
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 0;
string d;
d.resize(1);
scanf("%s%d", &d[0], &n);
for(int i = 1; i < n; i++){
string ans;
int p = 1, q = 0;
d = d + "@";
while(p < d.size()){
if(d[p] != d[q]){
//ans = ans + d[q] + to_string(p - q);
ans += d[q] + to_string(p - q);
q = p;
}
p++;
}
d = ans;
}
printf("%s", d.c_str());
return 0;
}
PAT甲级:1136 A Delayed Palindrome (20分)的更多相关文章
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT 甲级 1054 The Dominant Color (20 分)(简单题)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ...
- PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way ...
- pat甲级 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- 【PAT甲级】1042 Shuffling Machine (20 分)
题意: 输入洗牌次数K(<=20),输入54张牌每次洗入的位置(不是交换的位置),输出洗好的牌. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)
题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...
- 【PAT甲级】1108 Finding Average (20分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
随机推荐
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- Netty 框架学习 —— 传输
概述 流经网络的数据总是具有相同的类型:字节,这些字节如何传输主要取决于我们所说的网络传输.用户并不关心传输的细节,只在乎字节是否被可靠地发送和接收 如果使用 Java 网络编程,你会发现,某些时候当 ...
- Oracle数据库delete删除普通堆表千万条记录
Oracle数据库delete删除普通堆表千万条历史记录. 直接删除的影响: 1.可能由于undo表空间不足从而导致最终删除失败的问题: 2.可能导致undo表空间过度使用,影响到其他用户正常操作. ...
- 报错:vmnet8设置中出现错误。子网IP和子网掩码不一致
报错:vmnet8设置中出现错误.子网IP和子网掩码不一致 设置子网IP时报错,如下图 同样的,写成192.168.0.0就没问题,如下图 总结: 这个虚拟网络编辑器是给添加网卡的,你添加vmnet8 ...
- STL----vector注意事项
开vector时要注意内存容易炸 最好的办法就是在开vector之后,对他进行一步操作 vector<int> a; a.resize(n); n就是你要开的数组的大小,此时数组里已经插入 ...
- 关于WLS2中Ubuntu启用SSH远程登录功能,基于Xshell登录,支持Root
背景介绍 虽然WSL2提供了非常便利的访问Ubuntu目录的形式,但是仍然我们需要通过一个工具,比如XSHELL来实现对Ubuntu的SSH登录. 获取并安装Xshell 7 目前Xshell已经更新 ...
- UV贴图类型
凹凸贴图Bump Map.法线贴图Normal Map.高度贴图Height map.漫反射贴图Diffuse Map.高光贴图Specular Map.AO贴图Ambient Occlusion ...
- 48、django工程(model)
48.1.数据库配置: 1.django默认支持sqlite,mysql, oracle,postgresql数据库: (1)sqlite: django默认使用sqlite的数据库,默认自带sqli ...
- CMD命令进入某个目录
1.开始->运行->CMD 2.进入某个磁盘,直接盘符代号:如D:,不用CD 命令切换 3.进入除根录以下的文件夹 cd 文件夹路径 例如我要进入 E:/Program Files/PHP ...
- uniapp 微信小程序扫码处理
1.view 代码 <view class="v-main-scan"> <uni-icons @click="scanCode" clas ...