Blue Bridge cup algorithm improves shield God and bar necklace

Source: Internet
Author: User
Tags add time

The topics are as follows:

  Originally wrote a chain list, but there is a problem in writing, but after 3 groups of data, and later in the classmate's hint, with the array to emulate the list, and then I use 2 yuan array to simulate the two-way list, because this problem color is not repeated, so the simulation is relatively simple, the code is as follows:

  

#include <stdio.h>typedefstructcolor{intPre; intNext;} Color;color c[100001];intM,n;intMain () {inti,j,cr,p0=0, N0,p,q; Chars[4]; scanf ("%d%d",&n,&m);  for(i=0; i<n;i++) {scanf ("%d",&CR); N0=CR; C[p0].next=N0; C[n0].pre=P0; P0=N0; } C[p0].next=-1;  while(m) {scanf ("%s", s); if(s[0]=='A') {scanf ("%d%d",&p,&Q); P0=C[p].pre; C[p0].next=Q; C[q].pre=P0; C[q].next=P; C[p].pre=Q; N++; }        if(s[0]=='D') {scanf ("%d",&P); P0=C[p].pre; N0=C[p].next; C[p0].next=N0; C[n0].pre=P0; N--; } m--; } printf ("%d\n", N); I=0;  while(c[i].next!=-1) {printf ("%d", C[i].next); I=C[i].next; }    return 0;}

  This code has been evaluated because the Find delete add Time is O (1), so it is much stronger than the linked list. (It can be a bit more complicated if the color allows repetition, but a hash-like approach should be much stronger than a simple list)

  

Blue Bridge cup algorithm improves shield God and bar necklace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.