04-C. DS bidirectional linked list - Zuma

Posted by glassroof on Thu, 07 Oct 2021 22:55:58 +0200

04 - sequence table and stack exercise

Title Description
Zuma is a game once popular all over the world. Its playing method is: several colored beads are initially arranged on a track, of which any three adjacent beads will not be completely the same color. After that, you can launch the beads into orbit and add them to the original sequence. Once three or more beads of the same color become adjacent, they will disappear immediately. Such elimination may occur in a chain, during which you will not be able to launch beads for the time being.

The initial sequence of beads on a given track, followed by a series of operations by the player. Your task is to calculate the new bead sequence in time after each operation.

input
The first line is A string composed of capital letters' A '~'Z', which represents the initial bead sequence on the track, and different letters represent different colors.

The second line is a number n, indicating that the player has n operations in total.

The next n lines correspond to each operation in turn. Each operation is described by a number k and a capital letter, separated by spaces. The capital letter is the color of the new bead. If there are m beads before insertion, position 0-m-1, then K ∈ [0, m] represents the position where the new beads are embedded in the track.


output
Output a total of n lines, giving the bead sequence on the track after each operation (and the elimination phenomenon that may occur immediately).

If there are no beads on the track, it is indicated by "-".

sample input
ACCBA
5
1 B
0 A
2 B
4 C
0 A

ABCCBA
AABCCBA
AABBCCBA

A

There are three methods below. In fact, only the first method can pass OJ, the second method has compilation errors, and the third method has overflow
Very sad, only understand the second and third method

#include<iostream>
#include<string>
using namespace std;

class node
{
public:
 char ch;
 int data;
 node* next;
 node* prior;
};

void create(node* head, int n, string s)
{
    node* tail = head;
    head->data = 0;
    char ch;
    for (int i = 0; i < n; i++)
    {
        node* pnew = new node;
        pnew->next = NULL;
        pnew->prior = tail;
        pnew->ch = s[i];
        tail->next = pnew;
        tail = pnew;
        head->data++;
    }
}
void print(node* head)
{
    node* h = head->next;
    if (!head->data) cout << "-";
    else
    {
        for (int i = 0; i < head->data; i++)
        {
            cout << h->ch;
            h = h->next;
        }
    }
}

//insert
void insert(node* head, int index, char ch)
{
    node* p = head->next;
    if (index)
    {
        for (int i = 1; i < index; i++)
            p = p->next;
        
        node* pnew = new node;
        pnew->ch = ch;
        pnew->prior = p;
        pnew->next = p->next;
        p->next = pnew;
        
        if (pnew->next)
            pnew->next->prior = pnew;
        
        head->data++;
    }
    else if (head->data)
    {
        p = head;
        node *pnew = new node;
        
        pnew->ch = ch;
        pnew->prior = p;
        pnew->next = p->next;
        p->next = pnew;
        pnew->next->prior = pnew;
        
        head->data++;
    }
    else
    {
        p = head;
        node* pnew = new node;
        pnew->ch = ch;
        pnew->prior = p;
        pnew->next = p->next;
        p->next = pnew;
        head->data++;
    }
}
//Traversal check
void rearch(node* head)
{
 node* p = head->next;
 while (p)
 {
  if (!p->next) break;
  if (p->ch == p->prior->ch && p->ch == p->next->ch)
  {
   int sum = 3;
   //Use d0 to point to the front of the leftmost bead to be deleted
   node* d0 = p->prior->prior;
   //Use d1 to point to the successor of the rightmost bead to be deleted
   node* d1 = p->next->next;
   if (d1)
    while (d1->ch == p->ch)
    {
     if (!d1->next) break;
     d1 = d1->next;
     //Total to be deleted++
     sum++;
    }
   if (d1)
   {
    d0->next = d1;
    d1->prior = d0;
   }
   else d0->next = NULL;

   head->data -= sum;
   p = head->next;
  }
  else
   p = p->next;
 }
}
int main()
{
 string s;
 char ch;
 int index;
 cin >> s;
 int n = s.length();
 node *head = new node;
 create(head, n, s);
 int t;
 cin >> t;
 while (t--)
 {
  cin >> index >> ch;
     
  insert(head, index, ch);
  rearch(head);
  print(head);
     
  if (t)cout << endl;
 }
 return 0;
}
#include<iostream>
#include<string.h>
#include<cstdio>
#define MAX 200000000
using namespace std;

typedef struct node
{
 char data;
 struct node *next;
 struct node *prior;
}node;

