In C language, there are two functions for copying arrays, memcpy and
memmove. These two C language functions have a same purpose is copying the
contents of a certain section of memory to a section of memory in
units of bytes.The difference between these two functions is that memcpy
does not check whether the source address range and the destination
address range in memory have overlapping parts. It follows the logic
of copying each byte sequentially from left to right, so that can
cause the data copied is not equal to original data.When using the memmove function, if the source address range and the
destination address range have overlapping part, memmovewill do special processing to ensure that the memory of the
destination address range is filled with original data.Now suppose that both the memcpy and memmove function calls require
three parameters, p1,p2,l, which respectively represent the source
address, destination address, and the number of bytes copied. For
example, given a string “abcdefg”, if you call memcopy(1,3,5), the
result will be “abababa”, if you call memmove(1,3,5), the result will be It is “ababcde”.
Now give you a string S, and three call parameters, p1,p2,l . Please
tell Chino what the calling results of memcopy and memmoverespectively are. Input The first line of input data contains only a positive integer n(1≤n≤105)
indicates the length of the string.
The second line of input data contain a string S of length n. S
includes only lowercase English letters.The third line of input data contain three integers
p1,p2,l(1≤p1,p2,l≤n)And guarantee 1≤p1+l−1,p2+l−1≤n
represents the parameters of calling two functions . Output Please output two lines, the first line represents the result of calling the memcopy function, the second line represents the result of
calling memmove
. Example Input 7 abcdefg 1 3 5 Output abababa ababcde
思路
看懂题就能过,按照题目模拟两种拷贝方式,一种是从左至右边覆盖边拷贝,另一种要求不能覆盖,需要拷贝原始数据。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
char s[MAXN],t[MAXN];
int n,p1,p2,l;
int main()
{
scanf("%d",&n);
scanf("%s",s+1);
strcpy(t+1,s+1);
scanf("%d %d %d",&p1,&p2,&l);
for(int i=0;i<l;i++)t[p2+i]=s[p1+i];//还是按照原来的进行复制
for(int i=0;i<l;i++)s[p2+i]=s[p1+i];//直接覆盖
printf("%s\n%s\n",s+1,t+1);
return 0;
}