[Bzoj 1789] [Bzoj 1830] [AHOI 2008] Necklace Y-Necklace

Source: Internet
Author: User

1789 & 1830: [Ahoi2008]necklace y-Necklace time limit:1 Sec Memory limit:64 MB
Description Fun Island on the many exciting activities to make small cocoa they play very happy. Now they're playing a game of string necklaces, and the fastest way to get a good prize. This is not an ordinary necklace, but a Y-shaped necklace, the necklace of the center of a large pearl as a combination of points, from the big pearls connected out of 3 of various gems strung up chain. The rules of the game are this: each time you can remove a gem from one of the three chains, or attach a gem, called an operation, after several operations, and eventually make the three chains exactly the same. To win the game, you can only use as few operations as possible. Let's say that each gem has countless more to use and the chain is long enough. Can you help the few to win the game? Note: There is no special requirement for the number of jewels on the Y-necklace, so even if you take all the gems down, it is an acceptable option (three strings without a string of gems are exactly the same). Input has a total of 3 lines, representing the three chains of the Y-necklace, each beginning with a number n, indicating that the chain was initially a string of N gems (N<=50), followed by a space, then n ' a ' and ' Z ' characters, representing the jewel on the chain, Each letter represents a different gem, the leftmost character of the string represents the gem closest to the Big Pearl, and the rightmost one represents the gem at the end of the chain. Output has only one integer representing the minimum number of operations required. Sample Input3 CAT
3 TAC
5 CATCH
Sample Output8

HINT

Tip: 100% of the data, n<=50.
50% of the data, n<=20.

Exercises

Amount: Just messing around, learning to get a deque on the Internet. Double-ended queue

C + + is good stl good

1#include <cstdio>2#include <deque>3 using namespacestd;4deque<Char> str[3];5 intMain () {6     intn[3];7      for(intI=0;i<3;++i) {8scanf"%d",&n[i]);9         if(!n[i])Continue;Ten         CharC=GetChar (); One          while(c<'A'|| C>'Z') c=GetChar (); A          while(c>='A'&&c<='Z') Str[i].push_back (c), c=GetChar (); -     } -      while(str[0].size () &&str[1].size () &&str[2].size () &&str[0].front () ==str[1].front () &&str[1].front () ==str[2].front ()) { thestr[0].pop_front (); str[1].pop_front (); str[2].pop_front (); -     } -     inttot=str[1].size () +str[0].size () +str[2].size (); -     intcn1=tot; +      for(intI=0;i<3;++i) -          for(intj=i+1;j<3;++j) { +             intk=0; A              for(; K<min (Str[i].size (), Str[j].size ()) &&str[i][k]==str[j][k];++k); at             intRes=tot-str[i].size ()-str[j].size (); -res+=K; -Res+=str[i].size ()-k+str[j].size ()-K;  -ans=min (ans,res); -         } -printf"%d\n", ans); in}
View Code

[Bzoj 1789] [Bzoj 1830] [AHOI 2008] Necklace Y-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.