AZXU5....8....... 1GU2D4B..T....... K........1961.... PO4IUTFV.XWS34WQ. THE.............. Q34NBVC7.LNGLNSNX # TTPG............. ED............... MN............... MLMNG............ ................. ................. # ? QWERTY........ FLHL.......... .............. .............. # ? AZXU5....8....... 1GU2D4B..T....... K........1961.... PO4IUTFV.XWS34WQ. THE.............. Q34NBVC7.LNGLNSNX # TTPG............. ED............... MN............... MLMNG............ ................. ................. # ? QWERTY........ FLHL.......... .............. .............. # ? whensing le...... colmnyou # canget.. correct. output.. # ? 1....t1...x1.. bdd2......h... .....f3...ijk3 d4nx.gcdx.jzy4 # aaa1.a1lk.e1mn b2ov.b2dr.f2we c3zz.c3yu.g3po d4ag.d4qq.h4ov # xrsz......atus .....fals.agai cde..eerr.nove true.orst.rdne # ? a # b # c # d # ? ? ProblemB.................Atthebeginningthetwentyc Patience.................ardsarelaidinfiverowsbyf Astheproverbsays.........ourcolumnsSee........... Patienceisbitterbutitsfr.Figure1Allthecardsaredea uitissweet...............ltfaceupAnexampleof..... Writingprogramswithinthe.theinitiallayoutisshowni limitedtimemayimposesome.nFigure2................ patience.................Thepurposeofthegameistor onyoubutyouenjoyitandwin.emoveasmanycardsas...... thecontestwehope.........possiblebyrepeatedlyremo .........................vingapairofneighboringca Thewordpatiencehasthemea.rdsofthesame............ ningofperseverancebutith.facevalueLetuscallsuchap asanothermeaning.........airamatchingpair........ incardgamesCardgamesforo......................... neplayerare..............Thephraseapairofneighbor calledpatienceintheUKand.ingcards................ solitaireintheUS.........meansapairofcardswhichar .........................eadjacenttoeachotherFore Letsplayapatienceinthisp.xampleinFigure1C6is..... roblem...................adjacenttoanyofthefollow .........................ingeightcards........... Inthiscardgameyouuseonly.C1C2C3C5C7C9C10andC11Inc twentycardswhosefacevalu.ontrastC3isadjacenttoonl esare....................ythefollowing........... positiveandlessthanorequ.threecardsC2C6andC7..... alto5....................Everytimeyouremoveapairy Justfourcardsareavailabl.oumustrearrangetheremain eforeachfacevalue........ingcardsascompactaspossi .........................ble..................... # Toputitconcretelyeachrem.2oftheaboveprocedureTher ainingcardCimustbeexamin.esult................... edinturninits............ofthegamedependsonthesec subscriptordertobeshifte.hoices.................. dtotheuppermostleftmost..Yourjobistowriteaprogram space....................whichanswerstheminimalpe Howtoplay................naltyforeachinitiallayou 1Searchamatchingpair.....t....................... 2Whenyoufindmorethanonep......................... airchooseone.............Input................... InFigure3youdecidedtorem......................... ovethepairofC6andC9......Theinputconsistsofmultip 3Removethepair...........lecardlayoutsTheinputisg 4Shifttheremainingcardst.iveninthefollowingformat otheuppermostleftmostspa......................... ce.......................N....................... 5Repeattheaboveprocedure.Layout0................. untilyoucannotremoveanyp.Layout1................. air.............................................. Ifyoucanremoveallthetwen.LayoutN1................ tycardsyouwinthegameandy......................... ourpenaltyis0............Nisthenumberofcardlayout Ifyouleavesomecardsyoulo.sEachcardlayoutgivesthei sethegameandyour.........nitialstateofagame...... penaltyisthenumberofther.Acardlayoutisgiveninthef emainingcards............ollowingformat.......... Wheneveryoufindmultiplem......................... atchingpairsyoumustchoos.C0C1C2C3................ eonepairoutofthemasinthe.C4C5C6C7................ step.....................C8C9C10C11.............. # C12C13C14C15.............3534.................... C16C17C18C19.............3354.................... .........................4231.................... Ciisanintegerfrom1to5whi.2531.................... chrepresentsthefacevalue.3542.................... ofthecard................1541.................... .........................4532.................... Output........................................... .........................OutputfortheSampleInput. Foreveryinitialcardlayou......................... ttheminimalpenaltyshould.0....................... beoutputeachinaseparatel.4....................... ine......................12...................... .........................0....................... SampleInput..............0....................... ................................................. 4................................................ 1452............................................. 3143............................................. 5422............................................. 4523............................................. 1135............................................. 5151............................................. 4532............................................. 3214............................................. 1453............................................. 2342............................................. 1212............................................. 5454............................................. 2121............................................. # ? AincludeJBstdioD.JJcliDsinLaddrDs.JJreturnJfdJI... hC...............LaddrJQJinetLadd.H............... AincludeJBsysRty.rEipFJI.......................... pesDhC...........JJcliDsinLportJQ.AdefineJCOMMANDL AincludeJBsysRso.JhtonsEatoiEport.ENJ1024......... cketDhC..........numFFJI..........AdefineJMAXBUFJ2 AincludeJBnetine.JJifEPJstrcmpEsp.56.............. tRinDhC..........rotoKOtcpOFFJG................... AincludeJBstring.JJJJtypeJQJSOCKL.voidJReturnError DhC..............STREAMJIJprotoJQ.EFILEJZsfpKJintJ .................JIPPROTOLTCPJI...codeKJcharJZmess AdefineJWQLENJ5..JJH..............ageF............ .................JJelseJG.........G............... voidJerrorEcharJ.JJJJtypeJQJSOCKL.JJfprintfEsfpKJO ZsF..............DGRAMJIJprotoJQJ.HTTPR1D0JVdJVsSr G................IPPROTOLUDPJI....SnOKJcodeKJmessa JJperrorEsFJI....JJH..............geFJI........... JJexitE1FJI......JJfdJQJsocketEAF.JJfprintfEsfpKJO H................LINETKJtypeKJpro.ContentNtypeYJte .................toFJI............xtRplainSrSnOFJI intJSetupForClie.JJifEfdJBJ0FJerr.JJfprintfEsfpKJO ntEcharJZipKJcha.orEOcanJnotJopen.SrSnOFJI........ rJZsprotoKJcharJ.JsocketOFJI......JJfprintfEsfpKJO ZportnumF........JJifEbindEfdKJEs.ErrorYJcodeQVdJV G................tructJsockaddrJZ.sSrSnOKJcodeKJme JJintJfdJI.......FMcliKJsizeofEcl.ssageFIJ........ JJstructJsockadd.iFFJBJ0F.........JJfflushEsfpFJI. rLinJcliJI.......JJJJerrorEOcanJn.H............... JJintJtypeKJprot.otJconnectOFJI...voidJRecvCommand oJI..............JJifEtypeJQQJSOC.EFILEJZrfpKJchar .................KLSTREAMJMMJlist.JZcommandF...... JJmemsetEMcliK0K.enEfdKJWQLENFF...G............... sizeofEcliFFJI...JJJJerrorEOcanJn.JJifEfgetsEcomma JJcliDsinLfamily.otJsetJlistenOFJ.ndKJCOMMANDLENKJ JQJAFLINETJI.....I................rfpFJQQJNULLF... # JJJJerrorEOcanJn.sEFILEJZrfpKJFIL.JJSendBodyEsfpKJ otJreceiveJcomma.EJZsfpF..........fpFJI........... ndOFJI...........G................JJfcloseEfpFJI.. H................JJFILEJZfpJI.....H............... voidJSendHeaderE.JJcharJbufTCOMMA................. FILEJZsfpF.......NDLENVJI.........mainEintJargcKJc G................JJcharJcmdTMAXBU.harJZargvTVF.... JJfprintfEsfpKJO.FVKJpathTMAXBUFV.G............... HTTPR1D0J200JOKS.KJverTMAXBUFVJI..JJintJiKJfdKJlKJ rSnOFJI...........................dfdKJclenKddJI.. JJfprintfEsfpKJO.JJRecvCommandErf.JJFILEJZsfpKJZrf ContentNtypeYJte.pKJbufFJI........pJI............. xtRplainSrSnOFJI.JJifEsscanfEbufK.JJstructJsockadd JJfprintfEsfpKJO.JOVsJVsJVsOKJcmd.rLinJcliJI...... SrSnOFJI.........KJpathKJverFJCJ3.JJcharJrequestT1 JJfflushEsfpFJI..JWW..............024VIJ.......... H................JJJJJstrcmpEcmdK.JJ.............. voidJSendBodyEFI.JOGETOFJWWJstrcm.JJfdJQJSetupForC LEJZsfpKJFILEJZf.pEverKJOHTTPR1D0.lientEargvT1VKJO pF...............OFFJG............tcpOKJargvT2VFJI G................JJJJReturnErrorE.JJprintfEOserver JJintJlenJI......sfpKJ501KJOinval.YJwaitngJforJcli JJcharJbufT1024V.idJcommandOFJIJ..entSnOFJI....... JI...............JJJJreturnJ501JI.JJclenJQJsizeofE JJwhileEElenJQJf.JJH..............cliFJI.......... readEbufKsizeofE.JJifEEfpJQJfopen.JJdfdJQJacceptEf charFKJsizeofEbu.EpathX1KOrOFFJQQ.dKJEstructJsocka fFKJfpFFJCJ0F....JNULLFJG.........ddrJZFMcliKJMcle JJJJfwriteEbufKs.JJJJReturnErrorE.nFJI............ izeofEcharFKJlen.sfpKJ404KJOfileJ.JJifEdfdJBJ0FJer KJsfpFJI.........notJfoundOFJIJ...rorEOacceptJerro JJfflushEsfpFJI..JJJJreturnJ404JI.rOFJI........... H................JJH..............JJprintfEOserver .................JJSendHeaderEsfp.YJconnectedJfrom intJServerProces.FJI..............JVxSnOKJntohlEcl # iDsinLaddrDsLadd.................................. rFFJI............................................. JJrfpJQJfdopenEd.................................. fdKJOrOFJIJsfpJQ.................................. JfdopenEdfdKJOwO.................................. FJIJ.............................................. JJifErfpJQQJNULL.................................. JWWJsfpJQQJNULLF.................................. JerrorEOcanJnotJ.................................. fdopenOFJI........................................ JJServerProcessE.................................. rfpKJsfpFJI....................................... JJfcloseEsfpFJIJ.................................. JfcloseErfpFJI.................................... JJcloseEdfdFJI.................................... JJcloseEfdFJI..................................... H................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. # ? DS8981....in....... aaa.......1st...... 3dasd.....2001..... 4abcdrejj.dasdsa3.. j.........foru..... ..........5jjyGGHJJ fdd.......67seasofJ # QW1................ STY2............... 3HHG............... ................... ................... ................... ................... # ? JAPAN................ CUP20................ 01................... WIN.................. ..................... # ? thissam ple.... singlec olmn... # areyour program ....... ok..... # ? 1234..nxt1..BTBN..a14fs ............22fdf.dfR24 third.DDS...ds23f.as34R wrpnx.KL....sd24a.EE44G t4....RLOVR.afdfa.H54dn # xt11s.12345............ ds12..6789............. abcde.................. fghij.................. klmno.................. # ? w # x # y # z # ? ? ProblemC.........guardianYourult..enforcesitsloca CyberGuardian....imatemissionist..ladministrative .................ocreateaperfect..policyItreceive Inthegoodoldday..firewallsystem...sbothin........ s................thatcancomplete..boundandoutboun theInternetwasf..lyshutoutanyint..dpacketsnotedat reefromfearsand..rudersinvadingn..atransmittedont terrorism........etworks..........heNetaredivided Peopledidnothav..andprotectchild..intosmallsegmen etoworryaboutan..renfromharmfuli..tscalledpackets ycybercriminals..nformationexpos..andcarefullyins ormad............edontheNetHowev..pects.......... computerscienti..er...............themonebyonewhe sts..............itisextremelydi..therornoteachof Todayhoweveryou..fficultandnoneh..themislegal.The arefacingatroci..aveeverachieved..definitionofthe ouscrackerswher..it...............legalitymayvary everyouare.......beforeAsthefirs..fromsitetositeo unlessbeingdisc..tstepinsteadyou..rdependuponthel onnectedYouhave..arenowrequested..ocaladministrat toprotectyourse..to...............ive............ lvesagainstthei..writeasoftwares..policyofanorgan rattacks.........imulatorundermu..ization.Yoursim .................chsimplerassump..ulatorshouldacc Countinguponyou..tions............ept............ rexcellenttalen...................datarepresentin tforsoftwarecon..Ingeneralafirew..gnotonlyreceive structionand.....allsystemworksa..dpacketsbutalso strongsenseofju..ttheentranceofa..thelocaladminis sticeyouareinvi..localnetworkofa..trativepolicy.. tedtoworkasacyb..norganizationan................. er...............d................Forsimplicityin # thisproblemweas..is...............gative......... sumethateachnet..describedinfilt..integersnandm.. workpacketconsi..eringrules.......Ifbothnandmarez stsofthreefield..eachofwhichspec..eros,thismeanst s................ifiessomecollec..heendofinputOth itssourceaddres..tionofsource.....erwise......... sdestinationadd..destinationaddr..nlineseachrepre ress.............esspairsanddefi..sentingafilteri andmessagebodyT..nesthosepackets..ngruleandmlines hesourceaddress..with.............eachrepresentin specifies........thespecifiedadd..ganarrivingpack thecomputerorap..resspairseither..etfollowinthiso pliancethattran..legalorillegal...rderYoumayassum smitsthepacketa...................ethatn......... ndthedestinatio..Input............andmarelessthan naddress.........Theinputconsist..orequalto1024.. specifiesthecom..sofseveraldatas................. puterorapplianc..ets..............Eachruleiisinon etowhichthepack..eachofwhichrepr..eofthefollowing etistransmitted..esentsfiltering..formats........ Anaddressinyour..rulesandreceive................. simulatorisrepr..dpacketsinthefo..permitsourcepat esentedaseightd..llowingformat....terndestination igits............nm...............pattern........ suchas03214567o..rule1........................... r31415926........rule2............denysourcepatte insteadofusingt..rulen............rndestinationpa hestandardnotat..packet1..........ttern.......... ionof............packet2......................... IPaddressessuch..packetm..........Asourcepatterno as19216811Admin..Thefirstlinecon..rdestinationpat istrativepolicy..sistsoftwononne..ternisacharacte # rstringoflength..rn...............sourceaddressde eight............respectivelyiti..stinationaddres whereeachcharac..spermittedtopas..smessagebody... teriseitheradig..sthe............................ it0to9ora........firewallorthere..Eachofthefirstt wildcardcharact..questisdeniedac..woisacharacters erForinstance15..cordingtothekey..tringoflengthei matchesanyaddre..word.............ghtthatconsists ss...............Notethatapermit..solelyofdigits. whosefirstandfi..ruleandadenyrul..Thelastoneisach fthdigitsare1an..ecancontradicts..aracterstringco d5respectivelyI..incetheymay......nsisting....... n................sharethesamesou..solelyofalphanu generalawildcar..rceanddestinati..mericcharacters dcharactermatch..onaddresspair....atozAtoZand0to9 esanysingledigi..Forthepurposeof..Neitherwhitespa twhileadigitmat..conflictresolut..cesnorspecialch chesonlyitself...ionwedefineapri..aracterscanoccu .................orityruleruleih..rina........... Withthekeywords..asahigher........messagebodyYoum permitanddeny....priorityoverrul..ayassumethatiti filteringruless..ejifandonlyifij..snotemptyandtha pecifylegalandi..Forcompleteness..titslengthisatm llegalpacketsre..wedefinethedefa..ost50.......... spectivelyThati..ultruleanypacke................. s................tisillegalunles..Youmayalsoassum ifthesourceand...sbeingexplicitl..ethatthereisexa destinationaddr..yspecified.......ctlyonespace... essesofapackeda..legalbysomegive..characterbetwee rematchedwithso..nrule............nanytwoadjacent urcepatternandd..Apacketisinthef..fieldsinaninput estinationpatte..ollowingformat...linerepresentin # garule...........ylinesmustnotbe................. or...............written..........2.............. apacket...........................192168910123434 .................SampleInput......5Hello......... Output...........25...............192168091120034 Foreachdatasetp..permit192168123..0World......... rintthenumberof..4................1.............. legalpacketsint..deny19216899012..123456782345678 hefirstline......3435.............9Hello......... followedbyallle..192167111123334................. galpacketsinthe..0HiIamACracker.................. sameorderasthey..192168910123434................. occurin..........5Hello.......................... thedatasetEachp..192168990123434................. acketmustbewrit..5HiIamAlsoACrac................. tenexactlyinone..ker............................. line.............192168091120034................. Ifthedatasetinc..0World.......................... ludestwopackets..000000009999999................. consistingofthe..9TheEndOfTheWor................. samesourceand....ld.............................. destinationaddr..12.............................. essesandthesame..permit123456782................. messagebody......3456789......................... youshouldconsid..192168910123434................. erthemdifferent..5Hello.......................... packetsandsothe..123456782345678................. ymustbewritteni..9Hello.......................... n................00.............................. differentlines................................... Anyextrawhitesp..OutputfortheSam................. acesorextraempt..pleInput........................ # ? AincludeIBstdioDhC........IIcliDsinKportIRIhtonsEa AincludeIBsysQtypesDhC....toiEportnumFFIJ......... AincludeIBsysQsocketDhC...IIifEOIstrcmpEsprotoLPtc AincludeIBnetinetQinDhC...pPFFIG.................. AincludeIBstringDhC.......IIIItypeIRISOCKKSTREAMIJ AdefineIWQLENI5...........IprotoIRIIPPROTOKTCPIJ.. voidIerrorEcharIVsF.......IIH..................... G.........................IIelseIG................ IIperrorEsFIJ.............IIIItypeIRISOCKKDGRAMIJI IIexitE1FIJ...............protoIRIIPPROTOKUDPIJ... H.........................IIH..................... intISetupForClientEcharI..IIfdIRIsocketEAFKINETLIt VipLIcharIVsprotoLIcharI..ypeLIprotoFIJ........... VportnumF.................IIifEfdIBI0FIerrorEPcanI G.........................notIopenIsocketPFIJ..... IIintIfdIJ................IIifEbindEfdLIEstructIso IIstructIsockaddrKinIcli..ckaddrIVFNcliLIsizeofEcl IJ........................iFFIBI0F................ IIintItypeLIprotoIJ.......IIIIerrorEPcanInotIconne IImemsetENcliL0LsizeofEc..ctPFIJ.................. liFFIJ....................IIifEtypeIRRISOCKKSTREAM IIcliDsinKfamilyIRIAFKIN..INNIlistenEfdLIWQLENFF.. ETIJ......................IIIIerrorEPcanInotIsetIl IIcliDsinKaddrDsKaddrIRI..istenPFIJ............... inetKaddrEipFIJ...........IIreturnIfdIJ........... # H.........................G....................... voidIServerProcessEcharI..IIintIiLIfdLIlLIdfdLIcle VinLIFILEIVsfpF...........nLddIJ.................. G.........................IIFILEIVsfpLIVrfpIJ..... IIintIcLIlcapLIscapLInum..IIstructIsockaddrKinIcli sLIothersIJ...............IJ...................... IIforElcapRscapRnumsRoth..IIcharIrequestU1024TJI.. ersR0IJIEcIRIVinXXFIORIZ..II...................... SnZIJIFIG.................IIfdIRISetupForClientEar IIIIifEZaZIBRIcINNIcIBRI..gvU1TLIPtcpPLIargvU2TFIJ ZzZFIXXscapIJ.............IIprintfEPserverWIwaitng IIIIelseIifEZAZIBRIcINNI..IforIclientSnPFIJ....... cIBRIZZZFIXXlcapIJII......IIclenIRIsizeofEcliFIJ.. IIIIelseIifEZ0ZIBRIcINNI..IIdfdIRIacceptEfdLIEstru cIBRIZ9ZFIXXnumsIJ........ctIsockaddrIVFNcliLINcle IIIIelseIXXothersIJ.......nFIJ.................... IIH.......................IIifEdfdIBI0FIerrorEPacc IIfprintfEsfpLIPDLCAPRYd..eptIerrorPFIJ........... LSCAPRYdLNUMSRYdLothersR..IIprintfEPserverWIconnec YdSnPL....................tedIfromIYxSnPLIntohlEcl IIlcapLscapLnumsLothersF..iDsinKaddrDsKaddrFFIJ... IJ........................IIrfpIRIfdopenEdfdLIPrPF H.........................IJIsfpIRIfdopenEdfdLIPwP mainEintIargcLIcharIVarg..FIJI.................... vUTF......................IIifErfpIRRINULLI||IsfpI # RRINULLFIerrorEPcanInotI.......................... fdopenPFIJ........................................ IIdoIG............................................ IIIIifEfgetsErequestLIsi.......................... zeofErequestFLIrfpFIRRIN.......................... ULLFIbreakIJ...................................... IIIIifErequestU0TIRRIZQZ.......................... FI................................................ IIIIIIfputsEPQSnPLIsfpFI.......................... J................................................. IIIIelseI......................................... IIIIIIServerProcessErequ.......................... estX1LIsfpFIJI.................................... IIIIfflushEsfpFIJ................................. IIHIwhileErequestU0TIORI.......................... ZQZFIJ............................................ IIfcloseEsfpFIJIIfcloseE.......................... rfpFIJ............................................ IIcloseEdfdFIJ.................................... IIcloseEfdFIJ..................................... H................................................. .................................................. .................................................. .................................................. .................................................. # ?