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分)的更多相关文章

  1. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  2. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  3. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  4. PAT 甲级 1054 The Dominant Color (20 分)(简单题)

    1054 The Dominant Color (20 分)   Behind the scenes in the computer's memory, color is always talked ...

  5. PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)

    1027 Colors in Mars (20 分)   People in Mars represent the colors in their computers in a similar way ...

  6. pat甲级 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  7. 【PAT甲级】1042 Shuffling Machine (20 分)

    题意: 输入洗牌次数K(<=20),输入54张牌每次洗入的位置(不是交换的位置),输出洗好的牌. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  8. 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)

    题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...

  9. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

随机推荐

  1. vulhub-struct2-s2-005

    0x00 漏洞原理   s2-005漏洞的起源源于S2-003(受影响版本: 低于Struts 2.0.12),struts2会将http的每个参数名解析为OGNL语句执行(可理解为java代码).O ...

  2. th:insert、th:replace、th:include抽取和引用页面公共片段、传参等

    一.抽取公共片段 th:fragment  给片段命名 将公共片段抽取出来,并在顶级标签内使用th:fragment给该片段命名. 例如:将公共片段抽取出来放到comment/bar.html中: & ...

  3. 【NX二次开发】UF_CSYS_map_point()函数,绝对坐标,工作坐标,部件之间坐标转换。

    UF_CSYS_map_point用来变换点的坐标,比较简单且实用.例如工作坐标系与绝对坐标系转换,一个部件的坐标与另一个部件坐标系之间的转换.下面的例子是在三个坐标下创建三个点相对坐标为{10,50 ...

  4. css基本内容笔记(学习整理)

    一.css简介 1.什么是css 层叠样式表. 层叠:层层叠加,若果有冲突应用优先级高,不冲突的部分共同作用 样式表:就是css属性样式的集合: 2.作用 a.修饰html,使得html样式更好看 b ...

  5. Ubuntu18.04下安装Docker并配置SSL证书加密远程连接

    Docker下载与安装 下载安装包 国内网络连接docker镜像还是比较慢的,这里推荐直接下载docker镜像,Ubuntu镜像下载路径为:https://download.docker.com/li ...

  6. OSPF路由协议

    一.OSPF的工作过程 二.OSPF的基本概念 三.DR与BDR 四.OSPF数据包类型 五.OSPF邻接关系的建立 六.OSPF的路由器类型 一.OSPF的工作过程 OSPF路由协议针对每一个区域分 ...

  7. 让Qt5默认支持C++17的设置方法

    单个项目的设置方法 打开项目的 pro 文件,将CONFIG += console c++11改为CONFIG += console c++17即可. 永久默认支持 c++17 的设置方法 找到 Qt ...

  8. uniapp 打包IOS 更新AppStore版本

    Hello 你好,我是大粽子. 最近随着新版本UI的发布APP也随之更新,随之而来的也就是IOS程序提审步骤,这次我详细的截图了每一个步骤,如果你正好也需要那么跟着我的节奏一步步来肯定是没问题的. 提 ...

  9. 分布式AKF拆分原则

    1. 前言 当我们需要分布式系统提供更强的性能时,该怎样扩展系统呢?什么时候该加机器?什么时候该重构代码?扩容时,究竟该选择哈希算法还是最小连接数算法,才能有效提升性能? 在面对 Scalabilit ...

  10. VS 2017 RC .net core ef+ MySql 出现错误

    在di注入时会出现错误 MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLCommandBuilderFactory..ctor(ISensit ...