Algorithm note _098: Blue Bridge cup practice algorithm to improve the shield God and the bar necklace (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem DescriptionOne day, the shield God picked up a lot of colorful beads! He thought the beads were so beautiful that they could make a necklace and give it to the girl he wanted-so he used some of the beads to create a necklace of length n. When he was ready to bring the necklace to the end, the earth came in.
"Wow so disgusting necklace you can do it!!!" "
Shield God knows the aesthetic is not his forte, so he is very modest to ask Earth, how to make a beautiful necklace.
"Well, first you have to add a bead of this color here, and then you get rid of the bead here, and then ... and finally you see if it's pretty," said the Earth-moving man.
The shield God thinks this to do with the artificial too troublesome, therefore gives you. Input FormatThe first row two numbers, respectively, are n,m.
the second row n number, represents the shield God first necklace. The number of I indicates the color of the bead of the first I.
Next M-line, one of the following forms:
Add P Q: Represents a bead with a color of Q in front of the beads of color p.
DEL P: means to remove the beads with the color p, if it is not at the end point, you need to connect the two beads next to it. For example, when a necklace status is 1 4 5 8, Execution del 4 becomes 1 5 8, and Execution del 1 becomes 4 5 8.
The input guarantees that the necklace has a bead of color p before each operation, and the beads are different in color at any time. output FormatThe first act of a number Len, for the length of the necklace after all operations have been done.
The number of Len in the second row, indicating the status of the necklace at this time. The number of I indicates the color of the bead of the first I. Sample InputTen 5
1 2 3 4 5 6 7 8 9
DEL 5
ADD 7 5
DEL Ten
ADD 4
ADDSample Output One
1 2 3 4 6 5 7 8 9data size and conventionsthe number representing the color does not exceed 10^5 positive, 1<=n<=10^4,1<=m<=10^4.   2 Solutions

run results scored score of : Run time-out. Code for reference only ~

The specific code is as follows:

ImportJava.util.Scanner;ImportJava.util.Vector; Public classMain { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in);        intn =In.nextint (); intm =In.nextint (); Vector<Integer> list =NewVector<integer>();  for(inti = 0;i < n;i++) List.add (In.nextint ());  for(inti = 0;i < m;i++) {String operation=In.next (); if(Operation.equals ("ADD")) {                intP =In.nextint (); intQ =In.nextint (); intj =List.indexof (P);            List.add (J, Q); } Else if(Operation.equals ("DEL")) {                intP =In.nextint (); intj =List.indexof (P);            List.remove (j);        }} System.out.println (List.size ());  for(inti = 0, Len = list.size (); I < len;i++) System.out.print (List.get (i)+" "); }}

Algorithm note _098: Blue Bridge cup practice algorithm to improve the shield God and the bar necklace (Java)

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.