uva10152-Tortoise Shell Sort

Source: Internet
Author: User
Tags define local strcmp

The training reference for the basic linear table of data structure in small white book is taken from UVA

Topic Connection http://acm.hust.edu.cn/vjudge/problem/19299

Virtual judge, you can go to register.

Thinking of solving problems

Try to solve the sample, you will get the experience:

Find the first name in the target sequence that is not matched (from the last matching position in the initial sequence). Pull the name from the initial position to the top of the stack.

Repeat until the target sequence matches all.

Note matching refers to a scan sequence, and the pointer does not turn back.

Write the code with this idea and hand it over to AC. Why is such a minimum number of steps? Each time a mismatched name is pulled from the top of the stack, the total number of matches is added one, and it cannot be reduced because you are from the sequence

Start with a match. Is it possible to increase the number of matches after one move? Also impossible (think about why).

In the code, I used a dynamic single-linked list to simulate the turtles.

Oh, that's right. When reading the data, remember to eat all the blanks ... Don't think a getchar () will eat, there may be a lot of space ...

Code:

#include <iostream>#include<string.h>#include<cstdio>using namespacestd;//#define LOCALtypedefstructNode {Chars[ -]; structNode *Next;} Node;voidADD (node *first, node *&rear) {Node*temp =NewNode; Gets (temp->s); Temp->next = first->Next; First->next =temp; if(Rear->next! = NULL) rear = rear->next;}voidDeleteadd (Char*s, node *f, node *&R) {Node*p = f->next, *pre =F;  while(P! = NULL && strcmp (S, p->s)) {pre = P; p = p->Next;} Pre->next = p->Next; R->next =p; R=p; P->next =NULL;}voidDoIt (node *firstr, node *&rearr, node *firstt, node *&Reart) {Node*RAWF =Firstr; Node*TARGETF =Firstt; Node*rawr =Rearr; Node*TARGETR =Reart; Node*PR, *pt; PR= rawf->next; PT = targetf->Next;  while(pt! =NULL) {         while(pr = NULL && strcmp (Pr->s, pt->s)) PR = pr->Next; if(pr = =NULL) {cout<< Pt->s <<Endl; Deleteadd (PT-s, RAWF, Rearr); PR= rawf->next; PT = targetf->Next; }        ElsePT = pt->Next; }}voidDelete (Node *f) {Node*Q;  while(P!=null) {f = f->next;DeleteP p =f;}}intMain () {#ifdef LOCAL freopen ("Data.txt","R", stdin); Freopen ("Ans.txt","W", stdout); #endif    intN; scanf ("%d", &N);  while(n--) {Node*firstr =NewNode; Firstr->next =NULL; Node*rearr =Firstr; Node*FIRSTT =NewNode; Firstt->next =NULL; Node*reart =Firstt; intNumber ; scanf ("%d", &Number ); CharC;  while(c = GetChar ()! ='\ n') ;  for(intI=0; i<number; i++) Add (Firstr, Rearr);  for(intI=0; i<number; i++) Add (FIRSTT, Reart);        DoIt (Firstr, Rearr, Firstt, Reart); Delete (FIRSTR);        Delete (FIRSTT); cout<<Endl; }    return 0;}

uva10152-Tortoise Shell Sort

Related Article

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.