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 ...
随机推荐
- Nsight Compute Profilier 分析
profiler报告包含每次内核启动分析期间收集的所有信息.在用户界面中,它包含一个包含常规信息的标题,以及用于在报告页面或单个收集的启动之间切换的控件.默认情况下,报告以选定的详细信息页面开始. 页 ...
- 记录第一次完整的uni-app开发经验
由于我是做后端的,一直没有做过前端的页面,以前在学校图书馆看的都是jsp技术,几乎是把java代码嵌套在前端界面.后面出来实习了才发现,jsp是真的落后了.现在的大学和实际工作所需偏差太大了,没办法. ...
- 【NX二次开发】判断面是否相切,相切面。
判断面是否相切,相切面. 用到的函数: UF_MODL_ask_minimum_dist 获取两个对象之间的最短距离,以及点坐标 UF_MODL_ask_face_parm 给定一个参考点,返回曲面上 ...
- 好用的Java工具类库,GitHub星标10k+你在用吗?
简介 Hutool是Hu + tool的自造词,前者致敬我的"前任公司",后者为工具之意,谐音"糊涂",寓意追求"万事都作糊涂观,无所谓失,无所谓得& ...
- TestNG 组测试
方法分组测试 1. 给@Test注解后面加groups参数,如 @Test(groups = "groupa") 2. 可以添加@BeforeGroups和@AfterGroups ...
- redis cluster如何支持pipeline
当我们要操作一批key时,可以通过 redis pipline 再执行完后一次性读取所有结果来较少网络传输的消耗: 很明显,这有个限制条件 => 这批key的执行必须在同一个连接上 当部署的re ...
- 从 Vue parseHTML 来学习正则表达式
从 Vue parseHTML 所用正则来学习常用正则语法 Vue parseHTML 中所用的所有正则如下.常见正则规则可参见附录 1,Vue parseHTML 正则所用规则均可在其中找到定义. ...
- hive学习笔记之九:基础UDF
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 『心善渊』Selenium3.0基础 — 20、Selenium对Cookie的操作
目录 1.Cookie介绍 2.Session介绍 3.Cookie工作原理图解 4.Cookie内容参数说明 5.Selenium操作Cookie的API 6.Selenium操作Cookie的示例 ...
- CRM企业管理系统对于企业的价值
对于企业来说,一个完整的工作流程可以概括为三个阶段:售前.售中.售后.每个阶段都需要不同的管理.此外,客户关系管理客户关系管理系统可以帮助企业在这三个阶段进行业务管理和客户管理,帮助企业更好地运作,增 ...