题解 | #游游的字母翻倍#

游游的字母翻倍

https://www.nowcoder.com/practice/e10ed38e315442fc9772004c58582c8f

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
    int n, q;
    while (scanf("%d %d", &n, &q) != EOF) {
        char *str = (char*)malloc(sizeof(char) * (n + 1));
        if(str == NULL) {
            printf("内存分配失败");
            return 0;
        }
        scanf("%s",str);
        str[n] = '\0';
        int l, r;
        for(int i = 0; i < q; i++) {
            scanf("%d %d", &l, &r);
            int len = strlen(str);
            char* tmp = (char*)malloc(sizeof(char) * (len + r - l + 1));
            for(int j = 0; j < l - 1; j++) {
                tmp[j] = str[j];
            }
            int index = l - 1;
            for(int j = l - 1; j < r; j++) {
                tmp[index] = str[j];
                index++;
                tmp[index] = str[j];
                index++;
            }
            for(int j = r; j < len; j++) {
                tmp[index] = str[j];
                index++;
            }
            tmp[index] = '\0';
            str = tmp;
        }
        printf("%s\n",str);
    }
    return 0;
}

全部评论

相关推荐

2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务