Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.7 kB
0
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long int n,m;
typedef struct node{
    char* st;
    long int stl;
    struct node* next;
}Node;
Node* createNode();
void swap(long int,long int);
void move(long int,long int,long int);
Node* head;
Node* address[100001];
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%ld",&n);
    head = createNode();
    scanf("%ld",&m);

    while(m--){
        long int x,a,b;
        scanf("%ld %ld %ld",&x,&a,&b);
        if(x==1){
            move(x,a,b);
        }
        else if(x==2){
            move(x,b,a);
        }
        else if(x==3){
            swap(a,b);
        }
     }
     int d=1;
     while(d<=n){
         Node* cp = address[d];
         if(cp->stl==0){
             printf("\n");
             d++;
         }
         else{
             printf("%s",cp->st);
             printf("\n");
             d++;
         }
         
     }
}

Node* createNode(){
    Node* head = (Node*)malloc(sizeof(Node*));
    head->next = NULL;
    Node* cp = head;
    int c;
    int a=1;
    char* s;
    Node* p ;
    while(n--){
    scanf("%ld",&c);
    if(c==0){
      s = (char*)malloc(sizeof(char));
      *s = '\0';
    }
    else{
    s = (char*)malloc((c+1)*sizeof(char));
    scanf(" %s",s);
    }
    cp->st = s;
    cp->stl = strlen(s);
    address[a] = cp;
    if(n==0){
        cp->next = NULL;
    }
    else{
    p = (Node*)malloc(sizeof(Node*));
    cp->next = p;
    cp = cp->next;
    }
    a++;
    }
    cp->next = NULL;
   
    return head;
}

void move(long int x,long int a,long int b){
    Node* p1 = address[a];
    
    char* s1 = p1->st;
    Node* p2 = address[b];
    
    char* s2 = p2->st;
    strcat(s1,s2);
    if(p2->stl!=0)
    free(p2->st);
    char* ts = (char*)malloc(sizeof(char));
    *ts = '\0';
    if(x==1){
        p2->st = p1->st;
        p1->st = ts; 
        p2->stl = (p1->stl)+(p2->stl);
        p1->stl = 0;   
    }
    else if(x==2){
    p2->st = ts;
    p1->stl = (p1->stl)+(p2->stl);
    p2->stl = 0;
    }
}

void swap(long int a,long int b){
    Node* p1 = address[a];
    char* s1 = p1->st;
    Node* p2 = address[b];
    char* s2 = p2->st;
    char* sa = s1;
    char* sb = s2;
    long int templ;
    long int s1l = p1->stl;
    long int s2l = p2->stl;
    char* temp = (char*)malloc(s1l*sizeof(char));
    strcpy(temp,sa);
    strcpy(sa,sb);
    strcpy(sb,temp);
    templ = s1l;
    s1l = s2l;
    s2l = templ;
    p1->stl = s1l;
    p2->stl = s2l;
    if(temp!=NULL)
    free(temp);
}