今日头条2017客户端工程师实习生笔试题(回文解码)

现在有一个字符串,你要对这个字符串进行 n 次操作,每次操作给出两个数字:(p, l) 表示当前字符串中从下标为 p 的字符开始的长度为 l 的一个子串。你要将这个子串左右翻转后插在这个子串原来位置的正后方,求最后得到的字符串是什么。字符串的下标是从 0 开始的,你可以从样例中得到更多信息。
输入描述:
每组测试用例仅包含一组数据,每组数据第一行为原字符串,长度不超过 10 ,仅包含大小写字符与数字。接下来会有一个数字 n 表示有 n 个操作,再接下来有 n 行,每行两个整数,表示每次操作的(p , l)。
保证输入的操作一定合法,最后得到的字符串长度不超过 1000。
输出描述:
输出一个字符串代表最后得到的字符串。
输入例子:
ab
2
0 2
1 3
输出例子:
abbaabb

思路分析:1、获取字符串的字串后进行反置(例如:abc变成cba);2、将反置后的子串添加在原字符串的后面。

C代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void fanzhi(char *str);
void modify(char *str,int p,int l);
int main(void)
{
    int i,n,p[100],l[100];
    char *str = (char *)malloc(1000 * sizeof(char));
    scanf("%s",str);
    scanf("%d",&n);
    for(i = 0;i < n;i++)
    {
        scanf("%d %d",&p[i],&l[i]);
    }
    for(i = 0;i < n;i++)
    {
        modify(str,p[i],l[i]);
    }
    printf("%s\n",str);
    return 0;
}

void fanzhi(char *str)
{
    int length,i;
    char *temp;
    length = strlen(str);
    temp = (char *)malloc(length * sizeof(char));
    for(i = 0;i < length;i++)
    {
        temp[i] = str[length - 1 - i];
    }
    temp[length] = '\0';
    for(i = 0;i < length;i++)
    {
        str[i] = temp[i];
    }
    str[length] = '\0';

}

void modify(char *str,int p,int l)
{
    int i,length;
    char *temp = (char *)malloc(10 * sizeof(char));
    length = strlen(str);
    for(i = p;i < p + l;i++)
    {
        temp[i - p] = str[i];
    }
    temp[l] = '\0';
    fanzhi(temp);
    for(i = length;i < length + l;i++)
    {
        str[i] = temp[i - length];
    }
    str[length + l] = '\0';

}

此题博主觉得有些麻烦,求各位大神看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值