int main()
{
    int n,len,i,sum;
    node *L=new node;
    struct node *p=L;
    struct node *q;
    char a[100];

    gets(a);
    len=strlen(a);
    
    for(i=0;i<len;i++)
    {
        q=new node;
        q->data=a[i];
        q->prior=p;p->next=q;
        p=q;
    }
    
    cin>>n;
    
    while(n--)
    {
        
        int j;
        char ch;
        cin>>j>>ch;
        
        struct node *p,*q;
        p=L->next;
        while(j--)
            p=p->next;
        
        
        //Insert ch after t
        q=new node;
        q->data=ch;
        q->prior=p->prior;
        p->prior->next=q;
        q->next=p;
        p->prior=q;
        
        len++;
        
        //Delete three letters together
        
        while(1)
        {
        while(q&&q->data==ch)
            q=q->next;
        while(p->prior&&p->data==ch)
            p=p->prior;
            
        struct node *r=p;
        sum=0;
        while(r&&r!=q)
        {
            r=r->next;
            sum++;
        }
        if(sum>=4)
        {
            p->next=q;
            if(q)
                q->prior=p;
            if(p!=L)
                ch=p->data;
            else if(q)
                ch=q->data;
                
            len-=(sum-1);
            //shanchu(ch,p,q,L,len);
        }
        else
            break;
        }
        
        if(len==0)
        cout<<"-";
        else
        {
            p=L;
            for(i=0;i<len;i++)
            {
                p=p->next;
                cout<<p->data;
            }
        }
        cout<<"\n";
    }
    
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define MAX 200000000

//Zuma color blocks
struct Zuma{
    char date;
    Zuma *up;
    Zuma *down;
    Zuma(){};
    Zuma(char s):date(s){};
}*header,*tailer;    //Head and tail sentry

char s[MAX];
int length;    //Linked list length
int k;    //Record data length

//Bidirectional linked list tail interpolation method
void Creat_zuma(char *s)
{
    header = new Zuma();
    header->up = NULL;

    Zuma *rear = header;    //Define tail pointer - threading
    for (int i = 0; i < length; i++)
    {
        Zuma *p = new Zuma(s[i]);
        rear->down = p;
        p->up = rear;

        rear = p;    //Needle change
    }
    tailer = new Zuma();
    rear->down = tailer;
    tailer->up = rear;
    tailer->down = NULL;
}

//Traverse the pointer to find the pos position
Zuma* Find(int pos)
{
    int counter = 0;
    Zuma *p = header;
    if (length - pos >= pos)    //Header traversal
    {
        while (counter < pos && p->down != tailer)
        {
            p = p->down;
            counter++;
        }
    }
    else{                        //Tail traversal
        p = tailer;
        counter = length;
        while (counter >= pos && p->up != header)
        {
            p = p->up;
            counter--;
        }
    }
    return p;
}

//eliminate
Zuma* Remove(Zuma *cur,char c)
{
    while (1)
    {
        Zuma *pre = cur->down;
        int counter = 0;
        while (cur != header && cur->date == c)    //Forward duplicate check
        {
            cur = cur->up;
            counter++;
        }
        while (pre != tailer && pre->date == c)    //Backward duplicate check
        {
            pre = pre->down;
            counter++;
        }
        if (counter >= 3)    //More than three repeating elements
        {
            length -= counter;
            Zuma *p1 = cur->down, *p2;
            while (p1 != pre)    //delete
            {
                p2 = p1->down;
                delete p1;
                p1 = p2;
            }
            cur->down = pre;
            if (pre != NULL)
                pre->up = cur;
            c = cur->date;
        }
        else break;
    }
    return cur;
}

//insert
void Insert(Zuma *p, char c)
{
    Zuma *x = new Zuma(c);
    if (p->down != NULL)
    {
        x->up = p;
        x->down = p->down;
        p->down->up = x;
        p->down = x;
    }
    else{
        x->up = p;
        x->down = NULL;
        p->down = x;
    }
    length++;
}
int main()
{
    int n;
    gets(s);
    scanf("%d", &n);
    length = strlen(s);
    //build
    Creat_zuma(s);

    for (int t = 0; t < n; t++)
    {
        char c[2];
        int pos;
        scanf("%d%s", &pos, &c);

        Zuma *p = Find(pos);
        Insert(p, c[0]);
        Zuma *flag = Remove(p, c[0]);
        //Record
        if (flag == header && flag->down == tailer)
        {
            s[k++] = '-';
            s[k++] = '\n';
        }
        else{
            flag = header;
            while (flag->down != tailer)
            {
                s[k++] = flag->down->date;
                flag = flag->down;
            }
            s[k++] = '\n';
        }
        //Single Input
        if (t == n - 1)
        {
            s[k] = '\0';
            printf("%s", s);
            k = 0;
        }
    }
    return 0;
}

Topics: Algorithm data structure linked list