You got a job in String Factory. In String Factory, there are n kinds of raw materials(as strings), which can be processed into products(also as strings). To make perfect products, cutting and splicing are necessary.
Your job is making raw materials into products.
Definition:
// The following definition is the same as in the textbook excluding Substr
typedef struct
{
char* ch;
int length;
}string;
typedef int status;
status Assign(string* str, char* chars);
status Clear(string* str);
int Length(string* str);
status Concat(string* res, string* s1, string* s2);
status Substr(string* res, string* str, int pos, int len); // pos start with 0 instead of 1
Test Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200
#define MAXLEN 2010
typedef struct
{
char* ch;
int length;
}string;
typedef int status;
status Assign(string* str, char* chars);
status Clear(string* str);
int Length(string* str);
status Concat(string* res, string* s1, string* s2);
status Substr(string* res, string* str, int pos, int len);
void Write(string * res);
string str[N];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
char temp[MAXLEN];
scanf("%s", temp);
Assign(str + i, temp);
}
for (int i = 0; i < m; i++)
{
int op;
scanf("%d", &op);
string* product = NULL;
switch (op)
{
case 1:
{
int a, b;
scanf("%d %d", &a, &b);
product = (string*)malloc(sizeof(string));
product->ch = NULL;
Concat(product, str + a, str + b);
break;
}
case 2:
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
product = (string*)malloc(sizeof(string));
product->ch = NULL;
Substr(product, str + a, b, c);
break;
}
}
if (product)
{
Write(product);
Clear(product);
}
}
return 0;
}
/* 请在这里填写答案 */
英文很差,可能看样例清晰点
Output:
Output M lines, each line outputs a contact string or a substring coresponding to OP.
Sample Input:
4 4
love
live
pen
apple
1 1 2
1 4 3
2 1 1 1
2 3 0 3
结尾无空行
Sample Output:
lovelive
applepen
o
pen
the solution is
status Assign(string* str, char* chars){
if(str->ch) free(str->ch);
int i;char *c;
for(i=0,c=chars;*c;++i,++c);
if(!i){
str->ch=NULL;
str->length=0;
}
else{
if(!(str->ch=(char*)malloc(i*sizeof(char))))
return 0;
for(int j=0;j<i;j++){
str->ch[j]=chars[j];
}
str->length=i;
}
return 1;
}
status Clear(string* str){
str->length=0;
free(str->ch);
str->ch=NULL;
return 1;
}
int Length(string* str){
return str->length;
}
status Concat(string* res, string* s1, string* s2)
{
if(res->ch){
free(res->ch);
}
if(!(res->ch = (char *)malloc((s1->length + s2->length)*sizeof(char)))){
return 0;
}
int i;
for(i = 0;i<s1->length;i++){
res->ch[i] = s1->ch[i];
}
res->length = s1->length +s2->length;
int n = 0;
for(i = s1->length;i < res->length;i++,n++){
res->ch[i] = s2->ch[n];
}
return 1;
}
status Substr(string* res, string* str, int pos, int len)
{
int n;
if(pos<0||pos>str->length||len<0||len>str->length-pos+1)
{
return 0;
}
if(!len)
{
res->ch=NULL;
res->length=0;
}
else
{
res->ch=(char *)malloc((len + 1) * sizeof(char));
res->length=len;
// int n = 0;
// for(n=0;n<=len-1;n++)
// {
// res->ch[n]=str->ch[pos+n-1];
// }
memcpy(res->ch,str->ch+pos,len);
res->ch[res->length]=0;
}
return 1;
}