├── .gitattributes ├── .gitignore ├── README.md ├── liveArchive └── year2000 │ └── europe │ └── southeastern │ └── ClosestCommonAncestor_LA2045.java ├── regionals ├── dhaka2015 │ ├── AutomaticCheaterDetection.java │ ├── AutomaticCheaterDetection2.java │ ├── CountingWeekendDays.java │ ├── NumberedCards.java │ ├── Owllen.java │ ├── SumOfMSLCM.java │ ├── TollManagement.java │ ├── UniqueParty.java │ └── editorial.md ├── dhaka2015_preliminary │ ├── BackToThePast.java │ ├── BrainFry.java │ ├── Emoticons.java │ ├── GeekPowerInc.java │ ├── MarblesInJars1.java │ ├── MarblesInJars2.java │ ├── SearchTheKhoj.java │ ├── XORSubset.java │ └── editorial.md ├── latinAmerica2013 │ ├── BloggerLanguage.java │ ├── CountingOnes.java │ ├── DijointWaterSupply.java │ ├── Eleven.java │ ├── Football.java │ ├── GoUpTheUltras.java │ ├── HideAndSeek.java │ ├── InvertingHuffman.java │ └── JoinTwoKingdoms.java ├── latinAmerica2015 │ ├── AtMostTwice.java │ ├── BloodGroups.java │ ├── DAsinDaedalus.java │ ├── ExposingCorruption.java │ ├── FenceTheVegetablesFail.java │ ├── GalacticTaxes.java │ ├── HeightMap.java │ ├── IdentifyingTea.java │ └── editorial.md └── swerc2015 │ ├── CanvasPainting.java │ ├── Promotions.java │ └── editorial.md ├── v001 ├── AccordianPatience_UVa127.java ├── AllSquares_UVa155.java ├── Anagram_UVa195.java ├── Ananagrams_UVa156.java ├── Arbitrage_UVa104.java ├── Bandwidth_UVa140.java ├── BeggarMyNeighbour_UVa162.java ├── BitMaps_UVa183.java ├── BlocksProblem_UVa101.java ├── CircleThroughThreePoints_UVa190.java ├── ClimbingTrees_UVa115.java ├── Dollars_UVa147.java ├── FactorsAndFactorials_UVa160.java ├── FermatVsPythagoras_UVa106.java ├── FollowingOrders_UVa124.java ├── GraphColoring_UVa193.java ├── IDCodes_UVa142.java ├── Intersection_UVa191.java ├── KryptonFactor_UVa129.java ├── LaserLines_UVa184.java ├── MakingChange_UVa166.java ├── MouseClicks_UVa142.java ├── MutantFlatworldExplorers_UVa118.java ├── NumberingPaths_UVa125.java ├── OrchardTrees_UVa143.java ├── PerfectHash_UVa188.java ├── PipeFitters_UVa121.java ├── Polygons_UVa137.java ├── PowerCrisis_UVa151.java ├── PowerOfCryptography_UVa113.java ├── Recycling_UVa154.java ├── SCUDBusters_UVa109.java ├── SearchingQuickly_UVa123.java ├── SoftwareCRC_UVa128.java ├── SpreadSheet_UVa196.java ├── StackingBoxes_UVa103.java ├── StacksOfFlapjacks_UVa120.java ├── Stamps_UVa165.java ├── StreetNumbers_UVa138.java ├── StringComputer_UVa164.java ├── TheCatInTheHat_UVa107.java ├── TheDoleQueue_UVa133.java ├── ThePostalWorkerRingsOnce_UVa117.java ├── ThePsychicPokerPlayer_UVa131.java ├── TheSkylineProblem_UVa105.java ├── TheSultanSuccessors_UVa167.java ├── TheseusAndTheMinotaur_UVa168.java ├── TransactionProcessing_UVa187.java ├── TreeSumming_UVa112.java ├── TreesCrowd_UVa152.java ├── TreesOnTheLevel_UVa122.java ├── TripRouting_UVa186.java └── UnidirectionalTSP_UVa116.java ├── v002 ├── Borrowers_UVa230.java ├── BudgetTravel_UVa222.java ├── CallingCircles_UVa247.java ├── CatAndMouse_UVa274.java ├── Chess_UVa278.java ├── CodeGeneration_UVa214.java ├── CountOnCantor_UVa264.java ├── CubePainting_UVa253.java ├── Divisors_UVa294.java ├── Fatman_UVa295.java ├── Firetruck_UVa208.java ├── GettingInLine_UVa216.java ├── IlGiocodellX_UVa260.java ├── JackStraws_UVa273.java ├── LiningUp_UVa270.java ├── MothEradication_UVa218.java ├── Palinwords_UVa257.java ├── QuadTrees_UVa297.java ├── QuirksomeSquares_UVa256.java ├── RareOrder_UVa200.java ├── RepeatingDecimals_UVa202.java ├── Safebreaker_UVa296.java ├── SoftwareAllocation_UVa259.java ├── Squares_UVa201.java ├── SwitchingChannels_UVa234.java ├── TEXQuotes_UVa272.java ├── TestingCatcher_UVa231.java ├── TheHouseOfSantaClaus_UVa291.java └── Vertex_UVa280.java ├── v003 ├── ANodeTooFar_UVa336.java ├── BasicallySpeaking_UVa389.java ├── BigMod_UVa374.java ├── CallForwarding_UVa380.java ├── Combinations_UVa369.java ├── CopsAndRobbers_UVa361.java ├── DominoEffect_UVa318.java ├── ExtrapolationUsingADifferenceTable_UVa326.java ├── FactorialFrequencies_UVa324.java ├── GalacticImport_UVa388.java ├── IndentifyingConcurrentEvents_UVa334.java ├── InscribedCirclesAndIsoscelesTriangle_UVa375.java ├── IntersectingLines_UVa378.java ├── Joseph_UVa305.java ├── LetMeCountTheWays_UVa357.java ├── MapMaker_UVa394.java ├── MappingTheSwaps_UVa331.java ├── MasterMindHints_UVa340.java ├── MayaCalendar_UVa300.java ├── Network_UVa315.java ├── NonStopTravel_UVa341.java ├── OptimalArrayMultiplication_UVa348.java ├── Packets_UVa311.java ├── PerfectCubes_UVa386.java ├── PeskyPalindromes_UVa353.java ├── PolynomialShowdown_UVa392.java ├── PseudoRandomNumbers_UVa350.java ├── RationalNumbersFromRepeatingFractions_UVa332.java ├── Robot_UVa314.java ├── RomanDigititis_UVa344.java ├── Run_UVa347.java ├── ShippingRoutes_UVa383.java ├── SquarePegsAndRoundHoles_UVa356.java ├── TheNewVilla_UVa321.java ├── TheSeasonalWar_UVa352.java └── Transportation_UVa301.java ├── v004 ├── ACORN_UVa4106.java ├── ArbitrageII_UVa436.java ├── BankNotQuiteOCR_UVa433.java ├── BlockVoting_UVa435.java ├── BoggleBlitz_UVa487.java ├── BridgeHandEvaluator_UVa462.java ├── EenyMeenyMoo_UVa440.java ├── EncoderAndDecoder_UVa444.java ├── ExcusesExcuses_UVa409.java ├── FibonacciFreeze_UVa495.java ├── GraphConnectivity_UVa459.java ├── HangmanJudge_UVa489.java ├── HumbleNumbers_UVa443.java ├── IntegerInquiry_UVa424.java ├── KibblesnBitsnBitsnBits_UVa446.java ├── KnightMoves_UVa439.java ├── LEDTest_UVa416.java ├── LittleBlackBlock_UVa450.java ├── MPIMaelstrom_UVa423.java ├── MachinedSurfaces_UVa414.java ├── MarevlousMazes_UVa490.java ├── MatrixChainMultiplication_UVa442.java ├── MattyBlocks_UVa434.java ├── MirrorMirror_UVa466.java ├── Overflow_UVa465.java ├── OverlappingRectangles_UVa460.java ├── PI_UVa412.java ├── Palindromes_UVa401.java ├── PascalsTriangleOfDeath_UVa485.java ├── PeriodicStrings_UVa455.java ├── PermutationArrays_UVa482.java ├── PigLatin_UVa492.java ├── PointsInFigure_UVa477.java ├── PointsInFigure_UVa478.java ├── PrimeCuts_UVa406.java ├── ProjectScheduling_UVa452.java ├── RationalSpiral_UVa493.java ├── RaucousRockers_UVa473.java ├── RotatingSentences_UVa490.java ├── SimplySubsets_UVa496.java ├── StationBalance_UVa410.java ├── StrategicDefenseInitiative_UVa497.java ├── SynchingSignals_UVa467.java ├── TheCircumferenceOfTheCircle_UVa438.java ├── TheDecoder_UVa458.java ├── TheDepartmentOfRedundancyDepartment_UVa484.java ├── TheTowerOfBabylon_UVa437.java ├── TriangleWave_UVa488.java ├── UniformGenerator_UVa408.java ├── UnixIs_UVa400.java ├── WetLandsOfFlorida_UVa469.java ├── WhatGoesUp_UVa481.java ├── WhatsTheFrequencyKenneth_UVa499.java ├── WordIndex_UVa417.java ├── WordSearchWonder_UVa422.java └── WordTransformation_UVa429.java ├── v005 ├── AlwaysOnTheRun_UVa590.java ├── ArtificialIntelligence_UVa537.java ├── BinomialShutdown_UVa530.java ├── BlackBox_UVa501.java ├── BoxOfBricks_UVa591.java ├── BridgeHands_UVa555.java ├── BundlingNewspapers_UVa598.java ├── CaesarCypher_UVa554.java ├── ClockHands_UVa579.java ├── CriticalMass_UVa580.java ├── DDF_UVa547.java ├── DividingCoins_UVa562.java ├── DungeonMaster_UVa532.java ├── ErrorCorrection_UVa541.java ├── France98_UVa542.java ├── Frogger_UVa534.java ├── GoldbachConjecture_UVa543.java ├── Gossiping_UVa521.java ├── HeavyCargo_UVa544.java ├── JillRidesAgain_UVa507.java ├── Jugs_UVa571.java ├── JustTheFacts_UVa568.java ├── MultiplyingByRotation_UVa550.java ├── NestingABunchOfBrackets_UVa551.java ├── OilDeposits_UVa572.java ├── OneLittleTwoLittleThreeLittleEndians_UVa594.java ├── PizzaAnyone_UVa565.java ├── PrimeFactors_UVa583.java ├── PrimeLand_UVa516.java ├── PrimeRingProblem_UVa524.java ├── Rails_UVa514.java ├── Risk_UVa567.java ├── SkewBinary_UVa575.java ├── StringDistanceAndTransformProcess_UVa526.java ├── SumItUp_UVa574.java ├── TeamQueue_UVa540.java ├── TheForrestForTheTrees_UVa599.java ├── TheIncredibleHull_UVa596.java ├── TheSettlersOfCatan_UVa539.java ├── TheSnail_UVa573.java ├── TheresTreasureEverywhere_UVa587.java ├── TreeRecovery_UVa536.java ├── Tree_UVa548.java └── Wormholes_UVa558.java ├── v006 ├── AllWalks_UVa677.java ├── BlowingFuses_UVa661.java ├── BookletPrinting_UVa637.java ├── CD_UVa624.java ├── CoconutsRevisited_UVa616.java ├── CoinChange_UVa674.java ├── ConvexHullFinding_UVa681.java ├── DNASorting_UVa612.java ├── DontGetRooked_UVa639.java ├── DroppingBalls_UVa679.java ├── Ecosystem_UVa626.java ├── Eight_UVa652.java ├── FalseCoin_UVa665.java ├── FindingRectangles_UVa638.java ├── GoldbachConjectureII_UVa686.java ├── HowManyKnights_UVa696.java ├── ImmediateDecodability_UVa644.java ├── IsItATree_UVa615.java ├── ItIsNotABugItIsAFeature_UVa658.java ├── JackAndJill_UVa697.java ├── MappingTheRoute_UVa614.java ├── MobilePhoneCoverage_UVa688.java ├── NonstopTravel_UVa617.java ├── P500_UVa623.java ├── ParenthesesBalance_UVa673.java ├── Parliament_UVa668.java ├── Passwords_UVa628.java ├── Polygon_UVa634.java ├── SchedulingLectures_UVa607.java ├── SelfNumbers_UVa640.java ├── SortingSlides_UVa663.java ├── StreetDirections_UVa610.java ├── TheBoggleGame_UVa604.java ├── TheCollatzSequence_UVa694.java ├── TheDieisCast_UVa657.java ├── TheDogTask_UVa670.java ├── TheFallingLeaves_UVa699.java ├── TheNet_UVa627.java └── WordAmalgamation_UVa642.java ├── v007 ├── APlugForUNIX_UVa753.java ├── AddingReversedNumbers_UVa713.java ├── AnagramsByStack_UVa732.java ├── Biorhythms_UVa756.java ├── ContourPainting_UVa782.java ├── CopyingBooks_UVa714.java ├── CriticalLinks_UVa796.java ├── DNASequencing_UVa760.java ├── Dart_a_Mania_Uva735.java ├── DateBugs_UVa700.java ├── DividingUp_UVa711.java ├── Division_UVa725.java ├── Equation_UVa727.java ├── FibinaryNumbers_UVa763.java ├── GlassBeads_UVa719.java ├── GleamingTheCubes_UVa737.java ├── GridColouring_UVa785.java ├── HamiltonianCycle_UVa775.java ├── HeadJudgeHeadache_UVa790.java ├── LCDisplay_UVa706.java ├── Lakes_UVa722.java ├── LostInSpace_UVa736.java ├── MaximumSubsequenceProduct_UVa787.java ├── MazeExploration_UVa784.java ├── MonkeysInARegularForest_UVa776.java ├── NetworkConnections_UVa793.java ├── P4873279_UVa755.java ├── QueensChessProblem_UVa750.java ├── TheGame_UVa710.java ├── TheHammingDistanceProblem_UVa729.java ├── TheReturnOfTheRomanEmpire_UVa759.java ├── TheSameGame_UVa758.java ├── TheVindictiveCoach_UVa702.java ├── TripleTiesTheOrganizersNightmare_UVa703.java ├── WeShipCheap_UVa762.java └── sTrees_UVa712.java ├── v008 ├── AMultiplicationGame_UVa847.java ├── AirlineComparison_UVa869.java ├── AnagramaticPrimes_UVa897.java ├── BerryPicking_UVa858.java ├── ChineseCheckers_UVa859.java ├── CoastTracker_UVa824.java ├── CountingCellsInABlob_UVa871.java ├── DecidingVictoryInGo_UVa852.java ├── EntropyTextAnalyzer_UVa860.java ├── FactorialFactors_UVa884.java ├── Flooded_UVa815.java ├── InternetBandwidth_UVa820.java ├── LargestSubmatrix_UVa836.java ├── LightAndTransparencies_UVa837.java ├── LittleBishops_UVa861.java ├── LunchInGridCity_UVa855.java ├── NamedExtensionDialing_UVa886.java ├── NotSoMobile_UVa839.java ├── NumericalMaze_UVa868.java ├── Ordering_UVa872.java ├── PageHopping_UVa821.java ├── Steps_UVa846.java ├── TheFortifiedForest_UVa811.java ├── TheMailboxManufacturersProblem_UVa882.java ├── TradeOnVerweggistan_UVa812.java ├── WalkingOnTheSafeSide_UVa825.java └── WaterFalls_UVa833.java ├── v009 ├── BrickWallPatterns_UVa900.java ├── BridgeBuilding_UVa976.java ├── Circular_UVa967.java ├── DivingForGold_UVa990.java ├── EternalTruths_UVa928.java ├── FibonnaccimalBase_UVa948.java ├── Genes_UVa939.java ├── Getaway_UVa949.java ├── HowMany_UVa986.java ├── JoanaandtheOddNumbers_UVa913.java ├── JumpingChampion_UVa914.java ├── LemmingsBattle_UVa978.java ├── LocalizedSummingForBlurring_UVa983.java ├── ManyPathsOneDestination_UVa988.java ├── MultinomialCoefficients_UVa911.java ├── NoMorePrerequisitiesPlease_UVa925.java ├── NumberMaze_UVa929.java ├── Permutations_UVa941.java ├── Popes_UVa957.java ├── ProductsOfDigits_UVa993.java ├── ReconnectingComputerSites_UVa908.java ├── RoundAndRoundMaze_UVa985.java ├── SafeSalutations_UVa991.java ├── SpreadingTheNews_UVa924.java ├── Sudoko_UVa989.java ├── SunnyMountains_UVa920.java ├── TVGame_UVa910.java ├── WalkingAroundWisely_UVa926.java └── WenterimBackpackingTrip_UVa907.java ├── v010 ├── ACarefulApproach_UVa1079.java ├── BitCompressor_UVa1052.java ├── Castles_UVa1093.java ├── Containers_UVa1062.java ├── DegreesOfSeparation_UVa1056.java ├── Network_UVa1064.java ├── SharingChocolate_UVa1099.java ├── SimplifiedGSMNetwork_UVa1039.java ├── TheIslands_UVa1096.java ├── TheTravelingJudgesProblem_UVa1040.java ├── TrackingBiobots_UVa1092.java └── Zones_UVa1047.java ├── v011 ├── AirRaid_UVa1184.java ├── AncientMessages_UVa1103.java ├── CallingExtraterrestrial_UVa1195.java ├── CreditCheck_UVa1173.java ├── DynamicFrog_UVa1157.java ├── IPTV_UVa1174.java ├── MachineScheduling_UVa1194.java ├── MiceAndMaze_UVa1112.java ├── RadarInstallation_UVa1193.java ├── Subsequence_UVa1121.java ├── TheBridgesOfKolsberg_UVa1172.java ├── TheGeodeticSetProblem_UVa1198.java ├── TheMysertiousXNetwork_UVa1148.java ├── TheSuspects_UVa1197.java ├── TilingUpBlocks_UVa1196.java ├── TripleFreeBinaryString_UVa11127.java └── XPlosives_UVa1160.java ├── v012 ├── AGTC_UVa1207.java ├── AgainstMammoths_UVa1221.java ├── AntiBruteForceLock_UVa1235.java ├── Argus_UVa1203.java ├── AtomicCarRace_UVa1211.java ├── BoundaryPoints_UVa1206.java ├── BribingFIPA_UVa1222.java ├── CurvyLittleBottles_UVa1280.java ├── Editor_UVa1223.java ├── Euclid_UVa1249.java ├── FindTerrorists_UVa1246.java ├── FindingNemo_UVa1202.java ├── FreeParentheses_UVa1238.java ├── GuardianOfDecency_UVa12083.java ├── ICPCTeamStrategy_UVa1240.java ├── InfectedLand_UVa1253.java ├── InterstarTransport_UVa1247.java ├── JollyBeeTournament_UVa1241.java ├── MODEX_UVa1230.java ├── MagicSquare_UVa1266.java ├── Mines_UVa1263.java ├── NowhereMoney_UVa1258.java ├── Oreon_UVa1208.java ├── PalindromicPaths_UVa1244.java ├── PartyAtHaliBula_UVa1220.java ├── Password_UVa1262.java ├── PolynomialTimeReductions_UVa1243.java ├── Racing_UVa1234.java ├── RectangleByTheOcean_UVa1216.java ├── RepeatedSubstitutionWithSed_UVa1251.java ├── RobotChallenge_UVa1250.java ├── SKYLINE_UVa1232.java ├── StringPopping_UVa1261.java ├── Subdictionary_UVa1229.java ├── SumOfDifferentPrimes_UVa1213.java ├── TaxiCabScheme_UVa1201.java ├── TheBugSensorProblem_UVa1216.java ├── TileCode_UVa1224.java ├── Top10_UVa1254.java ├── TwentyQuestions_UVa1252.java ├── USHER_UVa1233.java └── Wordfish_UVa1209.java ├── v013 ├── Graveryard_UVa1388.java └── Tour_UVa1347.java ├── v015 └── Score_UVa1585.java ├── v016 └── BuildingForUN_UVa1605.java ├── v017 └── Nim2D_UVa10707.java ├── v100 ├── AHoleToCatchAMan_UVa10060.java ├── AMidSummernightsDream_UVa10057.java ├── Airlines_UVa10075.java ├── AllRoadsLeadToRome_UVa10009.java ├── Audiophoboa_UVa10048.java ├── Bicoloring_UVa10004.java ├── Bridge_UVa10037.java ├── CarmichaelNumbers_UVa10006.java ├── CenterOfMasses_UVa10002.java ├── CountTheTrees_UVa10007.java ├── CuttingSticks_UVa10003.java ├── DistinctSubsequences_UVa10069.java ├── Divisibility_UVa10036.java ├── EditStepLadders_UVa10029.java ├── ErdosNumbers_UVa10044.java ├── FlipFlopSquarelotron_UVa10016.java ├── Freckles_UVa10034.java ├── FunnyEncryptionMethod_UVa10019.java ├── GardenOfEden_UVa10001.java ├── GopherII_UVa10080.java ├── Hartals_UVa10050.java ├── HowBigIsIt_UVa10012.java ├── HowManyZerosAndHowManyDigits_UVa10061.java ├── Interpreter_UVa10033.java ├── JollyJumpers_UVa10038.java ├── KnuthsPermutation_UVa10063.java ├── LeapYearOrNotLeapYearAnd_UVa10070.java ├── LongestPaths_UVa10000.java ├── Marbles_UVa10090.java ├── MinimalCoverage_UVa10020.java ├── PackingPolygons_UVa10005.java ├── PizzaCutting_UVa10079.java ├── PlaceTheGuards_UVa10094.java ├── PlayingWithWheelsUVa10067.java ├── ReverseAndAdd_UVa10018.java ├── ShoemakersProblem_UVa10026.java ├── SimpleCalculations_UVa10014.java ├── SuperLongSums_UVa10013.java ├── TakeTheLand_UVa10074.java ├── Tellmethefrequencies_UVa10062.java ├── TestTheRods_UVa10086.java ├── TheColorGame_UVa10097.java ├── TheMonocycle_UVa10047.java ├── TheNecklace_UVa10054.java ├── TheNeverEndingTowersOfHanoi_UVa10017.java ├── TheProblemWithTheProblemSetter_UVa10092.java ├── TheSternBrocotNumberSystem_UVa10077.java ├── TheTouristGuide_UVa10099.java ├── TightWords_UVa10081.java ├── TowerOfCubes_UVa10051.java ├── TugOfWar_UVa10032.java ├── UselessTilePackers_UVa10065.java ├── VitoFamily_UVa10041.java ├── WERTYU_UVa10082.java ├── WhatIsTheProbability_UVa10056.java ├── WhatsCryptanalysis_UVa10008.java ├── WheresWaldorf_UVa10010.java └── kProblem_UVa10025.java ├── v101 ├── AllYouNeedIsLove_UVa10193.java ├── AntOnAChessBoard_UVa10161.java ├── AustralianVoting_UVa10142.java ├── BirthdayCake_UVa10167.java ├── CDVII_UVa10138.java ├── CheckTheCheck_UVa10196.java ├── ChocolateChipCookies_UVa10136.java ├── DistancePrimes_UVa10140.java ├── Doublets_UVa10150.java ├── EuclidProblem_UVa10104.java ├── ExchangeRates_UVa10113.java ├── Factovisiors_UVa10139.java ├── FileFragmentation_UVa10132.java ├── FindTheWinningMove_UVa10111.java ├── FootballAkaSoccer_UVa10194.java ├── FromDuskTillDawn_UVa10187.java ├── Highways_UVa10147.java ├── HowManyFibs_UVa10183.java ├── IrreducableBasicFractions_UVa10179.java ├── IsBiggerSmarter_UVa10131.java ├── LightMoreLight_UVa10110.java ├── LongestMatch_UVa10100.java ├── MeetingProfMiguel_UVa10171.java ├── MyacmTriangles_UVa10112.java ├── NumberGame_UVa10164.java ├── OceanDeepMakeItShallow_UVa10176.java ├── Ones_UVa10127.java ├── P234DSqrRectsCubesBoxes_UVa10177.java ├── PlayOnWords_UVa10129.java ├── PolynomialCoefficients_UVa10105.java ├── Product_UVa10106.java ├── Queue_UVa10128.java ├── RequestForProtocol_UVa10141.java ├── RobotMotion_UVa10116.java ├── RopeCrisisInRopeland_UVa10180.java ├── ShellSort_UVa10152.java ├── StoneGame_UVa10165.java ├── StorageKeepers_UVa10163.java ├── SummationOfFourPrimes_UVa10168.java ├── Sumsets_UVa10125.java ├── SuperSale_UVa10130.java ├── TheHotelwithInfiniteRooms_UVa10170.java ├── TheKnightsOfTheRoundTable_UVa10195.java ├── TheLonesomeCargoDistributor_UVa10172.java ├── TheTrip_UVa10137.java ├── TouristGuide_UVa10199.java ├── Travel_UVa10166.java ├── War_UVa10158.java ├── WeightsAndMeasures_UVa10154.java ├── WhatIsTheMedian_UVa10107.java └── Yahtzee_UVa10149.java ├── v102 ├── AdventuresInMoving_UVa10201.java ├── Babelfish_UVa10282.java ├── Beavergnaw_UVa10297.java ├── Chopsticks_UVa10271.java ├── CommonPermutation_UVa10252.java ├── ContestScoreboard_UVa10258.java ├── DecodetheMadman_UVa10222.java ├── DickAndJane_UVa10257.java ├── FerryLoading_UVa10261.java ├── FindTheWays_UVa10219.java ├── FireStation_UVa10278.java ├── Forests_UVa10227.java ├── FourthPoint_UVa10242.java ├── HanoiTowerTroublesAgain_UVa10276.java ├── HardwoodSpecies_UVa10226.java ├── HayPoints_UVa10295.java ├── HippityHopscotch_UVa10259.java ├── HowManyNodes_UVa10223.java ├── ILoveBigNumbers_UVa10220.java ├── IsThisIntegration_UVa10209.java ├── LongestRunOnASnowboard_UVa10285.java ├── PowerStrings_UVa10298.java ├── PrimeTime_UVa10200.java ├── Railway_UVa10263.java ├── Relatives_UVa10299.java ├── RomioAndJuliet_UVa10210.java ├── Satellites_UVa10221.java ├── SimplyEmirp_UVa10235.java ├── SnowClearing_UVa10203.java ├── Soundex_UVa10260.java ├── StackThemUp_UVa10205.java ├── TheClosestPairProblem_UVa10245.java ├── TheGrandDinner_UVa10249.java ├── TheKissingCircles_UVa10283.java ├── TheLastNonZeroDigit_UVa10212.java ├── TheMostPotentCorner_UVa10264.java ├── TheOtherTwoTrees_UVa10250.java └── TroubleWithAPentagon_UVa10286.java ├── v103 ├── AllInAll_UVa10340.java ├── ArcticNetwork_UVa10369.java ├── AtennaPlacement_UVa10349.java ├── AutomaticPoetry_UVa10361.java ├── AvoidingYourBoss_UVa10354.java ├── Billiard_UVa10387.java ├── Blocks_UVa10365.java ├── ChooseAndDivide_UVa10375.java ├── ConnectTheCampus_UVa10397.java ├── DogAndGopher_UVa10310.java ├── Election_UVa10374.java ├── EuclidsGame_UVa10368.java ├── ExpressionBracketing_UVa10312.java ├── FactorialYouMustBeKidding_UVa10323.java ├── FactoringLargeNumbers_UVa10392.java ├── FlightPlanner_UVa10337.java ├── FlipSort_UVa10327.java ├── GoldbachAndEuler_UVa10311.java ├── LeapsTallBuildingsInASingleBound_UVa10372.java ├── LiftLessEME_UVa10350.java ├── Manhattan_UVa10319.java ├── MazeTraversal_UVa10377.java ├── Medians_UVa10347.java ├── MischievousChildren_UVa10338.java ├── OptimalBinarySearchTree_UVa10304.java ├── OrderingTasks_UVa10305.java ├── PayThePrice_UVa10313.java ├── Playball_UVa10357.java ├── PokerHands_UVa10315.java ├── PolygonInsideACircle_UVa10342.java ├── PowerTransmission_UVa10330.java ├── RankTheLanguages_UVa10336.java ├── RatAttack_UVa10360.java ├── RayThroughGlasses_UVa10334.java ├── RingsAndGlue_UVa10301.java ├── RoadsInTheNorth_UVa10308.java ├── RoughRoads_UVa10356.java ├── SecurityPanel_UVa10318.java ├── SolveIt_UVa10341.java ├── Square_UVa10364.java ├── Subway_UVa10389.java ├── SummationofPolynomials_UVa10302.java ├── TheLottery_UVa10325.java ├── TheOneHandedTypist_UVa10393.java ├── Tiling_UVa10359.java ├── TimeZones_UVa10371.java ├── TurnTheLightsOff_UVa10309.java ├── TwinPrimes_UVa10394.java ├── WateringGrass_UVa10382.java ├── ZerosAndOnes_UVa10324.java ├── eCoins_UVa10306.java └── killingAliensInBorgMaze_UVa10307.java ├── v104 ├── AncientVillageSports_UVa10451.java ├── BachetsGame_UVa10404.java ├── BoxesOfChocolates_UVa10489.java ├── CollectingBeepers_UVa10496.java ├── CowsAndCars_UVa10491.java ├── CuttingTabletops_UVa10406.java ├── DieGame_UVa10409.java ├── DivisibilityOfFactors_UVa10484.java ├── FerryLoadingII_UVa10440.java ├── FindThePermutedString_UVa10460.java ├── GameShowMath_UVa10400.java ├── HelpTheLeaders_UVa10475.java ├── HomerSimpson_UVa10465.java ├── HowFar_UVa10466.java ├── IfWeWereAChildAgain_UVa10494.java ├── InjuredQueenProblem_UVa10401.java ├── IsThereASecondWayLeft_UVa10462.java ├── KnightsInFEN_UVa10422.java ├── ListofConquests_UVa10420.java ├── LongestCommonSubsequence_UVa10405.java ├── LoveCalculator_UVa10424.java ├── MakePalindrome_UVa10453.java ├── Marcus_UVa10452.java ├── MrAzadAndHisSon_UVa10490.java ├── NaughtySleepyBoys_UVa10427.java ├── RockScissorsPaper_UVa10443.java ├── Sabotage_UVa10480.java ├── SimpleBaseConversion_UVa10473.java ├── SimpleDivision_UVa10407.java ├── SumupThePrimes_UVa10419.java ├── SweetChildMakesTrouble_UVa10497.java ├── TheCandymanCan_UVa10482.java ├── TheLandOfJustice_UVa10499.java ├── TheMarriageInterview_UVa10466.java ├── TheSumEqualsTheProduct_UVa10483.java ├── TheTreeRoot_UVa10459.java ├── WhatIsTheMarble_UVa10474.java └── WorldCupNoise_UVa10450.java ├── v105 ├── ASCIILabyrinth_UVa10582.java ├── AlmostPrimeNumbers_UVa10539.java ├── BoundingBox_UVa10577.java ├── CaloriesFromFat_UVa10554.java ├── CenterOfSymmetry_UVa10585.java ├── CombinationOnceAgain_UVa10532.java ├── Councilling_UVa10511.java ├── CountingRectangles_UVa10502.java ├── CrossedLadders_UVa10566.java ├── DataFlow_UVa10594.java ├── DetermineIt_UVa10520.java ├── DigitPrimes_UVa10533.java ├── FibonacciNumbers_UVa10579.java ├── GameOfEuler_UVa10536.java ├── GeometryParadox_UVa10573.java ├── GuessingGame_UVa10530.java ├── HeightToArea_UVa10522.java ├── HelpingFillBates_UVa10567.java ├── MontescoVsCapuleto_UVa10505.java ├── MorningWalk_UVa10596.java ├── NewToBangladesh_UVa10525.java ├── PersistentNumbers_UVa10527.java ├── ReallyStrange_UVa10519.java ├── Stripe_UVa10541.java ├── TheDominoesSolitaire_UVa10503.java ├── TheGameOf31_UVa10578.java ├── TheOuroborosProblem_UVa10506.java ├── TravelingPolitician_UVa10543.java ├── UbiquitousReligions_UVa10583.java ├── WakingUpBrain_UVa10507.java ├── WavioSequence_UVa10534.java ├── XYZZY_UVa10557.java └── Y2KAccountingBug_UVa10576.java ├── v106 ├── ACMContestAndBlackout_UVa10600.java ├── BombsNoTheyAreMines_UVa10653.java ├── BroadWrapping_UVa10652.java ├── BuyingCoke_UVa10626.java ├── ChemicalReaction_UVa10604.java ├── CitizenAttentionOffices_UVa10660.java ├── ContemplationAlgebra_UVa10655.java ├── Coprimes_UVa10637.java ├── CountTheFactors_UVa10699.java ├── DeterminatePrime_UVa10650.java ├── DivisibleGroupSums_UVa10616.java ├── EditorNottoobad_UVa10602.java ├── ExpandingRods_UVa10668.java ├── ExpressionAgain_UVa10690.java ├── F91_UVa10696.java ├── FacingProblemsWithTrees_UVa10643.java ├── Fill_UVa10603.java ├── FittingTextIntoSlides_UVa10659.java ├── FootballSort_UVa10698.java ├── Friends_UVa10608.java ├── GopherAndHawks_UVa10610.java ├── ILoveStrings_UVa10679.java ├── LCM_UVa10680.java ├── Luggage_UVa10664.java ├── MarblesOnATree_UVa10672.java ├── MaximumSubsequenceProduct_UVa10684.java ├── MaximumSumII_UVa10656.java ├── Menu_UVa10645.java ├── MonitoringTheAmazon_UVa10687.java ├── Nature_UVa10685.java ├── OpeningDoors_UVa10606.java ├── PebbleSolitaire_UVa10651.java ├── PerfectPthPowers_UVa10622.java ├── PlayWithFloorAndCeil_UVa10673.java ├── PrinceAndPrincess_UVa10635.java ├── SQFProblem_UVa10686.java ├── TebaldosTrip_UVa10681.java ├── TheDecadaryWatch_UVa10683.java ├── TheGrazingCow_UVa10678.java ├── TheJackpot_UVa10684.java ├── ThePlayboyChimp_UVa10611.java ├── ThePoorGiant_UVa10688.java ├── TheWedding_UVa10662.java ├── WhatIsTheCard_UVa10646.java ├── WorkReduction_UVa10670.java └── YetAnotherNumberSequence_UVa10689.java ├── v107 ├── AgainPrimeNoTime_UVa10780.java ├── Antiarithmetic_UVa10730.java ├── Ants_UVa10714.java ├── BarCodes_UVa10721.java ├── BitMask_UVa10718.java ├── CamelTrading_UVa10700.java ├── CollectorsProblem_UVa10779.java ├── CrimeWaveTheSequel_UVa10746.java ├── Diagonal_UVa10784.java ├── DovesAndBombs_UVa10765.java ├── ForeignExchange_UVa10763.java ├── FreeSpots_UVa10703.java ├── HowManyPointsOfIntersection_UVa10790.java ├── MinimumSumLCM_UVa10791.java ├── Mint_UVa10717.java ├── NumberSequence_UVa10706.java ├── PreInAndPost_UVa10701.java ├── PrimeFrequency_UVa10789.java ├── RiemannVsMertens_UVa10738.java ├── RoadConstruction_UVa10724.java ├── StringToPalindrome_UVa10739.java ├── SuperLuckyNumbers_UVa10722.java ├── Test_UVa10731.java ├── TheColoredCubes_UVa10733.java ├── TheLaurelHardyStory_UVa10792.java ├── TheMadNumerologist_UVa10785.java ├── TheNewRuleOfEuphomia_UVa10742.java ├── TheOrcAttack_UVa10793.java ├── TravellingSalesman_UVa10702.java └── TrianglePartitioning_UVa10734.java ├── v108 ├── AndysFirstDictionary_UVa10815.java ├── BeatTheSpread_UVa10812.java ├── BrowniePoints_UVa10865.java ├── CockroachEscapeNetworks_UVa10805.java ├── ColinAndRyan_UVa10880.java ├── ComboDeal_UVa10898.java ├── ConnectTheCableWires_UVa10862.java ├── DecodeTheTape_UVa10878.java ├── DijkstraDijkstra_UVa10806.java ├── FactoryRobot_UVa10876.java ├── GameOfSum_UVa10891.java ├── GopherStrategy_UVa10804.java ├── HeadmastersHeadache_UVa10817.java ├── LCMCardinality_UVa10892.java ├── LessPrime_UVa10852.java ├── LiftHopping_UVa10801.java ├── MatrixTranspose_UVa10895.java ├── MaximumSumOnATorus_UVa10827.java ├── Maze_UVa10890.java ├── MoveTheBishop_UVa10849.java ├── OfCirclesAndSquares_UVa10823.java ├── PrimedSubsequence_UVa10871.java ├── RecoverFactorial_UVa10856.java ├── RotatedSquare_UVa10855.java ├── Segments_UVa10874.java ├── SendATable_UVa10820.java ├── ThunderMountain_UVa10803.java ├── TrafficFlow_UVa10842.java ├── TravelInDesert_UVa10816.java ├── Trouble13Dots_UVa10819.java ├── UniqueFactorization_UVa10858.java ├── Warehouse_UVa10888.java └── Yoyodyne_UVa10832.java ├── v109 ├── AddAll_UVa10954.java ├── AnotherNewFunction_UVa10990.java ├── BadCode_UVa10950.java ├── BearWithMeAgain_UVa10947.java ├── BigChocolate_UVa10970.java ├── BlackbeardThePirate_UVa10937.java ├── BrightLights_UVa10927.java ├── BuyOneGetTheRestFree_UVa10983.java ├── ChildrensGame_UVa10905.java ├── EnchantedForest_UVa10977.java ├── FerryLoadingIII_UVa10901.java ├── FindtheTelephone_UVa10921.java ├── FleaCircus_UVa10938.java ├── FormingQuizTeams_UVa10911.java ├── HowDoYouAdd_UVa10943.java ├── HowManyDependencies_UVa10926.java ├── HowManySolutions_UVa10958.java ├── LargestSquare_UVa10908.java ├── LetsPlayMagic_UVa10978.java ├── LowestPriceInTown_UVa10980.java ├── MarksDistribution_UVa10910.java ├── MotherBear_UVa10945.java ├── MyDearNeighbours_UVa10928.java ├── NutsForNuts_UVa10944.java ├── P2The9s_UVa10922.java ├── Parity_UVa10931.java ├── PickUpSticks_UVa10902.java ├── Prerequisties_UVa10919.java ├── PrimeWords_UVa10924.java ├── Region_UVa10991.java ├── RockPaperScissorsTournament_UVa10903.java ├── SendingEmail_UVa10986.java ├── SevenSeas_UVa10923.java ├── SimpleMindedHashing_UVa10912.java ├── SoDokuChecker_UVa10957.java ├── SpiralTap_UVa_10920.java ├── TheGreatEscape_UVa10967.java ├── ThePartyPartI_UVa10959.java ├── ThePrimaryProblem_UVa10948.java ├── ThrowingCardsAwayI_UVa10935.java ├── TriTiling_UVa10918.java ├── TriangleCounting_UVa10973.java ├── Troublemakers_UVa10982.java ├── WalkThroughTheForest_UVa10917.java ├── WalkingOnGrid_UVa10913.java ├── YouCanSay11_UVa10929.java └── YouWantWhatFilled_UVa10946.java ├── v110 ├── AGentlemensAgreement_UVa11065.java ├── AGraphProblem_UVa11069.java ├── AGroupingProblem_UVa11026.java ├── AddBricksInTheWall_UVa11040.java ├── AnEasyTask_UVa11068.java ├── AnagramDivision_UVa11084.java ├── AndysSecondDicitionary_UVa11062.java ├── Area_UVa10589.java ├── BackToThe8Queens_UVa11085.java ├── BasicWallMaze_UVa11049.java ├── Bee_UVa11000.java ├── Beverages_UVa11060.java ├── Boxes_UVa11003.java ├── BuildingDesigning_UVa11039.java ├── ComplexDifficultAndComplicated_UVa11042.java ├── CompositePrime_UVa11086.java ├── Continents_UVa11094.java ├── EndUpWithMoreTeams_UVa11088.java ├── ExactSum_UVa11057.java ├── FerryLoadingIV_UVa11034.java ├── FiBinaryNumber_UVa11089.java ├── GoingInCycle_UVa11090.java ├── JustFinishItUp_UVa11093.java ├── LeadingAndTrailing_UVa11029.java ├── LittleRedRidingHood_UVa11067.java ├── MaximumProduct_UVa11059.java ├── MyTshirtsSuitsMe_UVa11045.java ├── Nails_UVa11096.java ├── Necklace_UVa11001.java ├── NumberTheory_UVa11064.java ├── OpenCreditSystem_UVa11078.java ├── PlaceTheGuards_UVa11080.java ├── Rendezvous_UVa11015.java ├── StringFactoring_UVa11022.java ├── TheScroogeCoProblem_UVa11047.java ├── TowardsZero_UVa11002.java └── WineTradingInGergovia_UVa11054.java ├── v111 ├── AnotherNQueenProblem_UVa11195.java ├── ArrangeSomeMarbles_UVa11125.java ├── BeEfficient_UVa11155.java ├── CloseRelatives_UVa11131.java ├── ColourfulFlowers_UVa11152.java ├── DancingDigits_UVa11198.java ├── Eigensequence_UVa11133.java ├── Equidivisions_UVa11110.java ├── FactorsAndMultiples_UVa11159.java ├── GeneralizedMartioshkas_UVa11111.java ├── GreyCodes_UVa11173.java ├── GroupReverse_UVa11192.java ├── HelpMyBrotherII_UVa11161.java ├── HoaxOrWhat_UVa11136.java ├── IngenuousCubrency_UVa11137.java ├── KingdomDivision_UVa11164.java ├── LifeForms_UVa11107.java ├── LongestPalindrome_UVa11151.java ├── MallMania_UVa11101.java ├── MonkeysInTheEmeiMountain_UVa11167.java ├── NutsAndBolts_UVa11138.java ├── SemiHPrimes_UVa11105.java ├── Tautology_UVa11108.java ├── Ternary_UVa11185.java ├── TheTrip2700_UVa11100.java ├── TrashRemoval_UVa11111.java ├── UncleJack_UVa11115.java └── WFFProof_UVa11103.java ├── v112 ├── AnnoyingPaintingTool_UVa11230.java ├── Antimonotonicity_UVa11240.java ├── Census_UVa11297.java ├── CoinCollector_UVa11246.java ├── CoinCollector_UVa11264.java ├── Conformity_UVa11286.java ├── CountingStars_UVa11244.java ├── DragonOfLoowater_UVa11292.java ├── ExchangeRates_UVa11285.java ├── Expressions_UVa11234.java ├── FlyingToFrederiction_UVa11280.java ├── FrequentValues_UVa11235.java ├── GroceryStore_UVa11236.java ├── HowOldAreYou_UVa11219.java ├── KTV_UVa11218.java ├── MagicSquarePalindromes_UVa11221.java ├── MakingQuadrilaterals_UVa11256.java ├── MixingInvitations_UVa11282.java ├── MusicalInstruments_UVa11204.java ├── OneHandedTypist_UVa11278.java ├── OnlyIDidIt_UVa11222.java ├── OpenSource_UVa11239.java ├── PlayingBoggle_UVa11283.java ├── ReachingTheFixPoint_UVa11226.java ├── SettingProblems_UVa11269.java ├── ShoppingTrip_UVa11284.java ├── StringPartition_UVa11258.java ├── TarotScores_UVa11225.java ├── TheBrokenPedometer_UVa11205.java ├── TheEasiestWay_UVa11207.java ├── TheHireACoderBusinessModel_UVa11267.java ├── TheProblemOfTheCrazyLinguist_UVa11201.java ├── TheSilverBullet_UVa11227.java ├── TheSultansProblem_UVa11265.java ├── TilingDominoes_UVa11270.java ├── TransportationSystem_UVa11228.java ├── TriangularPegsInRoundHoles_UVa11281.java └── WeirdFence_UVa11262.java ├── v113 ├── ADifferentKindOfSorting_UVa11353.java ├── AirportSetup_UVa11377.java ├── AlternativeArborescence_UVa11307.java ├── BankruptBaker_UVa11308.java ├── BeyBattle_UVa11378.java ├── BlobsInTheBoard_UVa11391.java ├── ClawDecomposition_UVa11396.java ├── CountingChaos_UVa11309.java ├── CrazyKing_UVa11352.java ├── CuriousFleas_UVa11329.java ├── DeliveryDebacle_UVa11310.java ├── DiscretePursuit_UVa11335.java ├── DownWentTheTitanic_UVa11380.java ├── EnumeratingRationalNumbers_UVa11327.java ├── ExclusivelyEdible_UVa11311.java ├── Exibition_UVa11348.java ├── FullTank_UVa11367.java ├── GCDLCM_UVa11388.java ├── GreatWallOfChina_UVa11301.java ├── HaveFunWithMatrices_UVa11360.java ├── IsolatedSegments_UVa11343.java ├── LaserPointer_UVa11326.java ├── Minefield_UVa11338.java ├── Multifactorials_UVa11347.java ├── NestedDolls_UVa11368.java ├── Newspaper_UVa11340.java ├── NumberTheoryForNewbies_UVa11371.java ├── PhoneList_UVa11362.java ├── Rectangles_UVa11345.java ├── Shopaholic_UVa11369.java ├── SigmaFunction_UVa11395.java ├── SortSortAndSort_UVa11321.java ├── SternBrocotTree_UVa11350.java ├── SymmetricMatrix_UVa11349.java ├── TermStrategy_UVa11341.java ├── TheBusDriverProblem_UVa11389.java ├── TheBusDriverProblem_Uva11389.java ├── TheHugeOne_UVa11344.java ├── TheLargestClique_UVa11324.java └── ThreeSquare_UVa11342.java ├── v114 ├── AgeSort_UVa11462.java ├── AhoyPirates_UVa11402.java ├── ArrangeTheTiles_UVa11471.java ├── Babel_UVa11492.java ├── BeautifulNumbers_UVa11472.java ├── BeholdMyQuadrangle_UVa11455.java ├── BubblesAndBuckets_UVa11495.java ├── BusyProgrammer_UVa11432.java ├── CampusRoads_UVa11473.java ├── CanUWin_UVa11405.java ├── ChestOfDrawers_UVa11420.java ├── CleverNamingPatterns_UVa11418.java ├── Commandos_UVa11463.java ├── CountDeprimes_UVa11408.java ├── CountTheFactorials_UVa11415.java ├── Cubes_UVa11428.java ├── DigTheHoles_UVa11412.java ├── DyingTree_UVa11474.java ├── ErasingAndWinning_UVa11491.java ├── EvenParity_UVa11464.java ├── ExtendToPalindrome_UVa11475.java ├── FillTheContainers_UVa11413.java ├── FindingPathsInGrid_UVa11486.java ├── GCD_UVa11417.java ├── GatheringFood_UVa11487.java ├── InThisTheEasiestProblem_UVa11479.java ├── IntegerGame_UVa11489.java ├── JimmysBalls_UVa11480.java ├── LargestPrimeDivisor_UVa11466.java ├── MusicalLoop_UVa11496.java ├── Queen_UVa11494.java ├── ReservoirLogs_UVa11447.java ├── SAMIAM_UVa11419.java ├── SquareNumbers_UVa11461.java ├── SquareSums_UVa11470.java ├── Squares_UVa11407.java ├── Trainsorting_UVa11456.java ├── TriangleCounting_UVa11401.java ├── TriangleFun_UVa11437.java ├── UltraQuickSort_UVa11495.java └── WeddingShopping_UVa11450.java ├── v115 ├── AngryProgrammers_UVa11506.java ├── AvoidingJungleInTheDark_UVa11545.java ├── BenderBRodriguez_UVa11507.java ├── BlackboardBonanza_UVa11548.java ├── ChessQueen_UVa11538.java ├── CollidingTraffic_UVa11574.java ├── Cranes_UVa11515.java ├── Decoding_UVa11541.java ├── DemandingDilemma_UVa11550.java ├── Dominoes2_UVa11518.java ├── Dominos_UVa11504.java ├── EventPlanning_UVa11559.java ├── ExactChange_UVa11517.java ├── FewestFlops_UVa11552.java ├── FillTheSquare_UVa11520.java ├── GATTACA_UVa11512.java ├── GettingGold_UVa11561.java ├── GridGame_UVa11553.java ├── GridSuccessors_UVa11581.java ├── HaplessHedonism_UVa11554.java ├── ImageCoding_UVa11588.java ├── LetsYumCha_UVa11566.java ├── Logo2_UVa11519.java ├── Logo_UVa11505.java ├── MoliuNumberGenerator_UVa11567.java ├── Permutation_UVa11525.java ├── PrefixLookup_UVa11590.java ├── Puzzle9_UVa11513.java ├── SMSTyping_UVa11530.java ├── SayGoodbyeToTicTacToe_UVa11534.java ├── ScrollingSign_UVa11576.java ├── SimpleAdjacencyMaximization_UVa11532.java ├── SimpleEquations_UVa11565.java ├── SmallestSubArray_UVa11536.java ├── SpanningSubtree_UVa11597.java ├── TrainTracks_UVa11586.java ├── TriangleTrouble_UVa11579.java ├── UniqueSnowflakes_UVa11572.java ├── VirtualFriends_UVa11503.java └── Wifi_UVa11516.java ├── v116 ├── AlarmClock_UVa11677.java ├── AthleticsTrack_UVa11646.java ├── BallotEvaluation_UVa11629.java ├── BestCoalitions_UVa11658.java ├── BurgerTime_UVa11661.java ├── CardsExchange_UVa11678.java ├── ConvexHull_UVa11626.java ├── DarkRoads_UVa11631.java ├── Digits_UVa11687.java ├── FamilyTree_UVa11615.java ├── FewestFlops_UVa11552.java ├── Fire_UVa11624.java ├── FlightPlanning_UVa11695.java ├── GuardTheLand_UVa11639.java ├── HelloWorld_UVa11636.java ├── HotelBooking_UVa11635.java ├── LaserSculpture_UVa11683.java ├── MirrorClock_UVa11650.java ├── MoneyMatters_UVa11690.java ├── NoProblem_UVa11608.java ├── PickUpSticks_UVa11686.java ├── ReversePrime_UVa11610.java ├── RomanNumerals_UVa11616.java ├── SmallFactors_UVa11621.java ├── SpeedyEscape_UVa11693.java ├── SubPrimeUVa11679.java ├── Teams_UVa11609.java └── Waterland_UVa11655.java ├── v117 ├── AbstractNames_UVa11713.java ├── AlternateTask_UVa11728.java ├── AutomateTheGrades_UVa11777.java ├── BrotherArifPleaseFeedUs_UVa11760.java ├── Cantor_UVa11701.java ├── Car_UVa11715.java ├── CommandoWar_UVa11729.java ├── DigitalFortress_UVa11716.java ├── DoomsDay_UVa11774.java ├── ExpensiveSubway_UVa11710.java ├── HeavyCycleEdges_UVa11747.java ├── HorrorDash_UVa11799.java ├── InstantOfBigBang_UVa11721.java ├── JumpingMario_UVa11764.java ├── KrochanskaIsHere_UVa11792.java ├── LightingAway_UVa11770.java ├── Miles2Km_UVa11780.java ├── MurciasSkyline_UVa11790.java ├── NumberTransformation_UVa11730.java ├── OptimalCut_UVa11782.java ├── PoorTradeAdvisor_UVa11749.java ├── SocialConstraints_UVa11742.java ├── SqrtLogSin_UVa11703.java ├── TheSuperPowers_UVa11752.java └── TrustGroups_UVa11709.java ├── v118 ├── AMinimumLandPrice_UVa11824.java ├── Abnormal89s_UVa11888.java ├── AccountBook_UVa11832.java ├── Alaska_UVa11850.java ├── Argentina_UVa11804.java ├── Benefit_UVa11889.java ├── CD_UVa11849.java ├── CheerLeaders_UVa11806.java ├── ComeAndGo_UVa11838.java ├── DetermineTheShape_UVa11800.java ├── DocumentAnalyzer_UVa11860.java ├── DrivingRange_UVa11857.java ├── Egypt_UVa11854.java ├── Elevator_UVa11834.java ├── Formula1_UVa11835.java ├── FroshWeek_UVa11858.java ├── GeniusMJ_UVa11894.java ├── InternalRateOfReturn_UVa11881.java ├── MaximumGCD_UVa11827.java ├── Multipleof17_UVa11879.java ├── NNODN_UVa11876.java ├── RouteChange_UVa11833.java ├── Shopping_UVa11813.java └── StickerCollectorRobot_UVa11831.java ├── v119 ├── AChangeinThermalUnit_UVa11984.java ├── Battleships_UVa11953.java ├── BinomialTheorem_UVa11955.java ├── BoiledEggs_UVa11900.java ├── BrainFuck_UVa11956.java ├── BrokenKeyboard_UVa11988.java ├── Checkers_UVa11957.java ├── DNA_UVa11961.java ├── Dice_UVa11959.java ├── DivisorGame_UVa11960.java ├── DoYourOwnHomework_UVa11917.java ├── Dominator_UVa11902.java ├── DynamicInversion_UVa11990.java ├── HicHacHoe_UVa11967.java ├── ICanGuessTheDataStructure_UVa11995.java ├── KnightInAWarGrid_UVa11906.java ├── LumberjackSequencing_UVa11942.java ├── Multitasking_UVa11926.java ├── SoyaMilk_UVa11909.java ├── SplittingNumbers_UVa11933.java ├── SwitchTheLights_UVa11974.java ├── TeleLoto_UVa11975.java ├── TheLazyLumberjacks_UVa11936.java └── ThroughTheDesert_UVa11935.java ├── v120 ├── AGiftFromTheSetter_UVa12028.java ├── Divisors_UVa12043.java ├── FindSolutions_UVa12005.java ├── GoogleIsFeelingLucky_UVa12015.java ├── HelpTheWinners_UVa12030.java ├── HighestPaidToll_UVa12047.java ├── InviteYourFriends_UVa12070.java ├── JustPruneTheList_UVa12049.java ├── OrderingTshirts_UVa12022.java ├── Potentiometers_UVa12086.java └── TheMonkeyAndTheOiledBamboo_UVa12032.java ├── v121 ├── AlmostShortestPath_UVa12144.java ├── AnotherCrisis_UVa12186.java ├── Brothers_UVa12187.java ├── CatsVsDogs_UVa12168.java ├── ElectricBill_UVa12190.java ├── Grapevine_UVa12192.java ├── GunFight_UVa12159.java ├── MarchOfThePenguins_UVa12125.java ├── PolePosition_UVa12150.java ├── PrimePath_UVa12101.java ├── PrinterQueue_UVa12100.java ├── SwitchBulbs_UVa12135.java ├── TariffPlan_UVa12157.java └── UnlockTheLock_UVa12160.java ├── v122 ├── AntsColony_UVa12238.java ├── BoxGame_UVa12293.java ├── MatchMakingProblem_UVa12210.java ├── OverlappingScenes_UVa12249.java ├── PolyominoComposer_UVa12291.java └── ThatIsYourQueue_UVa12207.java ├── v123 ├── ArmyBuddies_UVa12356.java ├── AsLongAsILearnILive_UVa12376.java ├── BinarySearchTree_UVa12347.java ├── DigitalRoulette_UVa12318.java ├── EdgetownsTrafficJams_UVa12319.java ├── GasStations_12321.java ├── NumPuzzI_UVa12398.java ├── PhilipJFryProblem_UVa12324.java ├── RomanNumerals_UVa12397.java └── WaterGateManagement_UVa12346.java ├── v124 ├── AntsShoppingMall_UVa12498.java ├── Bars_UVa12455.java ├── CarefulTeacher_UVa12460.java ├── EnemyAtTheGates_UVa12428.java ├── ForwardingEmails_UVa12442.java ├── LittleNephew_UVa12463.java ├── PerfectChoir_UVa12485.java ├── Scarecrow_UVa12405.java ├── SecretWord_UVa12467.java ├── ShortStoryCompetition_UVa12482.java ├── StartGrid_UVa12488.java ├── Stones_UVa12469.java └── Zapping_UVa12468.java ├── v125 ├── ASpecialHappyBirthdaySong_UVa12554.java ├── Birthdates_UVa12541.java ├── IntervalProduct_UVa12532.java ├── MemoryOverflow_UVa12583.java ├── MoviePolice_UVa12515.java ├── RobotInstructions_UVa12503.java ├── SloganLearningOfPrincess_UVa12592.java ├── UpdatingADictionary_UVa12504.java └── WeddingOfSultan_UVa12582.java └── v126 ├── Balloon_UVa12647.java └── GoUpTheUltras_UVa12674.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UVa-Solutions 2 | Java solutions for +1,200 UVa problems 3 | -------------------------------------------------------------------------------- /regionals/dhaka2015/CountingWeekendDays.java: -------------------------------------------------------------------------------- 1 | package regionals.dhaka2015; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | import java.util.TreeMap; 6 | import java.util.TreeSet; 7 | 8 | public class CountingWeekendDays { 9 | 10 | public static void main(String[] args) { 11 | 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | TreeSet is31 = new TreeSet(); 16 | is31.add("JAN");is31.add("MAR");is31.add("MAY");is31.add("JUL"); 17 | is31.add("AUG");is31.add("OCT");is31.add("DEC"); 18 | 19 | TreeMap getDay = new TreeMap(); 20 | getDay.put("SAT", 0); getDay.put("SUN", 1); getDay.put("MON", 2); 21 | getDay.put("TUE", 3); getDay.put("WED", 4); getDay.put("THU", 5); 22 | getDay.put("FRI", 6); 23 | 24 | int tc = sc.nextInt(); 25 | while(tc-->0) 26 | { 27 | String month = sc.next(), day = sc.next(); 28 | int end = 30; 29 | if(month.equals("FEB")) 30 | end = 28; 31 | else if(is31.contains(month)) 32 | end = 31; 33 | int d = getDay.get(day), ans = 0; 34 | for(int i = 0; i < end; ++i) 35 | { 36 | if(d == 0 || d == 6) 37 | ++ans; 38 | d = (d + 1)%7; 39 | } 40 | out.println(ans); 41 | } 42 | out.flush(); 43 | out.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /regionals/dhaka2015/Owllen.java: -------------------------------------------------------------------------------- 1 | package regionals.dhaka2015; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.util.StringTokenizer; 9 | 10 | public class Owllen { 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | Scanner sc = new Scanner(System.in); 15 | PrintWriter out = new PrintWriter(System.out); 16 | 17 | int tc = sc.nextInt(); 18 | for(int t = 1; t <= tc; ++t) 19 | { 20 | int[] f = new int[26]; 21 | char[] s = sc.next().toCharArray(); 22 | for(char c: s) 23 | f[c-'a']++; 24 | int ans = s.length; 25 | for(int x: f) 26 | ans = Math.min(ans, x); 27 | out.printf("Case %d: %d\n", t, ans); 28 | } 29 | out.flush(); 30 | out.close(); 31 | } 32 | 33 | static class Scanner 34 | { 35 | BufferedReader br; 36 | StringTokenizer st; 37 | 38 | Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 39 | 40 | String next() throws IOException 41 | { 42 | while(st == null || !st.hasMoreTokens()) 43 | st = new StringTokenizer(br.readLine()); 44 | return st.nextToken(); 45 | } 46 | 47 | int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /regionals/dhaka2015_preliminary/BackToThePast.java: -------------------------------------------------------------------------------- 1 | package regionals.dhaka2015_preliminary; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.util.StringTokenizer; 9 | 10 | public class BackToThePast { 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | Scanner sc = new Scanner(System.in); 15 | PrintWriter out = new PrintWriter(System.out); 16 | 17 | out.println("May 29, 2013 Wednesday"); 18 | 19 | out.flush(); 20 | out.close(); 21 | } 22 | 23 | static class Scanner 24 | { 25 | BufferedReader br; 26 | StringTokenizer st; 27 | 28 | Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 29 | 30 | String next() throws IOException 31 | { 32 | while(st == null || !st.hasMoreTokens()) 33 | st = new StringTokenizer(br.readLine()); 34 | return st.nextToken(); 35 | } 36 | 37 | int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /regionals/dhaka2015_preliminary/MarblesInJars2.java: -------------------------------------------------------------------------------- 1 | package regionals.dhaka2015_preliminary; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.util.Arrays; 9 | import java.util.StringTokenizer; 10 | 11 | public class MarblesInJars2 { 12 | 13 | static final int mod = (int)1e9 + 7; 14 | static int N, M[]; 15 | 16 | public static void main(String[] args) throws NumberFormatException, IOException { 17 | 18 | Scanner sc = new Scanner(System.in); 19 | PrintWriter out = new PrintWriter(System.out); 20 | 21 | int tc = sc.nextInt(); 22 | for(int t = 1; t <= tc; ++t) 23 | { 24 | N = sc.nextInt(); 25 | M = new int[N]; 26 | for(int i = 0; i < N; ++i) 27 | M[i] = sc.nextInt(); 28 | Arrays.sort(M); 29 | long res = 1; 30 | for(int i = 0; i < N; ++i) 31 | res = (res * (M[i] - i)) % mod; 32 | out.printf("Case %d: %d\n", t, res); 33 | } 34 | 35 | out.flush(); 36 | out.close(); 37 | } 38 | 39 | static class Scanner 40 | { 41 | BufferedReader br; 42 | StringTokenizer st; 43 | 44 | Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 45 | 46 | String next() throws IOException 47 | { 48 | while(st == null || !st.hasMoreTokens()) 49 | st = new StringTokenizer(br.readLine()); 50 | return st.nextToken(); 51 | } 52 | 53 | int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /regionals/latinAmerica2015/IdentifyingTea.java: -------------------------------------------------------------------------------- 1 | package regionals.latinAmerica2015; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.util.StringTokenizer; 9 | 10 | public class IdentifyingTea { 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | Scanner sc = new Scanner(System.in); 15 | PrintWriter out = new PrintWriter(System.out); 16 | 17 | while(sc.ready()) 18 | { 19 | int t = sc.nextInt(), ans = 0; 20 | for(int i = 0; i < 5; ++i) 21 | if(sc.nextInt() == t) 22 | ++ans; 23 | out.println(ans); 24 | } 25 | out.flush(); 26 | out.close(); 27 | 28 | } 29 | 30 | static class Scanner 31 | { 32 | BufferedReader br; 33 | StringTokenizer st; 34 | 35 | Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 36 | 37 | String next() throws IOException 38 | { 39 | while(st == null || !st.hasMoreTokens()) 40 | st = new StringTokenizer(br.readLine()); 41 | return st.nextToken(); 42 | } 43 | 44 | int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } 45 | 46 | boolean ready() throws IOException { return br.ready(); } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /v001/Dollars_UVa147.java: -------------------------------------------------------------------------------- 1 | package v001; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class Dollars_UVa147 { 10 | 11 | static long[][] memo; 12 | static final int UNCAL = -1; 13 | static int[] value = new int[]{5,10,20,50,100,200,500,1000,2000,5000,10000}; 14 | 15 | public static long dp(int i, int rem) 16 | { 17 | if(rem < 0) 18 | return 0; 19 | if(rem == 0) 20 | return 1; 21 | if(i==11) 22 | if(rem == 0) 23 | return 1; 24 | else 25 | return 0; 26 | if(memo[i][rem]!=UNCAL) 27 | return memo[i][rem]; 28 | 29 | return memo[i][rem] = dp(i,rem-value[i]) + dp(i+1, rem); 30 | } 31 | 32 | public static void main(String[] args) throws IOException { 33 | 34 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 35 | StringBuilder sb = new StringBuilder(); 36 | 37 | memo = new long[11][30001]; 38 | for(int i = 0; i < 11; i++) 39 | Arrays.fill(memo[i], UNCAL); 40 | dp(0,30000); 41 | while(true) 42 | { 43 | String in = br.readLine(); 44 | StringTokenizer st = new StringTokenizer(in,"."); 45 | int cents = Integer.parseInt(st. nextToken()) * 100 + Integer.parseInt(st.nextToken()); 46 | if(cents==0) 47 | break; 48 | while(in.length() < 6) 49 | in = " " + in; 50 | String out = "" + memo[0][cents]; 51 | while(out.length() < 17) 52 | out = " " + out; 53 | sb.append(in+out+"\n"); 54 | } 55 | System.out.print(sb); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /v001/IDCodes_UVa142.java: -------------------------------------------------------------------------------- 1 | package v001; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | // generate next permutation 8 | 9 | public class IDCodes_UVa142 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | while(true) 17 | { 18 | char[] characters = br.readLine().toCharArray(); 19 | if(characters[0]=='#') 20 | break; 21 | boolean found = false; 22 | for(int i = characters.length - 1; i > 0; i--) 23 | { 24 | int current = characters[i]; 25 | int next = characters[i-1]; 26 | if(next-current >= 0) 27 | continue; 28 | found = true;int k = 1; 29 | 30 | while(characters[characters.length - k]-next<=0) 31 | k++; 32 | char tmp = characters[characters.length - k]; 33 | characters[characters.length - k] = characters[i-1]; 34 | characters[i - 1] = tmp; 35 | //reverse from i to end of the string 36 | int j = characters.length - 1; 37 | while(i0) 37 | { 38 | board = new int[8][8]; 39 | for(int i = 0; i < 8; i++) 40 | for(int j = 0; j < 8; j++) 41 | board[i][j] = sc.nextInt(); 42 | col = new int[8]; 43 | String best = ""+backtrack(0,0); 44 | while(best.length()<5) 45 | best = " "+best; 46 | out.println(best); 47 | 48 | } 49 | out.flush(); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /v002/Chess_UVa278.java: -------------------------------------------------------------------------------- 1 | package v002; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class Chess_UVa278 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int TC = Integer.parseInt(br.readLine()); 15 | while(TC-->0) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | char c = st.nextToken().charAt(0); 19 | int m = Integer.parseInt(st.nextToken()); 20 | int n = Integer.parseInt(st.nextToken()); 21 | int count = 0; 22 | switch(c) 23 | { 24 | case 'k':count = (m*n+1)/2;break; 25 | case 'K':count = ((m+1)/2)*((n+1)/2);break; 26 | default:count = Math.min(m, n); 27 | } 28 | sb.append(count).append("\n"); 29 | } 30 | System.out.print(sb); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /v002/QuirksomeSquares_UVa256.java: -------------------------------------------------------------------------------- 1 | package v002; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.text.DecimalFormat; 7 | 8 | public class QuirksomeSquares_UVa256 { 9 | 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | int[][] array = new int[4][]; 17 | array[0] = new int[]{0,1,81}; 18 | array[1] = new int[]{0,1,2025,3025,9801}; 19 | array[2] = new int[]{0,1,88209,494209,998001}; 20 | array[3] = new int[]{0,1,4941729,7441984,24502500,25502500,52881984,60481729,99980001}; 21 | String[] format = new String[]{"00","0000","000000","00000000"}; 22 | int[] map = new int[]{0,0,0,0,1,0,2,0,3}; 23 | 24 | while(br.ready()) 25 | { 26 | int n = Integer.parseInt(br.readLine()); 27 | for(int i = 0; i < array[map[n]].length; i++) 28 | out.println(new DecimalFormat(format[map[n]]).format(array[map[n]][i])); 29 | } 30 | out.flush(); 31 | out.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /v002/TEXQuotes_UVa272.java: -------------------------------------------------------------------------------- 1 | package v002; 2 | 3 | import java.util.Scanner; 4 | //272 - TEX Quotes 5 | public class TEXQuotes_UVa272 { 6 | public static void main(String[] args) { 7 | Scanner scanner = new Scanner(System.in); 8 | String line; 9 | boolean open = true; 10 | while(scanner.hasNextLine()) { 11 | line = scanner.nextLine(); 12 | for(int i = 0; i < line.length(); i++) { 13 | if(line.charAt(i) == '\u001a') // EOF Character 14 | break; 15 | 16 | if(line.charAt(i) == '\"') { 17 | if(open == true) { 18 | System.out.print("``"); 19 | open = false; 20 | continue; 21 | } else { 22 | System.out.print("''"); 23 | open = true; 24 | continue; 25 | } 26 | } 27 | System.out.print(line.charAt(i)); 28 | } 29 | System.out.print("\n"); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /v002/TheHouseOfSantaClaus_UVa291.java: -------------------------------------------------------------------------------- 1 | package v002; 2 | public class TheHouseOfSantaClaus_UVa291 { 3 | 4 | static boolean[][] used = new boolean[6][6]; 5 | static StringBuilder sb = new StringBuilder(); 6 | 7 | static void bt(int pos, int line, String s) 8 | { 9 | if(line == 8) 10 | sb.append(s+"\n"); 11 | else 12 | { 13 | for(int i = 1; i <= 5; ++i) 14 | if(pos != i && !used[pos][i]) 15 | { 16 | used[pos][i] = used[i][pos] = true; 17 | bt(i, line + 1, s + i); 18 | used[pos][i] = used[i][pos] = false; 19 | } 20 | } 21 | } 22 | 23 | public static void main(String[] args) throws Exception 24 | { 25 | used[1][4] = used[4][1] = used[2][4] = used[4][2] = true; 26 | bt(1, 0, "1"); 27 | System.out.print(sb); 28 | } 29 | } -------------------------------------------------------------------------------- /v003/LetMeCountTheWays_UVa357.java: -------------------------------------------------------------------------------- 1 | package v003; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | //count ways --> long!!!!!!!!!!!!! 9 | public class LetMeCountTheWays_UVa357 { 10 | 11 | static final int UNCAL = -1; 12 | static long[][] memo; 13 | static int[] value = new int[]{1,5,10,25,50}; 14 | 15 | 16 | public static long dp(int coin, int rem) 17 | { 18 | if(rem==0) 19 | return 1; 20 | if(rem<0 || coin == 5) 21 | return 0; 22 | 23 | if(memo[coin][rem]!=UNCAL) 24 | return memo[coin][rem]; 25 | long take = dp(coin,rem-value[coin]); 26 | long leave = dp(coin+1,rem); 27 | return memo[coin][rem] = take + leave; 28 | 29 | } 30 | 31 | public static void main(String[] args) throws IOException { 32 | 33 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 | StringBuilder sb = new StringBuilder(); 35 | memo = new long[5][30001]; 36 | for(int i = 0; i < 5; i++) 37 | Arrays.fill(memo[i], UNCAL); 38 | 39 | 40 | while(br.ready()) 41 | { 42 | 43 | int n = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 44 | 45 | for(int i = 1; i <= 9; i++) 46 | dp(0,n/10*i); 47 | 48 | long ways = dp(0,n); 49 | if(ways==1) 50 | sb.append("There is only 1 way to produce "+n+" cents change.\n"); 51 | else 52 | sb.append("There are "+ways+" ways to produce "+n+" cents change.\n"); 53 | } 54 | System.out.print(sb); 55 | 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /v003/MasterMindHints_UVa340.java: -------------------------------------------------------------------------------- 1 | package v003; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class MasterMindHints_UVa340 { 9 | 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | int k = 1; 16 | while(true) 17 | { 18 | int n = Integer.parseInt(br.readLine()); 19 | if(n==0) 20 | break; 21 | sb.append("Game "+k+++":\n"); 22 | StringTokenizer st = new StringTokenizer(br.readLine()); 23 | int[] arr = new int[n]; 24 | for(int i = 0; i < n; i++) 25 | arr[i] = Integer.parseInt(st.nextToken()); 26 | 27 | while(true) 28 | { 29 | int p = 0; 30 | int[] g = new int[n]; 31 | st = new StringTokenizer(br.readLine()); 32 | for(int i = 0; i < n; i++) 33 | p += (g[i] = Integer.parseInt(st.nextToken())); 34 | if(p==0) 35 | break; 36 | int[] s = arr.clone(); 37 | int x = 0, y = 0; 38 | for(int i = 0; i < n; i++) if(s[i]==g[i]){x++;s[i]=g[i]=-1;} 39 | for(int i = 0; i < n; i++) 40 | if(s[i]!=-1) for(int j = 0; j < n; j++) if(s[i]==g[j]){y++;g[j]=-1;break;} 41 | sb.append(" ("+x+","+y+")\n"); 42 | } 43 | 44 | } 45 | System.out.print(sb); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v003/PeskyPalindromes_UVa353.java: -------------------------------------------------------------------------------- 1 | package v003; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.HashSet; 7 | 8 | public class PeskyPalindromes_UVa353{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | while(br.ready()) 16 | { 17 | 18 | String line = br.readLine(); 19 | HashSet h = new HashSet(); 20 | int count = 0; 21 | for(int i = 0; i < line.length(); i++) 22 | { 23 | for(int j = 0; j < line.length() - i; j++) 24 | { 25 | String cur = line.substring(j, j + i + 1); 26 | if(isPalindrome(cur)&&!h.contains(cur)) 27 | { 28 | count++; 29 | h.add(cur); 30 | } 31 | } 32 | } 33 | sb.append("The string '").append(line).append("' contains ").append(count).append(" palindromes.\n"); 34 | } 35 | System.out.print(sb); 36 | 37 | 38 | 39 | } 40 | 41 | public static boolean isPalindrome(String s) 42 | { 43 | if(s.length()<=1) 44 | return true; 45 | if(s.charAt(0)!=s.charAt(s.length()-1)) 46 | return false; 47 | return isPalindrome(s.substring(1,s.length()-1)); 48 | } 49 | } -------------------------------------------------------------------------------- /v004/ACORN_UVa4106.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class ACORN_UVa4106 { 9 | 10 | static int[] memo; 11 | static int[][] acorns; 12 | static int t,f,H; 13 | 14 | public static int dp() 15 | { 16 | for(int i = H - 1; i >= 0; i--) 17 | { 18 | for(int j = 0; j < t; j++) 19 | { 20 | acorns[j][i] += Math.max(acorns[j][i+1], i + f <= H?memo[i+f]:0); 21 | memo[i] = Math.max(memo[i], acorns[j][i]); 22 | } 23 | } 24 | return memo[0]; 25 | 26 | } 27 | 28 | public static void main(String[] args) throws NumberFormatException, IOException { 29 | 30 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 31 | StringBuilder sb = new StringBuilder(); 32 | 33 | int TC =Integer.parseInt(br.readLine()); 34 | while(TC-->0) 35 | { 36 | StringTokenizer st = new StringTokenizer(br.readLine()); 37 | t = Integer.parseInt(st.nextToken()); 38 | H = Integer.parseInt(st.nextToken()); 39 | f = Integer.parseInt(st.nextToken()); 40 | acorns = new int[t][H+1]; 41 | for(int i = 0; i < t; i++) 42 | { 43 | st = new StringTokenizer(br.readLine()); 44 | int count = Integer.parseInt(st.nextToken()); 45 | while(count-->0) 46 | acorns[i][Integer.parseInt(st.nextToken())]++; 47 | } 48 | memo = new int[H+1]; 49 | int max = dp(); 50 | sb.append(max+"\n"); 51 | } 52 | System.out.print(sb); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /v004/GraphConnectivity_UVa459.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class GraphConnectivity_UVa459 { 8 | 9 | static int N; 10 | static boolean[][] adjMatrix; 11 | static boolean visited[]; 12 | 13 | public static void dfs(int u) 14 | { 15 | visited[u] = true; 16 | for(int i = 0; i < N; i++) 17 | if(adjMatrix[u][i] && !visited[i]) 18 | dfs(i); 19 | } 20 | 21 | public static void main(String[] args) throws NumberFormatException, IOException { 22 | 23 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 24 | StringBuilder sb = new StringBuilder(); 25 | 26 | int TC = Integer.parseInt(br.readLine()); 27 | br.readLine(); 28 | 29 | while(TC-->0) 30 | { 31 | 32 | N = br.readLine().charAt(0) - 'A' + 1; 33 | adjMatrix = new boolean[N][N]; 34 | String line; 35 | while(br.ready() && !(line=br.readLine()).equals("")) 36 | { 37 | int u = line.charAt(0) - 'A'; 38 | int v = line.charAt(1) - 'A'; 39 | adjMatrix[u][v] = adjMatrix[v][u] = true; 40 | } 41 | visited = new boolean[N]; 42 | int c = 0; 43 | for(int i = 0; i < N; i++) 44 | if(!visited[i]) 45 | { 46 | c++; dfs(i); 47 | } 48 | 49 | sb.append(c+"\n"); 50 | if(TC!=0) 51 | sb.append("\n"); 52 | } 53 | System.out.print(sb); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /v004/HangmanJudge_UVa489.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | 8 | public class HangmanJudge_UVa489 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | PrintWriter out = new PrintWriter(System.out); 14 | while(true) 15 | { 16 | int k = Integer.parseInt(br.readLine()); 17 | if(k==-1) 18 | break; 19 | int[] word = new int[26]; 20 | boolean[] checked = new boolean[26]; 21 | String line = br.readLine(); 22 | int length = line.length(); 23 | for(int i = 0; i < length; i++) 24 | word[line.charAt(i) - 'a']++; 25 | line = br.readLine(); 26 | int strokes = 0; 27 | for(int i = 0; i < line.length(); i++) 28 | { 29 | int c = line.charAt(i) - 'a'; 30 | if(!checked[c]) 31 | { 32 | if(word[c]==0){if(++strokes==7)break;} 33 | else 34 | { 35 | length -= word[c]; 36 | if(length==0)break; 37 | } 38 | checked[c] = true; 39 | } 40 | } 41 | out.printf("Round %d\n",k); 42 | if(length==0) 43 | out.println("You win."); 44 | else 45 | if(strokes==7) 46 | out.println("You lose."); 47 | else 48 | out.println("You chickened out."); 49 | 50 | } 51 | out.flush(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /v004/MachinedSurfaces_UVa414.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | 8 | public class MachinedSurfaces_UVa414{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t; 15 | while((t=Integer.parseInt(br.readLine()))!=0) 16 | { 17 | int[] rowXs = new int[t]; int max = 0; 18 | for(int i = 0; i < t; i++) 19 | { 20 | int count = 0; 21 | String s = br.readLine(); 22 | for(int j = 0; j < 25; j++) 23 | if(s.charAt(j)=='X') 24 | count++; 25 | if(count > max) 26 | max = count; 27 | rowXs[i] = count; 28 | } 29 | int spaces = 0; 30 | for(int i = 0; i < t; i++) 31 | spaces += max - rowXs[i]; 32 | sb.append(spaces+"\n"); 33 | } 34 | System.out.print(sb); 35 | 36 | } 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /v004/PermutationArrays_UVa482.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class PermutationArrays_UVa482{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | 16 | for(int j = 0; j < t; j++) 17 | { 18 | br.readLine(); 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int n = st.countTokens(); 21 | int[] indexArray = new int[n]; 22 | for(int i = 0; i < n; i++) 23 | indexArray[i] = Integer.parseInt(st.nextToken()); 24 | st = new StringTokenizer(br.readLine()); 25 | String[] x = new String[st.countTokens()]; 26 | for(int i = 0; i < x.length; i++) 27 | x[i] = st.nextToken(); 28 | String[] y = new String[x.length]; 29 | for(int i = 0; i < x.length; i++) 30 | y[indexArray[i]-1] = x[i]; 31 | for(int i = 0; i < y.length; i++) 32 | sb.append(y[i]+"\n"); 33 | if(t-1!=j) 34 | sb.append("\n"); 35 | } 36 | 37 | System.out.print(sb); 38 | 39 | 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /v004/TheDecoder_UVa458.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class TheDecoder_UVa458 { 8 | 9 | public static void main(String[] args) throws IOException { 10 | 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | while(br.ready()) 15 | { 16 | String in = br.readLine(); 17 | for(int i = 0; i < in.length(); i++) 18 | sb.append((char)(in.charAt(i)-7)); 19 | sb.append("\n"); 20 | } 21 | System.out.print(sb); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /v004/UnixIs_UVa400.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | 8 | public class UnixIs_UVa400 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | while(br.ready()) 16 | { 17 | int N = Integer.parseInt(br.readLine()); 18 | String[] words = new String[N]; 19 | int max = 0; 20 | for(int i = 0; i < N; i++) 21 | { 22 | String line = br.readLine(); 23 | max = Math.max(max, line.length()); 24 | words[i] = line; 25 | } 26 | Arrays.sort(words); 27 | int C = 1 + (60-max)/(max+2); 28 | int R = (int) Math.ceil(N*1.0/C); 29 | String[][] out = new String[R][C]; 30 | for(int i = 0, k = 0; i < C; i++) 31 | for(int j = 0; j < R && k < N; j++, k++) 32 | out[j][i] = words[k]; 33 | for(int i = 0; i < 60; i++) 34 | sb.append("-"); 35 | sb.append("\n"); 36 | for(int i = 0; i < R; i++) 37 | { 38 | for(int j = 0; j < C; j++) 39 | { 40 | String cur = out[i][j]; 41 | if(cur==null) 42 | break; 43 | sb.append(cur); 44 | int spaces = (j == C - 1?max:(max+2)) - cur.length(); 45 | while(spaces-->0) 46 | sb.append(" "); 47 | } 48 | sb.append("\n"); 49 | } 50 | 51 | } 52 | System.out.print(sb); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /v004/WhatGoesUp_UVa481.java: -------------------------------------------------------------------------------- 1 | package v004; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.Stack; 10 | 11 | public class WhatGoesUp_UVa481 { 12 | 13 | 14 | public static void main(String[] args) throws IOException { 15 | 16 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | PrintWriter out = new PrintWriter(System.out); 18 | int[] array = new int[100000]; 19 | int N = 0; 20 | while(br.ready()) 21 | array[N++] = Integer.parseInt(br.readLine()); 22 | 23 | int[] parent = new int[100000]; 24 | int[] L_id = new int[100000]; 25 | ArrayList L = new ArrayList(); 26 | 27 | int lis = 0, lis_end = -1; 28 | for(int i = 0; i < N; i++) 29 | { 30 | int pos = Collections.binarySearch(L, array[i]); 31 | if(pos<0) 32 | pos = -(pos+1); 33 | if(pos>=L.size()) 34 | L.add(array[i]); 35 | else 36 | L.set(pos, array[i]); 37 | L_id[pos] = i; 38 | parent[i] = pos > 0?L_id[pos-1]:-1; 39 | if(pos + 1 >= lis) 40 | { 41 | lis = pos + 1; 42 | lis_end = i; 43 | } 44 | } 45 | out.printf("%d\n-\n",lis); 46 | Stack stack = new Stack(); 47 | while(lis_end!=-1) 48 | { 49 | stack.push(array[lis_end]); 50 | lis_end = parent[lis_end]; 51 | } 52 | while(!stack.isEmpty()) 53 | out.println(stack.pop()); 54 | out.flush(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /v005/BoxOfBricks_UVa591.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class BoxOfBricks_UVa591{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder();int k = 1; 14 | while(true) 15 | { 16 | int n = Integer.parseInt(br.readLine()); 17 | if(n==0) 18 | break; 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int[] blocks = new int[n]; 21 | int sum = 0; 22 | for(int i = 0; i < n; i++) 23 | { 24 | blocks[i] = Integer.parseInt(st.nextToken()); 25 | sum += blocks[i]; 26 | } 27 | int count = 0; 28 | for(int i = 0; i < n; i++) 29 | { 30 | if(blocks[i] primes; 13 | static boolean[] notPrime; 14 | public static void sieve(int N) { 15 | 16 | notPrime = new boolean[N+1]; 17 | notPrime[0] = notPrime[1] = true; 18 | primes = new ArrayList(); 19 | 20 | for (int i = 2; i <= N; i++) 21 | if (!notPrime[i]) 22 | { 23 | primes.add(i); 24 | for (int j = i * i; j <= N; j += i) 25 | notPrime[j] = true; 26 | } 27 | } 28 | 29 | 30 | public static void main(String[] args) throws IOException { 31 | 32 | 33 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 | StringBuilder sb = new StringBuilder(); 35 | 36 | while(true) 37 | { 38 | primes = new ArrayList(); 39 | String line = br.readLine(); 40 | StringTokenizer st = new StringTokenizer(line); 41 | int N = Integer.parseInt(st.nextToken()); 42 | if(N==0) 43 | break; 44 | sieve(N); 45 | int i = 1; 46 | while(true) 47 | { 48 | int prime = primes.get(i++); 49 | int other = N - prime; 50 | if(!notPrime[other]) 51 | { 52 | sb.append(N+" = "+prime+" + "+other+"\n"); 53 | break; 54 | } 55 | } 56 | 57 | } 58 | 59 | 60 | System.out.print(sb); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /v005/JillRidesAgain_UVa507.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class JillRidesAgain_UVa507 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | int TC = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 16 | for(int k = 1; k <= TC; k++) 17 | { 18 | int N = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 19 | int[] routes = new int[N-1]; 20 | for(int i = 0; i < N - 1; i++) 21 | routes[i] = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 22 | int a =-1, b = -1, max = 0, cur = 0, x=0; 23 | for(int i = 0; i < N - 1; i++) 24 | { 25 | cur += routes[i]; 26 | if(cur < 0) { cur = 0; x = i + 1; continue;} 27 | if(cur > max) {max = cur; a = x; b = i + 1;} 28 | else if (cur == max && (i + 1 - x > b - a)){a = x; b = i + 1;} 29 | } 30 | if(a==-1) 31 | out.printf("Route %d has no nice parts\n",k); 32 | else 33 | out.printf("The nicest part of route %d is between stops %d and %d\n",k,a+1,b+1); 34 | 35 | } 36 | out.flush(); 37 | out.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v005/MultiplyingByRotation_UVa550.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.LinkedList; 7 | import java.util.StringTokenizer; 8 | 9 | public class MultiplyingByRotation_UVa550 { 10 | 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder(); 16 | 17 | 18 | while(br.ready()) 19 | { 20 | StringTokenizer st = new StringTokenizer(br.readLine()); 21 | int base = Integer.parseInt(st.nextToken()); 22 | int least = Integer.parseInt(st.nextToken()); 23 | int factor = Integer.parseInt(st.nextToken()); 24 | LinkedList digits = new LinkedList(); 25 | 26 | digits.push(least); 27 | int product;int rem = 0; 28 | while((product=rem+factor*digits.peek())!=least) 29 | { 30 | digits.push(product%base); 31 | rem = product/base; 32 | } 33 | sb.append(digits.size()+"\n"); 34 | 35 | } 36 | System.out.print(sb); 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v005/OneLittleTwoLittleThreeLittleEndians_UVa594.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | 8 | public class OneLittleTwoLittleThreeLittleEndians_UVa594 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | 16 | while(br.ready()) 17 | { 18 | int x = Integer.parseInt(br.readLine()); 19 | out.printf("%d converts to %d\n",x,convert(x)); 20 | } 21 | 22 | out.flush(); 23 | out.close(); 24 | } 25 | 26 | public static int convert(int x) 27 | { 28 | int lowlow = x & 0x000000FF; 29 | int highhigh = x & 0xFF000000; 30 | int highlow = x & 0x00FF0000; 31 | int lowhigh = x & 0x0000FF00; 32 | return (lowhigh << 8 | highlow >>> 8) | (lowlow << 24 | highhigh >>> 24); 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /v005/Rails_UVa514.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.LinkedList; 7 | import java.util.Queue; 8 | import java.util.Stack; 9 | import java.util.StringTokenizer; 10 | 11 | public class Rails_UVa514 { 12 | 13 | public static void main(String[] args) throws NumberFormatException, IOException { 14 | 15 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | StringBuilder sb = new StringBuilder(); 17 | while(true) 18 | { 19 | int N = Integer.parseInt(br.readLine()); 20 | if(N==0) 21 | break; 22 | while(true) 23 | { 24 | StringTokenizer st = new StringTokenizer(br.readLine()); 25 | int first = Integer.parseInt(st.nextToken()); 26 | if(first==0) 27 | break; 28 | 29 | Queue B = new LinkedList(); 30 | B.add(first); 31 | for(int i = 1; i < N; i++) 32 | B.add(Integer.parseInt(st.nextToken())); 33 | 34 | Stack station = new Stack(); 35 | int A = 0; 36 | while(!B.isEmpty()) 37 | { 38 | int cur = B.remove(); 39 | while((station.isEmpty() || station.peek() != cur) && A <= N) 40 | station.push(++A); 41 | if(A>N) 42 | break; 43 | station.pop(); 44 | } 45 | if(A>N) 46 | sb.append("No\n"); 47 | else 48 | sb.append("Yes\n"); 49 | } 50 | sb.append("\n"); 51 | } 52 | System.out.print(sb); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /v005/TheSettlersOfCatan_UVa539.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class TheSettlersOfCatan_UVa539 { 10 | 11 | static boolean[][] adjMatrix; 12 | static boolean[] visited; 13 | static int N; 14 | 15 | public static int dfs(int u) 16 | { 17 | int max = 0; 18 | for(int i = 0; i < N; i++) 19 | if(adjMatrix[u][i]) 20 | { 21 | adjMatrix[u][i] = adjMatrix[i][u] = false; 22 | max = Math.max(dfs(i)+1,max); 23 | adjMatrix[u][i] = adjMatrix[i][u] = true; 24 | } 25 | return max; 26 | } 27 | public static void main(String[] args) throws IOException { 28 | 29 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 30 | PrintWriter out = new PrintWriter(System.out); 31 | while(true) 32 | { 33 | StringTokenizer st = new StringTokenizer(br.readLine()); 34 | N = Integer.parseInt(st.nextToken()); 35 | if(N==0) 36 | break; 37 | int E = Integer.parseInt(st.nextToken()); 38 | adjMatrix = new boolean[N][N]; 39 | while(E-->0) 40 | { 41 | st = new StringTokenizer(br.readLine()); 42 | int u = Integer.parseInt(st.nextToken()); 43 | int v = Integer.parseInt(st.nextToken()); 44 | adjMatrix[u][v] = adjMatrix[v][u] = true; 45 | } 46 | visited = new boolean[N]; 47 | int max = 0; 48 | for(int i = 0; i < N; i++) 49 | max = Math.max(max, dfs(i)); 50 | out.println(max); 51 | } 52 | out.flush(); 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /v005/TheSnail_UVa573.java: -------------------------------------------------------------------------------- 1 | package v005; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class TheSnail_UVa573{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | while(true) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int h = Integer.parseInt(st.nextToken()); 18 | if(h==0) 19 | break; 20 | double u = Integer.parseInt(st.nextToken()); 21 | double d = Integer.parseInt(st.nextToken()); 22 | double f = Integer.parseInt(st.nextToken())*1.0/100; 23 | int day = 0;double curU = 0; 24 | double curH = 0;boolean reached = false; 25 | while(!reached) 26 | { 27 | day++; 28 | curU = u-(day-1)*u*f; 29 | if(curU>0) 30 | curH = curH + curU; 31 | if(curH>h) 32 | reached = true; 33 | else 34 | { 35 | curH = curH - d; 36 | if(curH<0) 37 | break; 38 | } 39 | } 40 | if(reached) 41 | sb.append("success on day "+day+"\n"); 42 | else 43 | sb.append("failure on day "+day+"\n"); 44 | } 45 | 46 | System.out.print(sb); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /v006/BlowingFuses_UVa661.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class BlowingFuses_UVa661{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder();int k = 1; 14 | while(true) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int n = Integer.parseInt(st.nextToken()); 18 | if(n==0) 19 | break; 20 | int m = Integer.parseInt(st.nextToken()); 21 | int c = Integer.parseInt(st.nextToken()); 22 | int maxP = 0;int curP = 0; boolean willBlow = false; 23 | int[] state = new int[n]; 24 | int[] consumption = new int[n]; 25 | for(int i = 0; i < n; i++) 26 | consumption[i] = Integer.parseInt(br.readLine()); 27 | for(int i = 0; i < m; i++) 28 | { 29 | int device = Integer.parseInt(br.readLine())-1; 30 | if(state[device]==0) 31 | { 32 | curP += consumption[device]; 33 | if(curP>c) 34 | willBlow = true; 35 | if(curP>maxP) 36 | maxP = curP; 37 | } 38 | else 39 | curP -= consumption[device]; 40 | state[device] = (state[device]+1)%2; 41 | } 42 | sb.append("Sequence "+k+++"\n"); 43 | if(willBlow) 44 | sb.append("Fuse was blown.\n"); 45 | else 46 | sb.append("Fuse was not blown.\nMaximal power consumption was "+maxP+" amperes.\n"); 47 | sb.append("\n"); 48 | } 49 | System.out.print(sb); 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /v006/BookletPrinting_UVa637.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class BookletPrinting_UVa637 { 8 | 9 | public static void main(String[] args) throws NumberFormatException, IOException { 10 | 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | while(true) 15 | { 16 | int n = Integer.parseInt(br.readLine()); 17 | if(n==0) 18 | break; 19 | int N = (n+3)/4 * 4; 20 | 21 | int[] arr = new int[]{N,1,2,N-1}; 22 | sb.append("Printing order for "+n+" pages:\n"); 23 | for(int i = 1; i <= N >> 2; i++) 24 | { 25 | if(arr[0] <= n || arr[1] <= n) 26 | sb.append("Sheet "+i+", front: "+(arr[0]>n?"Blank":arr[0])+", "+(arr[1]>n?"Blank":arr[1])+"\n"); 27 | 28 | if(arr[2] <= n || arr[3] <= n) 29 | sb.append("Sheet "+i+", back : "+(arr[2]>n?"Blank":arr[2])+", "+(arr[3]>n?"Blank":arr[3])+"\n"); 30 | 31 | arr[0]-=2;arr[1]+=2;arr[2]+=2;arr[3]-=2; 32 | } 33 | } 34 | System.out.print(sb); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /v006/CoinChange_UVa674.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | 8 | public class CoinChange_UVa674 { 9 | 10 | static final int UNCAL = -1; 11 | 12 | static int[] value = new int[]{1,5,10,25,50}; 13 | static long[][] memo; 14 | 15 | public static long dp(int coin, int rem) 16 | { 17 | if(rem < 0) 18 | return 0; 19 | if(coin==5) 20 | if(rem==0) 21 | return 1; 22 | else 23 | return 0; 24 | if(memo[coin][rem]!=UNCAL) 25 | return memo[coin][rem]; 26 | return memo[coin][rem] = dp(coin,rem-value[coin]) + dp(coin+1,rem); 27 | 28 | } 29 | 30 | public static void main(String[] args) throws IOException { 31 | 32 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 33 | StringBuilder sb = new StringBuilder(); 34 | memo = new long[5][7500]; 35 | for(int i = 0; i < 5; i++) 36 | Arrays.fill(memo[i], UNCAL); 37 | 38 | 39 | while(br.ready()) 40 | { 41 | int n = Integer.parseInt(br.readLine()); 42 | long ways = dp(0,n); 43 | sb.append(ways+"\n"); 44 | } 45 | System.out.print(sb); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v006/HowManyKnights_UVa696.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class HowManyKnights_UVa696 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | while(true) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int N = Integer.parseInt(st.nextToken()); 19 | int M = Integer.parseInt(st.nextToken()); 20 | if(N==0 && M==0) 21 | break; 22 | int total = N * M; 23 | int max; 24 | if(N==1 || M==1) 25 | max = total; 26 | else 27 | if(N==2 || M==2) 28 | max = total/8*4 + (total%8==0?0:total%8/4*2+2); 29 | else 30 | max = (total+1)/2; 31 | out.printf("%d knights may be placed on a %d row %d column board.\n",max,N,M); 32 | } 33 | out.flush(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /v006/Parliament_UVa668.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.math.BigInteger; 7 | 8 | public class Parliament_UVa668 { 9 | 10 | static BigInteger[][] memo; 11 | static final int UNCAL = -1; 12 | static int N; 13 | static PrintWriter out = new PrintWriter(System.out); 14 | 15 | public static BigInteger dp(int n, int curSum) 16 | { 17 | if(curSum==N) 18 | return BigInteger.ONE; 19 | if(curSum>N || n > N) 20 | return BigInteger.ZERO; 21 | if(memo[n][curSum]!=null) 22 | return memo[n][curSum]; 23 | 24 | return memo[n][curSum] = dp(n+1,curSum).max(BigInteger.valueOf(n).multiply(dp(n+1,curSum+n))); 25 | } 26 | 27 | public static void print(int n, int curSum) 28 | { 29 | if(curSum>N || n > N) 30 | return; 31 | BigInteger optimal = dp(n,curSum); 32 | if(optimal.equals(dp(n+1,curSum))) 33 | print(n+1,curSum); 34 | else 35 | { 36 | out.print(n+(curSum+n==N?"\n":" ")); 37 | print(n+1,curSum+n); 38 | } 39 | 40 | 41 | } 42 | 43 | public static void main(String[] args) throws NumberFormatException, IOException { 44 | 45 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 46 | int TC = Integer.parseInt(br.readLine()); 47 | 48 | while(TC-->0) 49 | { 50 | br.readLine(); 51 | N = Integer.parseInt(br.readLine()); 52 | memo = new BigInteger[1001][1001]; 53 | print(1,0); 54 | if(TC!=0) 55 | out.println(); 56 | } 57 | 58 | 59 | out.flush(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /v006/Passwords_UVa628.java: -------------------------------------------------------------------------------- 1 | package v006; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | 7 | public class Passwords_UVa628 { 8 | 9 | static PrintWriter out = new PrintWriter(System.out); 10 | static String[] dic; 11 | static String[] pass; 12 | static int N, L; 13 | static String rule; 14 | 15 | public static void backtrack(int i) 16 | { 17 | if(i==L) { print(); return; } 18 | 19 | if(rule.charAt(i)=='0') 20 | for(int j = 0; j < 10; j++) 21 | { 22 | pass[i] = Integer.toString(j); 23 | backtrack(i+1); 24 | } 25 | else 26 | for(int j = 0; j < N; j++) 27 | { 28 | pass[i] = dic[j]; 29 | backtrack(i+1); 30 | } 31 | } 32 | 33 | public static void print() 34 | { 35 | for(int i = 0; i < L - 1; i++) 36 | out.print(pass[i]); 37 | out.println(pass[L-1]); 38 | } 39 | 40 | public static void main(String[] args) throws IOException { 41 | 42 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 43 | 44 | 45 | while(br.ready()) 46 | { 47 | N = Integer.parseInt(br.readLine()); 48 | dic = new String[N]; 49 | for(int i = 0; i < N; i++) 50 | dic[i] = br.readLine(); 51 | out.print("--\n"); 52 | int M = Integer.parseInt(br.readLine()); 53 | while(M-->0) 54 | { 55 | rule = br.readLine(); 56 | L = rule.length(); 57 | pass = new String[L]; 58 | backtrack(0); 59 | } 60 | } 61 | out.flush(); 62 | out.close(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /v007/QueensChessProblem_UVa750.java: -------------------------------------------------------------------------------- 1 | package v007; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | 6 | public class QueensChessProblem_UVa750 { 7 | 8 | static PrintWriter out = new PrintWriter(System.out); 9 | static int counter; 10 | static int[] row = new int[9]; 11 | static int a,b; 12 | public static void backtrack(int col) 13 | { 14 | if(col==9) 15 | { 16 | if(row[b]==a) 17 | print(); 18 | return; 19 | } 20 | for(int i = 1; i <= 8;i++) 21 | if(valid(col,i)) 22 | { 23 | row[col] = i; 24 | backtrack(col+1); 25 | } 26 | 27 | } 28 | 29 | public static boolean valid(int col, int tryRow) 30 | { 31 | for(int i = 1; i < col; i++) 32 | if(row[i]==tryRow || Math.abs(tryRow-row[i])== Math.abs(col-i)) 33 | return false; 34 | return true; 35 | } 36 | 37 | public static void print() 38 | { 39 | out.printf("%2d ",++counter ); 40 | for(int i = 1; i < 9; i++) 41 | out.printf(" %d",row[i]); 42 | out.println(); 43 | } 44 | 45 | public static void main(String[] args) { 46 | 47 | Scanner sc = new Scanner(System.in); 48 | 49 | int TC = sc.nextInt(); 50 | while(TC-->0) 51 | { 52 | out.println("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n"); 53 | counter = 0; 54 | a = sc.nextInt(); 55 | b = sc.nextInt(); 56 | 57 | backtrack(1); 58 | 59 | if(TC!=0) 60 | out.println(); 61 | } 62 | out.flush(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /v007/TheHammingDistanceProblem_UVa729.java: -------------------------------------------------------------------------------- 1 | package v007; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class TheHammingDistanceProblem_UVa729 { 10 | 11 | static int N,H; 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | PrintWriter out = new PrintWriter(System.out);StringTokenizer st; 16 | int TC = Integer.parseInt(br.readLine()); 17 | while(TC-->0) 18 | { 19 | br.readLine(); 20 | st = new StringTokenizer(br.readLine()); 21 | N = Integer.parseInt(st.nextToken()); 22 | H = Integer.parseInt(st.nextToken()); 23 | int max = (int)Math.pow(2, N) - 1; 24 | for(int i = 1; i <= max; i++) 25 | if(countOnes(i)==H) 26 | { 27 | out.printf("%0"+N+"d\n",Long.parseLong(Integer.toBinaryString(i))); 28 | } 29 | if(TC!=0) 30 | out.println(); 31 | } 32 | 33 | out.flush(); 34 | out.close(); 35 | } 36 | 37 | public static int countOnes(int x) 38 | { 39 | int count = 0; 40 | for(int i = 0; i < N; i++) 41 | if((x & (1<0) 48 | { 49 | line = br.readLine(); 50 | for(int i = 0; i < N; i++) 51 | x[map[i]] = line.charAt(i) - '0'; 52 | 53 | sb.append(dfs(1, 0)); 54 | } 55 | sb.append("\n\n"); 56 | } 57 | System.out.print(sb); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /v008/CoastTracker_UVa824.java: -------------------------------------------------------------------------------- 1 | package v008; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.StringTokenizer; 7 | 8 | public class CoastTracker_UVa824 { 9 | 10 | static int[][] island; 11 | static int x,y,d; 12 | static int[] dy = new int[]{1,1,0,-1,-1,-1,0,1}; 13 | static int[] dx = new int[]{0,-1,-1,-1,0,1,1,1}; 14 | 15 | public static int findNext() 16 | { 17 | int start = (d + 6)%8; 18 | for(int i = 0; i < 8; i++) 19 | { 20 | if(island[y+dy[start]][x+dx[start]]==1) 21 | return start; 22 | else 23 | start = (start+1)%8; 24 | } 25 | return -1; 26 | } 27 | 28 | public static void main(String[] args) throws IOException { 29 | 30 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 31 | PrintWriter out = new PrintWriter(System.out); 32 | StringTokenizer st; 33 | while(true) 34 | { 35 | st = new StringTokenizer(br.readLine()); 36 | x = Integer.parseInt(st.nextToken()); 37 | if(x==-1) 38 | break; 39 | y = Integer.parseInt(st.nextToken()); 40 | d = Integer.parseInt(st.nextToken()); 41 | island = new int[y+2][x+2]; 42 | for(int i = 0; i < 8; i++) 43 | { 44 | st = new StringTokenizer(br.readLine()); 45 | int a = Integer.parseInt(st.nextToken()); 46 | int b = Integer.parseInt(st.nextToken()); 47 | island[b][a] = Integer.parseInt(st.nextToken()); 48 | } 49 | out.println(findNext()); 50 | } 51 | out.flush(); 52 | out.close(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /v008/Steps_UVa846.java: -------------------------------------------------------------------------------- 1 | package v008; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.util.StringTokenizer; 6 | 7 | public class Steps_UVa846 { 8 | 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int TC = Integer.parseInt(br.readLine()); 16 | while(TC-->0) 17 | { 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | int x = Integer.parseInt(st.nextToken()); 20 | int y = Integer.parseInt(st.nextToken()); 21 | 22 | int distance = y - x; 23 | int maxStep = 0; 24 | while(true) 25 | { 26 | int curD = maxStep + (maxStep) * (maxStep-1); 27 | if(curD>distance) 28 | break; 29 | maxStep++; 30 | } 31 | maxStep--; 32 | if(maxStep==0) 33 | sb.append("0\n"); 34 | else 35 | { 36 | int steps = 1 + 2*(maxStep-1); 37 | int rem = distance - (maxStep + (maxStep) * (maxStep-1)); 38 | while(rem!=0) 39 | { 40 | steps += rem/maxStep; 41 | rem %= maxStep; 42 | maxStep--; 43 | } 44 | sb.append(steps+"\n"); 45 | } 46 | 47 | } 48 | 49 | System.out.print(sb); 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /v009/HowMany_UVa986.java: -------------------------------------------------------------------------------- 1 | package v009; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class HowMany_UVa986 { 10 | 11 | static final int UNCAL = -1; 12 | static int[][][][] memo; 13 | static int k, n; 14 | 15 | static int dp(int x, int y, int r, int y_last) 16 | { 17 | if(r < 0 || y < 0) 18 | return 0; 19 | if(x == n<<1) 20 | if(y == 0 && r == 0) 21 | return 1; 22 | else 23 | return 0; 24 | if(memo[x][y][r][y_last] != UNCAL) 25 | return memo[x][y][r][y_last]; 26 | 27 | return memo[x][y][r][y_last] = dp(x+1,y+1,r,0) + dp(x+1,y-1, y_last==0 && y == k?(r-1):r,1); 28 | } 29 | 30 | public static void main(String[] args) throws IOException { 31 | 32 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 33 | StringBuilder sb = new StringBuilder(); 34 | while(br.ready()) 35 | { 36 | StringTokenizer st = new StringTokenizer(br.readLine()); 37 | n = Integer.parseInt(st.nextToken()); 38 | int r = Integer.parseInt(st.nextToken()); 39 | k = Integer.parseInt(st.nextToken()); 40 | memo = new int[n<<1][n<<1][r+1][2]; 41 | for(int i = 0; i < n<<1; i++) 42 | for(int j = 0; j < n<<1; j++) 43 | for(int k = 0; k < r + 1; k++) 44 | Arrays.fill(memo[i][j][k], UNCAL); 45 | int ways = dp(0,0,r,0); 46 | sb.append(ways+"\n"); 47 | } 48 | System.out.print(sb); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /v009/JoanaandtheOddNumbers_UVa913.java: -------------------------------------------------------------------------------- 1 | package v009; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | 10 | public class JoanaandtheOddNumbers_UVa913 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner sc = new Scanner(System.in); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | while(sc.ready()) 17 | { 18 | int n = (sc.nextInt() + 1) / 2; 19 | long idx = 1l * n * n; 20 | out.println(get(idx) + get(idx - 1) + get(idx - 2)); 21 | } 22 | out.flush(); 23 | out.close(); 24 | } 25 | 26 | static long get(long n) { return 2 * n - 1; } 27 | 28 | static class Scanner 29 | { 30 | StringTokenizer st; 31 | BufferedReader br; 32 | 33 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 34 | 35 | public String next() throws IOException 36 | { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine()); 39 | return st.nextToken(); 40 | } 41 | 42 | public int nextInt() throws IOException {return Integer.parseInt(next());} 43 | 44 | public long nextLong() throws IOException {return Long.parseLong(next());} 45 | 46 | public String nextLine() throws IOException {return br.readLine();} 47 | 48 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 49 | 50 | public boolean ready() throws IOException {return br.ready();} 51 | } 52 | } -------------------------------------------------------------------------------- /v010/Containers_UVa1062.java: -------------------------------------------------------------------------------- 1 | package v010; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | import java.util.StringTokenizer; 8 | 9 | public class Containers_UVa1062 { 10 | 11 | static int compute(String line) 12 | { 13 | ArrayList stacks = new ArrayList(); 14 | boolean[] hasStack = new boolean[26]; 15 | 16 | for(int i = 0; i < line.length(); i++) 17 | { 18 | int c = line.charAt(i) - 'A'; 19 | if(!hasStack[c]) 20 | { 21 | hasStack[c] = true; 22 | int min = 100, idx = -1; 23 | for(int j = 0; j < stacks.size(); j++) 24 | { 25 | int b = stacks.get(j); 26 | if(c <= b && b - c < min) 27 | { 28 | min = c -b; 29 | idx = j; 30 | } 31 | } 32 | if(idx==-1) 33 | stacks.add(c); 34 | else 35 | { 36 | hasStack[stacks.get(idx)] =false; 37 | stacks.set(idx, c); 38 | } 39 | } 40 | } 41 | return stacks.size(); 42 | } 43 | public static void main(String[] args) throws IOException { 44 | 45 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 46 | StringBuilder sb = new StringBuilder(); 47 | int k = 1; 48 | while(true) 49 | { 50 | String line = new StringTokenizer(br.readLine()).nextToken(); 51 | if(line.equals("end")) 52 | break; 53 | sb.append("Case "+k+++": "+compute(line)+"\n"); 54 | } 55 | System.out.print(sb); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /v011/CreditCheck_UVa1173.java: -------------------------------------------------------------------------------- 1 | package v011; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class CreditCheck_UVa1173 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | int tc = Integer.parseInt(br.readLine()); 14 | while(tc-->0) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int sum = 0; 18 | for(int i = 0; i < 4; i++) 19 | { 20 | int cur = Integer.parseInt(st.nextToken()); 21 | for(int j = 0; j < 4; j++) 22 | { 23 | int d = cur%10; 24 | cur /= 10; 25 | if(j%2==0) 26 | sum += d; 27 | else 28 | { 29 | d <<= 1; 30 | sum += d%10 + d/10; 31 | } 32 | } 33 | } 34 | if(sum%10==0) 35 | sb.append("Valid\n"); 36 | else 37 | sb.append("Invalid\n"); 38 | } 39 | System.out.print(sb); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /v012/Argus_UVa1203.java: -------------------------------------------------------------------------------- 1 | package v012; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.PriorityQueue; 7 | import java.util.StringTokenizer; 8 | 9 | public class Argus_UVa1203 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | PriorityQueue r = new PriorityQueue(1000); 16 | while(true) 17 | { 18 | String line = br.readLine(); 19 | if(line.equals("#")) 20 | break; 21 | StringTokenizer st = new StringTokenizer(line); 22 | st.nextToken(); 23 | r.add(new Pair(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()))); 24 | } 25 | int k = Integer.parseInt(br.readLine()); 26 | while(k-->0) 27 | { 28 | Pair cur = r.remove(); 29 | sb.append(cur.id).append("\n"); 30 | cur.period += cur.c; 31 | r.add(cur); 32 | } 33 | System.out.print(sb); 34 | 35 | } 36 | } 37 | 38 | class Pair implements Comparable 39 | { 40 | int id, period, c; 41 | Pair(int x, int y) 42 | { 43 | id = x; 44 | period = c = y; 45 | } 46 | 47 | public int compareTo(Pair x) 48 | { 49 | if(period!=x.period) 50 | return period - x.period; 51 | return id - x.id; 52 | } 53 | } -------------------------------------------------------------------------------- /v012/StringPopping_UVa1261.java: -------------------------------------------------------------------------------- 1 | package v012; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.TreeSet; 7 | 8 | public class StringPopping_UVa1261 { 9 | 10 | static TreeSet checked; 11 | 12 | static boolean pop(StringBuilder x) 13 | { 14 | if(checked.contains(x.toString())) 15 | return false; 16 | checked.add(x.toString()); 17 | if(x.length() == 0) 18 | return true; 19 | for(int i = 0; i < x.length(); i++) 20 | { 21 | char c = x.charAt(i); 22 | int j = i + 1; 23 | for(; j < x.length(); j++) 24 | if(x.charAt(j) != c) 25 | break; 26 | if(j - i < 2) 27 | continue; 28 | if(pop(new StringBuilder(x).delete(i, j))) 29 | return true; 30 | i = j - 1; 31 | } 32 | return false; 33 | } 34 | 35 | public static void main(String[] args) throws NumberFormatException, IOException { 36 | 37 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 38 | StringBuilder sb = new StringBuilder(); 39 | int TC = Integer.parseInt(br.readLine()); 40 | while(TC-->0) 41 | { 42 | checked = new TreeSet(); 43 | sb.append(pop(new StringBuilder(br.readLine()))?1:0).append("\n"); 44 | } 45 | System.out.print(sb); 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /v015/Score_UVa1585.java: -------------------------------------------------------------------------------- 1 | package v015; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class Score_UVa1585 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | int n = sc.nextInt(); 16 | while(n-->0) 17 | { 18 | int cons = 0, ans = 0; 19 | char[] s = sc.next().toCharArray(); 20 | for(char c: s) 21 | if(c == 'X') 22 | cons = 0; 23 | else 24 | ans += ++cons; 25 | out.println(ans); 26 | } 27 | out.flush(); 28 | out.close(); 29 | } 30 | 31 | static class Scanner 32 | { 33 | StringTokenizer st; 34 | BufferedReader br; 35 | 36 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 37 | 38 | public String next() throws IOException 39 | { 40 | while (st == null || !st.hasMoreTokens()) 41 | st = new StringTokenizer(br.readLine()); 42 | return st.nextToken(); 43 | } 44 | 45 | public int nextInt() throws IOException {return Integer.parseInt(next());} 46 | 47 | public long nextLong() throws IOException {return Long.parseLong(next());} 48 | 49 | public String nextLine() throws IOException {return br.readLine();} 50 | 51 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 52 | 53 | public boolean ready() throws IOException {return br.ready();} 54 | } 55 | } -------------------------------------------------------------------------------- /v100/FunnyEncryptionMethod_UVa10019.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | 10 | public class FunnyEncryptionMethod_UVa10019 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner sc = new Scanner(System.in); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | int tc = sc.nextInt(); 17 | while(tc-->0) 18 | { 19 | int M = sc.nextInt(); 20 | int x = Integer.bitCount(M); 21 | int y = Integer.bitCount(Integer.parseInt("" + M, 16)); 22 | out.printf("%d %d\n", x, y); 23 | } 24 | out.flush(); 25 | out.close(); 26 | } 27 | 28 | static class Scanner 29 | { 30 | StringTokenizer st; 31 | BufferedReader br; 32 | 33 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 34 | 35 | public String next() throws IOException 36 | { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine()); 39 | return st.nextToken(); 40 | } 41 | 42 | public int nextInt() throws IOException {return Integer.parseInt(next());} 43 | 44 | public long nextLong() throws IOException {return Long.parseLong(next());} 45 | 46 | public String nextLine() throws IOException {return br.readLine();} 47 | 48 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 49 | 50 | public boolean ready() throws IOException {return br.ready();} 51 | } 52 | } -------------------------------------------------------------------------------- /v100/GardenOfEden_UVa10001.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class GardenOfEden_UVa10001 { 9 | 10 | 11 | static int[] state; 12 | static int N; 13 | static int id; 14 | 15 | static boolean dp(int idx, int last, int cur, int start, int end) 16 | { 17 | if(idx==N) 18 | return cur==start && last == end; 19 | for(int i = 0; i < 8; i++) 20 | if((id >> i)%2==state[idx] && last == (i >> 2)%2 && cur == (i >> 1)%2) 21 | if(dp(idx+1,cur,i%2,start,end)) 22 | return true; 23 | return false; 24 | } 25 | 26 | public static void main(String[] args) throws IOException { 27 | 28 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 29 | StringBuilder sb = new StringBuilder(); 30 | 31 | while(br.ready()) 32 | { 33 | StringTokenizer st = new StringTokenizer(br.readLine()); 34 | id = Integer.parseInt(st.nextToken()); 35 | N = Integer.parseInt(st.nextToken()); 36 | state = new int[N]; 37 | String x = st.nextToken(); 38 | for(int i = 0; i < N; i++) 39 | state[i] = x.charAt(i) - '0'; 40 | boolean reachable = false; 41 | for(int i = 0; i < 8; i++) 42 | if((id >> i)%2==state[0]) 43 | if(reachable = dp(1,(i>>1)%2,i%2,(i>>1)%2,(i>>2)%2)) 44 | break; 45 | 46 | if(!reachable) 47 | sb.append("GARDEN OF EDEN\n"); 48 | else 49 | sb.append("REACHABLE\n"); 50 | } 51 | System.out.print(sb); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /v100/Hartals_UVa10050.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Hartals_UVa10050{ 8 | 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | int t = Integer.parseInt(br.readLine()); 15 | 16 | for(int i = 0; i < t; i++) 17 | { 18 | int n = Integer.parseInt(br.readLine()); 19 | int p = Integer.parseInt(br.readLine()); 20 | boolean[] days = new boolean[n];int count=0; 21 | for(int j = 0; j < p; j++) 22 | { 23 | int curh = Integer.parseInt(br.readLine()); 24 | int curDay = 0; 25 | while((curDay += curh)<=n) 26 | { 27 | if(curDay%7==6||curDay%7==0||days[curDay-1]) 28 | continue; 29 | count++; 30 | days[curDay-1] = true; 31 | } 32 | } 33 | sb.append(count+"\n"); 34 | 35 | } 36 | System.out.print(sb); 37 | } 38 | 39 | 40 | } -------------------------------------------------------------------------------- /v100/Interpreter_UVa10033.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Interpreter_UVa10033 { 8 | 9 | static int[] RAM, reg; 10 | static int counter; 11 | 12 | static void go(int idx) 13 | { 14 | counter++; 15 | int x = RAM[idx]; 16 | int d = x/10%10, n = x%10; 17 | switch(x/100) 18 | { 19 | case 0: if(reg[n] == 0) break; go(reg[d]); return; 20 | case 2: reg[d] = n; break; 21 | case 3: reg[d] = (reg[d]+n)%1000;break; 22 | case 4: reg[d] = (reg[d]*n)%1000;break; 23 | case 5: reg[d] = reg[n];break; 24 | case 6: reg[d] = (reg[n] + reg[d])%1000;break; 25 | case 7: reg[d] = (reg[n] * reg[d])%1000;break; 26 | case 8: reg[d] = RAM[reg[n]];break; 27 | case 9: RAM[reg[n]] = reg[d];break; 28 | default: return; 29 | } 30 | go(idx+1); 31 | } 32 | public static void main(String[] args) throws NumberFormatException, IOException { 33 | 34 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 35 | StringBuilder sb = new StringBuilder(); 36 | int tc = Integer.parseInt(br.readLine()); 37 | br.readLine(); 38 | while(tc-->0) 39 | { 40 | RAM = new int[1000]; 41 | reg = new int[10]; 42 | String line = br.readLine(); 43 | int idx = 0; 44 | while(line != null && !line.isEmpty()) 45 | { 46 | RAM[idx++] = Integer.parseInt(line); 47 | line = br.readLine(); 48 | } 49 | counter = 0; 50 | go(0); 51 | sb.append(counter+"\n"); 52 | if(tc!=0) 53 | sb.append("\n"); 54 | } 55 | System.out.print(sb); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /v100/JollyJumpers_UVa10038.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class JollyJumpers_UVa10038{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | while(br.ready()) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | 19 | if(isJolly(st)) 20 | sb.append("Jolly\n"); 21 | else 22 | sb.append("Not jolly\n"); 23 | } 24 | System.out.print(sb); 25 | 26 | 27 | } 28 | 29 | public static boolean isJolly(StringTokenizer st) 30 | { 31 | int n= Integer.parseInt(st.nextToken()); 32 | boolean[] flag = new boolean[n-1]; 33 | int pre = Integer.parseInt(st.nextToken()); 34 | for(int i = 0; i < n-1; i++) 35 | { 36 | int cur = Integer.parseInt(st.nextToken()); 37 | int diff = Math.abs(cur-pre); 38 | if(diff<1||diff>n-1||flag[diff-1]) 39 | return false; 40 | flag[diff-1] = true; 41 | pre = cur; 42 | } 43 | return true; 44 | } 45 | } -------------------------------------------------------------------------------- /v100/PlaceTheGuards_UVa10094.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class PlaceTheGuards_UVa10094 { 8 | 9 | static int[] dx = new int[]{0,0,1,1,1,-1,-1,-1}; 10 | static int[] dy = new int[]{-1,1,1,0,-1,1,0,-1}; 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | while(br.ready()) 17 | { 18 | int n = Integer.parseInt(br.readLine()); 19 | if(n < 4) 20 | { 21 | sb.append("Impossible\n"); 22 | continue; 23 | } 24 | int[] row = new int[n+1]; 25 | 26 | for(int i = 1; i <= n/2; i++) 27 | row[i] = 2 * i; 28 | for(int i = n/2 + 1, k = 1; i <= n; i++,k+=2) 29 | row[i] = k; 30 | if(n%6==2) 31 | { 32 | row[n/2+1] = 3; 33 | row[n/2+2] = 1; 34 | for(int i = n/2+3; i <= n; i++) 35 | row[i] += 2; 36 | row[n] = 5; 37 | } 38 | else 39 | if(n%6==3) 40 | { 41 | for(int i = 1; i <= n/2; i++) 42 | row[i] += 2; 43 | row[n/2] = 2; 44 | for(int i = n/2+1; i <= n; i++) 45 | row[i] += 4; 46 | row[n-1] = 1; 47 | row[n] = 3; 48 | } 49 | 50 | 51 | 52 | 53 | for(int i = 1; i < n; i++) 54 | sb.append(row[i]).append(" "); 55 | sb.append(row[n]).append("\n"); 56 | } 57 | System.out.print(sb); 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /v100/ReverseAndAdd_UVa10018.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | 8 | public class ReverseAndAdd_UVa10018{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 0; i < t; i++) 16 | { 17 | long number = Integer.parseInt(br.readLine()); 18 | reverseAndAdd(number, sb); 19 | } 20 | System.out.print(sb); 21 | 22 | 23 | } 24 | 25 | public static void reverseAndAdd(long number, StringBuilder sb) 26 | { 27 | int count = 0; 28 | while(!isPalindrome(number)) 29 | { 30 | count++; 31 | number += reverse(number); 32 | } 33 | sb.append(count+" "+number+"\n"); 34 | 35 | } 36 | public static long reverse(long number) 37 | { 38 | return new Integer(new StringBuffer(""+number).reverse().toString()); 39 | } 40 | 41 | public static boolean isPalindrome(long number) 42 | { 43 | return isPalindrome(""+number); 44 | } 45 | 46 | public static boolean isPalindrome(String s) 47 | { 48 | if(s.length()<=1) 49 | return true; 50 | if(s.charAt(0)!=s.charAt(s.length()-1)) 51 | return false; 52 | return isPalindrome(s.substring(1,s.length()-1)); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /v100/TakeTheLand_UVa10074.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class TakeTheLand_UVa10074 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | while(true) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int R = Integer.parseInt(st.nextToken()); 18 | int C = Integer.parseInt(st.nextToken()); 19 | if(R==0) 20 | break; 21 | int[][] prefix = new int[R][C]; 22 | for(int i = 0; i < R; i++) 23 | { 24 | st = new StringTokenizer(br.readLine()); 25 | for(int j = 0; j < C; j++) 26 | { 27 | prefix[i][j] = Integer.parseInt(st.nextToken()); 28 | if(j > 0) prefix[i][j] += prefix[i][j-1]; 29 | } 30 | } 31 | int max = 0; 32 | for(int j = 0; j < C; j++) 33 | for(int k = j; k < C; k++) 34 | { 35 | int cur = 0, start = 0; 36 | for(int r = 0; r < R; r++) 37 | { 38 | if(j > 0) cur += prefix[r][k] - prefix[r][j-1]; 39 | else cur += prefix[r][k]; 40 | 41 | if(cur==0) 42 | max = Math.max(max, (r - start + 1) * (k - j + 1)); 43 | if(cur!=0) 44 | { 45 | start = r + 1; 46 | cur = 0; 47 | } 48 | } 49 | } 50 | sb.append(max).append("\n"); 51 | } 52 | System.out.print(sb); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /v100/VitoFamily_UVa10041.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class VitoFamily_UVa10041 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int TC = Integer.parseInt(br.readLine()); 15 | 16 | while(TC-->0) 17 | { 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | int R = Integer.parseInt(st.nextToken()); 20 | int[] locations = new int[R]; 21 | for(int i = 0; i < R; i++) 22 | locations[i] = Integer.parseInt(st.nextToken()); 23 | 24 | int min = Integer.MAX_VALUE; 25 | for(int i = 0; i < R; i++) 26 | { 27 | int cur = locations[i]; 28 | int d = 0; 29 | for(int j = 0; j < R; j++) 30 | d += Math.abs(cur-locations[j]); 31 | min = Math.min(min, d); 32 | } 33 | sb.append(min).append("\n"); 34 | 35 | } 36 | System.out.print(sb); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /v100/kProblem_UVa10025.java: -------------------------------------------------------------------------------- 1 | package v100; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | 6 | public class kProblem_UVa10025 { 7 | 8 | public static void main(String[] args) throws IOException { 9 | 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | StringBuilder sb = new StringBuilder(); 12 | 13 | int TC = Integer.parseInt(br.readLine()); 14 | while(TC-->0) 15 | { 16 | br.readLine(); 17 | 18 | long k = Math.abs(Integer.parseInt(br.readLine())); 19 | int N = (int) Math.ceil((-1+Math.sqrt(1+8*k))/2); 20 | int diff = (int) (N*(N+1)/2 - k); 21 | 22 | if(diff%2==1) 23 | { 24 | if((N+1-diff)%2==0 && (N+1-diff)/2 <= N) 25 | N+=1; 26 | else 27 | N += 2; 28 | } 29 | if(k==0) 30 | sb.append("3\n"); 31 | else 32 | sb.append(N+"\n"); 33 | if(TC!=0) 34 | sb.append("\n"); 35 | } 36 | System.out.print(sb); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /v101/IrreducableBasicFractions_UVa10179.java: -------------------------------------------------------------------------------- 1 | package v101; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | public class IrreducableBasicFractions_UVa10179 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | 14 | Scanner sc = new Scanner(System.in); 15 | StringBuilder sb = new StringBuilder(); 16 | while(true) 17 | { 18 | int n = sc.nextInt(); 19 | if(n == 0) break; 20 | sb.append(phi(n)+"\n"); 21 | } 22 | System.out.print(sb); 23 | } 24 | 25 | static int phi(int n) 26 | { 27 | int res = n; 28 | for(long i = 2; i * i <= n; i++ ) 29 | if(n%i == 0) 30 | { 31 | res -= res / i; 32 | while(n%i == 0) n /= i; 33 | } 34 | if(n > 1) 35 | res -= res / n; 36 | return res; 37 | } 38 | static class Scanner { 39 | StringTokenizer st; 40 | BufferedReader br; 41 | 42 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 43 | 44 | public String next() throws IOException 45 | { 46 | while (st == null || !st.hasMoreTokens()) 47 | st = new StringTokenizer(br.readLine()); 48 | return st.nextToken(); 49 | } 50 | 51 | public int nextInt() throws IOException {return Integer.parseInt(next());} 52 | 53 | public long nextLong() throws IOException {return Long.parseLong(next());} 54 | 55 | public String nextLine() throws IOException {return br.readLine();} 56 | 57 | public boolean ready() throws IOException {return br.ready();} 58 | 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /v101/Product_UVa10106.java: -------------------------------------------------------------------------------- 1 | package v101; 2 | import java.io.BufferedReader; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.math.BigInteger; 9 | import java.util.StringTokenizer; 10 | 11 | 12 | public class Product_UVa10106 { 13 | 14 | public static void main(String[] args) throws IOException { 15 | Scanner sc = new Scanner(System.in); 16 | PrintWriter out = new PrintWriter(System.out); 17 | while(sc.ready()) 18 | out.println(new BigInteger(sc.next()).multiply(new BigInteger(sc.next()))); 19 | out.flush(); 20 | out.close(); 21 | } 22 | static class Scanner 23 | { 24 | StringTokenizer st; 25 | BufferedReader br; 26 | 27 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 28 | 29 | public Scanner(FileReader s){ br = new BufferedReader(s);} 30 | 31 | public String next() throws IOException 32 | { 33 | while (st == null || !st.hasMoreTokens()) 34 | st = new StringTokenizer(br.readLine()); 35 | return st.nextToken(); 36 | } 37 | 38 | public int nextInt() throws IOException {return Integer.parseInt(next());} 39 | 40 | public long nextLong() throws IOException {return Long.parseLong(next());} 41 | 42 | public String nextLine() throws IOException {return br.readLine();} 43 | 44 | public double nextDouble() throws IOException 45 | { 46 | return Double.parseDouble(next()); 47 | } 48 | 49 | public boolean ready() throws IOException {return br.ready();} 50 | 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /v101/RequestForProtocol_UVa10141.java: -------------------------------------------------------------------------------- 1 | 2 | package v101; 3 | 4 | import java.io.BufferedReader; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.util.StringTokenizer; 8 | 9 | public class RequestForProtocol_UVa10141{ 10 | 11 | public static void main(String[] args) throws IOException 12 | { 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | int k = 1; 16 | while(true) 17 | { 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | int r = Integer.parseInt(st.nextToken()); 20 | if(r==0) 21 | break; 22 | int p = Integer.parseInt(st.nextToken()); 23 | for(int i = 0; i < r; i++) 24 | br.readLine(); 25 | int maxMetR = -1; String bestP = ""; double bestPrice = 0; 26 | for(int i = 0; i < p; i++) 27 | { 28 | String curP = br.readLine(); 29 | st = new StringTokenizer(br.readLine()); 30 | double curPrice = Double.parseDouble(st.nextToken()); 31 | int curMetR = Integer.parseInt(st.nextToken()); 32 | for(int j = 0; j < curMetR; j++) 33 | br.readLine(); 34 | if(curMetR>maxMetR) 35 | { 36 | maxMetR = curMetR;bestP = curP;bestPrice = curPrice; 37 | } 38 | else 39 | if(curMetR==maxMetR&&curPrice0) 20 | { 21 | TreeMap map = new TreeMap(); 22 | ArrayList names = new ArrayList(10000); 23 | int total = 0; 24 | String tree = br.readLine(); 25 | while(tree != null && !tree.isEmpty()) 26 | { 27 | total++; 28 | Integer count = map.get(tree); 29 | if(count == null) 30 | { 31 | names.add(tree); 32 | map.put(tree, 1); 33 | } 34 | else 35 | map.put(tree, count + 1); 36 | tree = br.readLine(); 37 | } 38 | Collections.sort(names); 39 | for(int i = 0; i < names.size(); i++) 40 | sb.append(names.get(i)+" "+(new DecimalFormat("0.0000").format(map.get(names.get(i))*100.0/total))+"\n"); 41 | if(tc!=0) 42 | sb.append("\n"); 43 | } 44 | System.out.print(sb); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /v102/HayPoints_UVa10295.java: -------------------------------------------------------------------------------- 1 | package v102; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | import java.util.TreeMap; 8 | 9 | public class HayPoints_UVa10295 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | StringTokenizer st = new StringTokenizer(br.readLine()); 16 | int n = Integer.parseInt(st.nextToken()); 17 | int m = Integer.parseInt(st.nextToken()); 18 | TreeMap dic = new TreeMap(); 19 | while(n-->0) 20 | { 21 | st = new StringTokenizer(br.readLine()); 22 | dic.put(st.nextToken(), Integer.parseInt(st.nextToken())); 23 | } 24 | while(m-->0) 25 | { 26 | long ans = 0; 27 | String line; 28 | while(!(line = br.readLine()).equals(".")) 29 | { 30 | st = new StringTokenizer(line); 31 | while(st.hasMoreTokens()) 32 | { 33 | String word = st.nextToken(); 34 | if(dic.containsKey(word)) 35 | ans += dic.get(word); 36 | } 37 | } 38 | sb.append(ans).append("\n"); 39 | } 40 | 41 | System.out.print(sb); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /v102/Soundex_UVa10260.java: -------------------------------------------------------------------------------- 1 | package v102; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Soundex_UVa10260{ 8 | 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | while(br.ready()) 14 | { 15 | String word = br.readLine(); 16 | String code = ""; String lastDigit = ""; 17 | for(int i = 0; i < word.length(); i++) 18 | { 19 | String curDigit = getDigit(word.charAt(i)); 20 | if(!curDigit.equals(lastDigit)) 21 | code +=curDigit; 22 | lastDigit = curDigit; 23 | } 24 | 25 | 26 | sb.append(code+"\n"); 27 | } 28 | System.out.print(sb); 29 | 30 | } 31 | 32 | public static String getDigit(char c) 33 | { 34 | switch(c) 35 | { 36 | case 'B': case 'F': case 'P': case 'V': 37 | return "1"; 38 | case 'C': case 'G': case 'J': case 'K': 39 | case 'Q': case 'S': case 'X': case 'Z': 40 | return "2"; 41 | case 'D': case 'T': return "3"; 42 | case 'L': return "4"; 43 | case 'M': case 'N': return "5"; 44 | case 'R': return "6"; 45 | } 46 | return ""; 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /v103/LiftLessEME_UVa10350.java: -------------------------------------------------------------------------------- 1 | package v103; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class LiftLessEME_UVa10350 { 10 | 11 | static final int INF = 10000000; 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | StringBuilder sb = new StringBuilder(); 17 | while(br.ready()) 18 | { 19 | String head = br.readLine(); 20 | StringTokenizer st = new StringTokenizer(br.readLine()); 21 | int n = Integer.parseInt(st.nextToken()); 22 | int m = Integer.parseInt(st.nextToken()); 23 | int[][][] time = new int[n-1][m][m]; 24 | for(int k = 0; k < n - 1; k++) 25 | for(int i = 0; i < m; i++) 26 | { 27 | st = new StringTokenizer(br.readLine()); 28 | for(int j = 0; j < m; j++) 29 | time[k][i][j] = Integer.parseInt(st.nextToken()); 30 | } 31 | int[][] dp = new int[n][m]; 32 | for(int i = 1; i < n; i++) 33 | Arrays.fill(dp[i], INF); 34 | for(int k = 1; k < n; k++) 35 | for(int j = 0; j < m; j++) 36 | for(int i = 0; i < m; i++) 37 | dp[k][j] = Math.min(dp[k][j], dp[k-1][i] + time[k-1][i][j]); 38 | int min = INF; 39 | for(int j = 0; j < m; j++) 40 | min = Math.min(dp[n-1][j], min); 41 | min += 2 * (n-1); 42 | sb.append(head+"\n"+min+"\n"); 43 | 44 | } 45 | 46 | System.out.print(sb); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /v103/SummationofPolynomials_UVa10302.java: -------------------------------------------------------------------------------- 1 | package v103; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | 10 | public class SummationofPolynomials_UVa10302 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner sc = new Scanner(System.in); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | long[] s = new long[50001]; 17 | for(int i = 1; i <= 50000; ++i) 18 | s[i] = s[i - 1] + 1l * i * i * i; 19 | while(sc.ready()) 20 | out.println(s[sc.nextInt()]); 21 | out.flush(); 22 | out.close(); 23 | } 24 | 25 | static class Scanner 26 | { 27 | StringTokenizer st; 28 | BufferedReader br; 29 | 30 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 31 | 32 | public String next() throws IOException 33 | { 34 | while (st == null || !st.hasMoreTokens()) 35 | st = new StringTokenizer(br.readLine()); 36 | return st.nextToken(); 37 | } 38 | 39 | public int nextInt() throws IOException {return Integer.parseInt(next());} 40 | 41 | public long nextLong() throws IOException {return Long.parseLong(next());} 42 | 43 | public String nextLine() throws IOException {return br.readLine();} 44 | 45 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 46 | 47 | public boolean ready() throws IOException {return br.ready();} 48 | } 49 | } -------------------------------------------------------------------------------- /v103/TwinPrimes_UVa10394.java: -------------------------------------------------------------------------------- 1 | package v103; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | 6 | 7 | 8 | public class TwinPrimes_UVa10394 { 9 | 10 | static int[] isPrime; 11 | static int[][] twins = new int[1000001][2]; 12 | static int counter = 1; 13 | 14 | public static void sieve(int N) { //generates all primes [2,N] 15 | 16 | isPrime = new int[N+1]; // zero means is prime 17 | isPrime[0] = isPrime[1] = 1; 18 | 19 | for (long i = 2; i <= N; i++) //to test primality of N, loop till i*i <= N 20 | if (isPrime[(int)i] == 0) 21 | { 22 | if(isPrime[(int) (i-2)]==0) 23 | { 24 | twins[counter][1] = (int)i; 25 | twins[counter++][0] = (int) (i - 2); 26 | } 27 | for (long j = i * i; j <= N; j += i) //to test primality of N, loop till j*j <= N 28 | isPrime[(int)j] = 1; 29 | } 30 | } //for N > 10^6 change int N to long N 31 | 32 | public static void main(String[] args) throws IOException { 33 | 34 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 35 | StringBuilder sb = new StringBuilder(); 36 | sieve(20000000); 37 | 38 | while(br.ready()) 39 | { 40 | int S = Integer.parseInt(br.readLine()); 41 | 42 | sb.append("("+twins[S][0]+", "+twins[S][1]+")\n"); 43 | 44 | } 45 | 46 | System.out.print(sb); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /v103/ZerosAndOnes_UVa10324.java: -------------------------------------------------------------------------------- 1 | package v103; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class ZerosAndOnes_UVa10324{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int k = 1; 15 | while(br.ready()) 16 | { 17 | String s = br.readLine(); 18 | if(s.equals("")) 19 | break; 20 | sb.append("Case "+k+++":\n"); 21 | 22 | int t = Integer.parseInt(br.readLine()); 23 | 24 | for(int i = 0; i < t; i++) 25 | { 26 | StringTokenizer st = new StringTokenizer(br.readLine()); 27 | int x = Integer.parseInt(st.nextToken()); 28 | int y = Integer.parseInt(st.nextToken()); 29 | if(x>y) 30 | { 31 | int tmp = x; 32 | x = y; 33 | y = tmp; 34 | } 35 | int j; 36 | for(j = x; j < y; j++) 37 | if(s.charAt(j)!=s.charAt(j+1)) 38 | break; 39 | if(j!=y) 40 | sb.append("No\n"); 41 | else 42 | sb.append("Yes\n"); 43 | } 44 | } 45 | System.out.print(sb); 46 | 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /v104/DieGame_UVa10409.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class DieGame_UVa10409 { 8 | 9 | public static void main(String[] args) throws NumberFormatException, IOException { 10 | 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | while(true) 14 | { 15 | int moves = Integer.parseInt(br.readLine()); 16 | if(moves==0) 17 | break; 18 | int[] state = new int[]{1,2,3}; 19 | int tmp; 20 | while(moves-->0) 21 | switch(br.readLine().charAt(0)) 22 | { 23 | case 'n': 24 | tmp = state[0];state[0] = 7 - state[1];state[1] = tmp;break; 25 | case 's': 26 | tmp = state[0];state[0] = state[1];state[1] = 7 - tmp;break; 27 | case 'w': 28 | tmp = state[0];state[0] = 7 - state[2];state[2] = tmp;break; 29 | default: 30 | tmp = state[0];state[0] = state[2];state[2] = 7 - tmp; 31 | } 32 | sb.append(state[0]).append("\n"); 33 | } 34 | System.out.print(sb); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /v104/HomerSimpson_UVa10465.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class HomerSimpson_UVa10465 { 10 | 11 | static final int INF = 1000000; 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | PrintWriter out = new PrintWriter(System.out); 17 | 18 | 19 | while(br.ready()) 20 | { 21 | StringTokenizer st = new StringTokenizer(br.readLine()); 22 | int m = Integer.parseInt(st.nextToken()); 23 | int n = Integer.parseInt(st.nextToken()); 24 | int t = Integer.parseInt(st.nextToken()); 25 | int[] memo = new int[t+1]; 26 | memo[0] = 0; 27 | for(int i = 1; i <= t; i++) 28 | { 29 | int x = -INF, y = -INF; 30 | if(i-n>=0) 31 | x = 1 + memo[i-n]; 32 | if(i-m>=0) 33 | y = 1 + memo[i-m]; 34 | memo[i] = Math.max(x, y); 35 | } 36 | int i; 37 | for(i = t; memo[i] < 0; i--); 38 | out.println(memo[i]+(i==t?"":(" "+(t-i)))); 39 | } 40 | out.flush(); 41 | out.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /v104/InjuredQueenProblem_UVa10401.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class InjuredQueenProblem_UVa10401 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | 17 | while(br.ready()) 18 | { 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | if(st.countTokens()==0) 21 | continue; 22 | String line = st.nextToken(); 23 | int n = line.length(); 24 | int[] row = new int[n]; 25 | 26 | for(int i = 0; i < n; i++) 27 | if(line.charAt(i)=='?') 28 | row[i] = -1; 29 | else 30 | if(line.charAt(i) >= '0' && line.charAt(i) <= '9') 31 | row[i] = line.charAt(i) - '0' - 1; 32 | else 33 | row[i] = line.charAt(i) - 'A' + 9; 34 | 35 | long[][] dp = new long[n][n]; 36 | if(row[0]==-1) 37 | for(int i = 0; i < n; i++) 38 | dp[0][i] = 1; 39 | else 40 | dp[0][row[0]] = 1; 41 | for(int j = 1; j < n; j++) 42 | for(int i = 0; i < n; i++) 43 | if(row[j] == -1 || row[j] == i) 44 | for(int k = 0; k < n; k++) 45 | if(Math.abs(k-i) > 1) 46 | dp[j][i] += dp[j-1][k]; 47 | long ways = 0; 48 | for(int i = 0; i < n; i++) 49 | ways += dp[n-1][i]; 50 | sb.append(ways+"\n"); 51 | } 52 | System.out.print(sb); 53 | 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /v104/SimpleDivision_UVa10407.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.math.BigInteger; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | 10 | public class SimpleDivision_UVa10407 { 11 | 12 | 13 | 14 | public static void main(String[] args) throws NumberFormatException, IOException { 15 | 16 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | StringBuilder sb = new StringBuilder(); 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | 20 | while(st.countTokens()>1) 21 | { 22 | BigInteger x = new BigInteger(st.nextToken()); 23 | BigInteger y = new BigInteger(st.nextToken()); 24 | BigInteger d = x.subtract(y).abs(); 25 | while(st.countTokens()>1) 26 | { 27 | BigInteger z = new BigInteger(st.nextToken()); 28 | d = d.gcd(z.subtract(x).abs()); 29 | x = z; 30 | } 31 | sb.append(d.toString()+"\n"); 32 | 33 | st = new StringTokenizer(br.readLine()); 34 | } 35 | 36 | 37 | System.out.print(sb); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v104/TheLandOfJustice_UVa10499.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class TheLandOfJustice_UVa10499 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | while(true) 16 | { 17 | int n = sc.nextInt(); 18 | if(n < 0) 19 | break; 20 | if(n == 1) 21 | n = 0; 22 | out.printf("%d%%\n", 25l * n); 23 | } 24 | out.flush(); 25 | out.close(); 26 | } 27 | 28 | static class Scanner 29 | { 30 | StringTokenizer st; 31 | BufferedReader br; 32 | 33 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 34 | 35 | public String next() throws IOException 36 | { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine()); 39 | return st.nextToken(); 40 | } 41 | 42 | public int nextInt() throws IOException {return Integer.parseInt(next());} 43 | 44 | public long nextLong() throws IOException {return Long.parseLong(next());} 45 | 46 | public String nextLine() throws IOException {return br.readLine();} 47 | 48 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 49 | 50 | public boolean ready() throws IOException {return br.ready();} 51 | } 52 | } -------------------------------------------------------------------------------- /v104/TheMarriageInterview_UVa10466.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.math.BigInteger; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | 10 | public class TheMarriageInterview_UVa10466 { 11 | 12 | static BigInteger[][] memo; 13 | static BigInteger counter; 14 | 15 | static BigInteger trib(int n,int back) 16 | { 17 | if(n<=1) return BigInteger.ONE; 18 | if(memo[n][back]!=null) 19 | return memo[n][back]; 20 | BigInteger sum = BigInteger.ONE; 21 | for(int i=1;i<=back;i++) 22 | sum = sum.add(trib(n-i,back)); 23 | return memo[n][back] = sum; 24 | } 25 | public static void main(String[] args) throws IOException { 26 | 27 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 28 | StringBuilder sb = new StringBuilder(); 29 | int k = 1; 30 | memo = new BigInteger[70][70]; 31 | while(true) 32 | { 33 | StringTokenizer st = new StringTokenizer(br.readLine()); 34 | int n = Integer.parseInt(st.nextToken()); 35 | int back = Integer.parseInt(st.nextToken()); 36 | if(n>61 || back > 60) 37 | break; 38 | sb.append("Case "+k+++": ").append(trib(n,back)).append("\n"); 39 | } 40 | System.out.print(sb); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /v104/WhatIsTheMarble_UVa10474.java: -------------------------------------------------------------------------------- 1 | package v104; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class WhatIsTheMarble_UVa10474 { 10 | 11 | static int bs(int[] A, int t) 12 | { 13 | int lo = 0, hi = A.length - 1, ans = -1; 14 | while(lo <= hi) 15 | { 16 | int mid = lo + (hi - lo)/2; 17 | if(A[mid] == t) 18 | { 19 | ans = mid; 20 | hi = mid - 1; 21 | } 22 | else 23 | if(A[mid] > t) 24 | hi = mid - 1; 25 | else 26 | lo = mid + 1; 27 | } 28 | return ans; 29 | } 30 | 31 | public static void main(String[] args) throws IOException { 32 | 33 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 | StringBuilder sb = new StringBuilder(); 35 | int k = 1; 36 | while(true) 37 | { 38 | StringTokenizer st = new StringTokenizer(br.readLine()); 39 | int n = Integer.parseInt(st.nextToken()); 40 | int q = Integer.parseInt(st.nextToken()); 41 | if(n == 0 && q == 0) 42 | break; 43 | sb.append("CASE# "+k+++":\n"); 44 | int[] A = new int[n]; 45 | for(int i = 0; i < n; i++) 46 | A[i] = Integer.parseInt(br.readLine()); 47 | Arrays.sort(A); 48 | while(q-->0) 49 | { 50 | int t = Integer.parseInt(br.readLine()); 51 | int ans = bs(A,t) + 1; 52 | if(ans != 0) 53 | sb.append(t+" found at "+ans+"\n"); 54 | else 55 | sb.append(t+" not found\n"); 56 | } 57 | } 58 | System.out.print(sb); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /v105/GuessingGame_UVa10530.java: -------------------------------------------------------------------------------- 1 | package v105; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.BitSet; 7 | 8 | public class GuessingGame_UVa10530 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | boolean play = true; 16 | while(play) 17 | { 18 | BitSet numbers = new BitSet(11); 19 | numbers.set(1,11); 20 | while(true) 21 | { 22 | int guess = Integer.parseInt(br.readLine()); 23 | if(guess==0) 24 | { 25 | play = false; 26 | break; 27 | } 28 | String response = br.readLine(); 29 | if(response.equals("right on")) 30 | { 31 | if(!numbers.get(guess)) 32 | sb.append("Stan is dishonest\n"); 33 | else 34 | sb.append("Stan may be honest\n"); 35 | break; 36 | } 37 | else 38 | if(response.equals("too high")) 39 | numbers.clear(guess,11); 40 | else 41 | numbers.clear(1,guess+1); 42 | } 43 | } 44 | System.out.print(sb); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /v106/EditorNottoobad_UVa10602.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class EditorNottoobad_UVa10602 { 8 | 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int TC = Integer.parseInt(br.readLine()); 15 | while(TC-->0) 16 | { 17 | int N = Integer.parseInt(br.readLine()); 18 | String last = br.readLine(); 19 | int X = last.length(); 20 | String[] words = new String[N-1]; 21 | for(int i = 0; i < N - 1; i++) 22 | words[i] = br.readLine(); 23 | StringBuilder x = new StringBuilder(); 24 | while(true) 25 | { 26 | x.append(last).append("\n"); 27 | int max = -1;int cur = -1; 28 | for(int i = 0; i < N -1; i++) 29 | if(words[i]!=null) 30 | { 31 | int match = match(last,words[i]); 32 | if(match>max) 33 | { 34 | max = match; 35 | cur =i; 36 | } 37 | } 38 | if(cur==-1) 39 | break; 40 | last = words[cur]; 41 | X += words[cur].length() - max; 42 | words[cur] = null; 43 | 44 | } 45 | sb.append(X+"\n").append(x); 46 | 47 | } 48 | System.out.print(sb); 49 | } 50 | 51 | static int match(String x, String y) 52 | { 53 | int i = 0; 54 | for(; i < x.length() && i < y.length(); i++) 55 | if(x.charAt(i)!=y.charAt(i)) 56 | return i; 57 | return i; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /v106/Luggage_UVa10664.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.util.Arrays; 6 | import java.util.StringTokenizer; 7 | 8 | public class Luggage_UVa10664 { 9 | 10 | static final int UNCAL = -1; 11 | static int N, weight[], memo[][]; 12 | 13 | public static int dp(int suitcase, int remW) 14 | { 15 | if(remW==0) 16 | return 1; 17 | if(suitcase==N || remW < 0) 18 | return 0; 19 | 20 | if(memo[suitcase][remW]!=UNCAL) 21 | return memo[suitcase][remW]; 22 | 23 | return memo[suitcase][remW] = dp(suitcase+1,remW) | dp(suitcase+1,remW-weight[suitcase]); 24 | } 25 | 26 | public static void main(String[] args) throws NumberFormatException, IOException { 27 | 28 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 29 | StringBuilder sb = new StringBuilder(); 30 | 31 | int TC = Integer.parseInt(br.readLine()); 32 | while(TC-->0) 33 | { 34 | int totalWeight = 0; 35 | weight = new int[20]; N = 0; 36 | StringTokenizer st = new StringTokenizer(br.readLine()); 37 | while(st.hasMoreTokens()) 38 | totalWeight += weight[N++] = Integer.parseInt(st.nextToken()); 39 | 40 | memo = new int[N][totalWeight+1]; 41 | for(int i = 0; i < N; i++) 42 | Arrays.fill(memo[i], UNCAL); 43 | if(totalWeight%2==1 || dp(0,totalWeight/2) == 0) 44 | sb.append("NO\n"); 45 | else 46 | sb.append("YES\n"); 47 | 48 | 49 | 50 | 51 | } 52 | System.out.print(sb); 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /v106/MaximumSumII_UVa10656.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | 8 | public class MaximumSumII_UVa10656 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | while(true) 15 | { 16 | int N = Integer.parseInt(br.readLine()); 17 | if(N==0) 18 | break; 19 | ArrayList out = new ArrayList(N); 20 | while(N-->0) 21 | { 22 | int cur = Integer.parseInt(br.readLine()); 23 | if(cur==0) 24 | continue; 25 | out.add(cur); 26 | } 27 | if(out.size()==0) 28 | sb.append("0\n"); 29 | else 30 | { 31 | for(int i = 0; i < out.size() - 1; i++) 32 | sb.append(out.get(i)).append(" "); 33 | sb.append(out.get(out.size()-1)).append("\n"); 34 | } 35 | } 36 | 37 | System.out.print(sb); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v106/TheDecadaryWatch_UVa10683.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.text.DecimalFormat; 8 | 9 | public class TheDecadaryWatch_UVa10683 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | StringBuilder sb = new StringBuilder(); 16 | while(br.ready()) 17 | { 18 | String line = br.readLine(); 19 | int hh = Integer.parseInt(line.substring(0, 2)); 20 | int mm = Integer.parseInt(line.substring(2, 4)); 21 | int ss = Integer.parseInt(line.substring(4, 6)); 22 | int cc = Integer.parseInt(line.substring(6)); 23 | 24 | cc = 125 * (hh*60*60*100 + mm*60*100 + ss*100 + cc) /108; 25 | 26 | hh = cc / 1000000; 27 | cc %= 1000000; 28 | mm = cc / 10000; 29 | cc %= 10000; 30 | ss = cc / 100; 31 | cc %= 100; 32 | sb.append(new DecimalFormat("0").format(hh)).append(new DecimalFormat("00").format(mm)).append(new DecimalFormat("00").format(ss)).append(new DecimalFormat("00").format(cc)).append("\n"); 33 | } 34 | out.print(sb); 35 | out.flush(); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /v106/TheJackpot_UVa10684.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class TheJackpot_UVa10684 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | while(true) 17 | { 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | while(!st.hasMoreTokens()) 20 | st = new StringTokenizer(br.readLine()); 21 | int n = Integer.parseInt(st.nextToken()); 22 | if(n==0) 23 | break; 24 | int max = 0; int cur = 0; 25 | st = new StringTokenizer(br.readLine()); 26 | while(n-->0) 27 | { 28 | while(!st.hasMoreTokens()) 29 | st = new StringTokenizer(br.readLine()); 30 | cur += Integer.parseInt(st.nextToken()); 31 | if(cur < 0) {cur = 0; continue;} 32 | max = Math.max(cur, max); 33 | } 34 | if(max>0) 35 | out.printf("The maximum winning streak is %d.\n",max); 36 | else 37 | out.println("Losing streak."); 38 | } 39 | out.flush(); 40 | out.close(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /v106/ThePoorGiant_UVa10688.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.Arrays; 8 | import java.util.StringTokenizer; 9 | 10 | public class ThePoorGiant_UVa10688 { 11 | 12 | static final int UNCAL = -1; 13 | static final int INF = 10000000; 14 | static int[][] memo; 15 | static int k; 16 | 17 | static int dp(int b, int e) 18 | { 19 | if(e-b<=1) 20 | return 0; 21 | if(memo[b][e]!=UNCAL) 22 | return memo[b][e]; 23 | int min = INF; 24 | for(int i = b; i < e; i++) 25 | min = Math.min(min,(e-b)*(i+k)+dp(b,i)+dp(i+1,e)); 26 | return memo[b][e] = min; 27 | } 28 | 29 | public static void main(String[] args) throws NumberFormatException, IOException { 30 | 31 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 32 | PrintWriter out = new PrintWriter(System.out); 33 | int TC = Integer.parseInt(br.readLine()); 34 | for(int i = 1; i <= TC; i++) 35 | { 36 | StringTokenizer st = new StringTokenizer(br.readLine()); 37 | int n = Integer.parseInt(st.nextToken()); 38 | k = Integer.parseInt(st.nextToken()); 39 | memo = new int[n+2][n+2]; 40 | for(int j = 1; j <= n; j++) 41 | Arrays.fill(memo[j], UNCAL); 42 | out.printf("Case %d: %d\n",i,dp(1,n+1)); 43 | } 44 | out.flush(); 45 | out.close(); 46 | } 47 | 48 | 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /v106/WhatIsTheCard_UVa10646.java: -------------------------------------------------------------------------------- 1 | package v106; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | import java.util.Stack; 6 | 7 | public class WhatIsTheCard_UVa10646 { 8 | 9 | 10 | public static void main(String[] args) { 11 | 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | int TC = sc.nextInt(); 16 | for(int k = 1; k <= TC; k++) 17 | { 18 | Stack pile = new Stack(); 19 | for(int i = 0; i < 52; i++) 20 | pile.push(sc.next()); 21 | Stack hand = new Stack(); 22 | for(int i = 0; i < 25; i++) 23 | hand.push(pile.pop()); 24 | int Y = 0; 25 | for(int i = 0; i < 3; i++) 26 | { 27 | String card = pile.pop(); 28 | int X = getValue(card); 29 | Y += X; 30 | X = 10 - X; 31 | while(X-->0) 32 | pile.pop(); 33 | } 34 | while(!hand.isEmpty()) 35 | pile.push(hand.pop()); 36 | int remove = pile.size() - Y; 37 | while(remove-->0) 38 | pile.pop(); 39 | out.printf("Case %d: %s\n",k,pile.pop()); 40 | } 41 | out.flush(); 42 | } 43 | 44 | public static int getValue(String card) 45 | { 46 | int x = card.charAt(0) - '0'; 47 | if(x>=2&& x<=9) 48 | return x; 49 | return 10; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /v107/Ants_UVa10714.java: -------------------------------------------------------------------------------- 1 | package v107; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class Ants_UVa10714 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int TC = Integer.parseInt(br.readLine()); 16 | while(TC-->0) 17 | { 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | int L = Integer.parseInt(st.nextToken()); 20 | int N = Integer.parseInt(st.nextToken()); 21 | if(N==0) 22 | { 23 | sb.append("0 0\n");continue; 24 | } 25 | int min = 0;int max = 0; 26 | st = new StringTokenizer(br.readLine()); 27 | for(int i = 0; i < N; i++) 28 | { 29 | if(!st.hasMoreTokens()) 30 | st = new StringTokenizer(br.readLine()); 31 | int cur = Integer.parseInt(st.nextToken()); 32 | min = Math.max(min, Math.min(cur, L - cur)); 33 | max = Math.max(max, Math.max(cur, L - cur)); 34 | } 35 | 36 | sb.append(min).append(" ").append(max).append("\n"); 37 | } 38 | System.out.print(sb); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /v107/BarCodes_UVa10721.java: -------------------------------------------------------------------------------- 1 | package v107; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class BarCodes_UVa10721 { 10 | 11 | 12 | static int m, k; 13 | static long[][] memo; 14 | static final int UNCAL = -1; 15 | 16 | public static long dp(int i, int n) 17 | { 18 | if(n <= 0) 19 | return 0; 20 | if(i==k-1) 21 | return n<=m?1:0; 22 | if(memo[i][n]!=UNCAL) 23 | return memo[i][n]; 24 | 25 | long count = 0; 26 | for(int j = 1; j <= m; j++) 27 | count += dp(i+1,n-j); 28 | return memo[i][n] = count; 29 | } 30 | 31 | public static void main(String[] args) throws IOException { 32 | 33 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 34 | PrintWriter out = new PrintWriter(System.out); 35 | 36 | 37 | while(br.ready()) 38 | { 39 | StringTokenizer st = new StringTokenizer(br.readLine()); 40 | int n = Integer.parseInt(st.nextToken()); 41 | k = Integer.parseInt(st.nextToken()); 42 | m = Integer.parseInt(st.nextToken()); 43 | memo = new long[k][n+1]; 44 | for(int i = 0; i < k; i++) 45 | Arrays.fill(memo[i], UNCAL); 46 | long sol = dp(0,n); 47 | out.println(sol); 48 | } 49 | out.flush(); 50 | out.close(); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /v108/DecodeTheTape_UVa10878.java: -------------------------------------------------------------------------------- 1 | package v108; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class DecodeTheTape_UVa10878 { 8 | 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | br.readLine(); 16 | String line; 17 | while(!(line=br.readLine()).equals("___________")) 18 | { 19 | int unicode = 0; 20 | 21 | for(int i = 0; i < 3; i++) 22 | unicode += line.charAt(line.length()-i-2)=='o'?(int)Math.pow(2, i):0; 23 | for(int i = 4; i <= 9; i++) 24 | unicode += line.charAt(line.length()-i-2)=='o'?(int)Math.pow(2, i-1):0; 25 | sb.append((char)unicode); 26 | } 27 | 28 | 29 | System.out.print(sb); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /v108/MoveTheBishop_UVa10849.java: -------------------------------------------------------------------------------- 1 | package v108; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class MoveTheBishop_UVa10849 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int tc = Integer.parseInt(br.readLine()); 16 | while(tc-->0) 17 | { 18 | br.readLine(); 19 | int t = Integer.parseInt(br.readLine()); 20 | br.readLine(); 21 | while(t-->0) 22 | { 23 | StringTokenizer st = new StringTokenizer(br.readLine()); 24 | int r1 = Integer.parseInt(st.nextToken()); 25 | int c1 = Integer.parseInt(st.nextToken()); 26 | int r2 = Integer.parseInt(st.nextToken()); 27 | int c2 = Integer.parseInt(st.nextToken()); 28 | if(r1==r2 && c1==c2) 29 | sb.append("0\n"); 30 | else 31 | if((r1+c1)%2!=(r2+c2)%2) 32 | sb.append("no move\n"); 33 | else 34 | if(Math.abs(r1-r2)==Math.abs(c1-c2)) 35 | sb.append("1\n"); 36 | else 37 | sb.append("2\n"); 38 | } 39 | 40 | } 41 | System.out.print(sb); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /v109/AddAll_UVa10954.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.util.PriorityQueue; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | public class AddAll_UVa10954 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | while(true) 17 | { 18 | int n = Integer.parseInt(br.readLine()); 19 | if(n==0) 20 | break; 21 | PriorityQueue q = new PriorityQueue(); 22 | StringTokenizer st = new StringTokenizer(br.readLine()); 23 | for(int i = 0; i < n; i++) 24 | q.add(Integer.parseInt(st.nextToken())); 25 | 26 | long cost = 0; 27 | while(q.size()>1) 28 | { 29 | int x = q.remove(); 30 | int y = q.remove(); 31 | cost += x + y; 32 | q.add(x+y); 33 | } 34 | sb.append(cost).append("\n"); 35 | } 36 | System.out.print(sb); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /v109/ChildrensGame_UVa10905.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class ChildrensGame_UVa10905 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | while(true) 17 | { 18 | int N = Integer.parseInt(br.readLine()); 19 | if(N==0) 20 | break; 21 | StringX[] numbers = new StringX[N]; 22 | StringTokenizer st = new StringTokenizer(br.readLine()); 23 | for(int i = 0; i < N; i++) 24 | numbers[i] = new StringX(st.nextToken()); 25 | Arrays.sort(numbers); 26 | while(N-->0) 27 | out.print(numbers[N].num); 28 | out.println(); 29 | } 30 | out.flush(); 31 | out.close(); 32 | 33 | } 34 | } 35 | 36 | class StringX implements Comparable 37 | { 38 | String num; 39 | 40 | StringX (String x) {num = x;} 41 | public int compareTo(StringX o) { 42 | 43 | String x = this.num; 44 | String y = o.num; 45 | int i = 0, j = 0; 46 | int a = 0, b = 0; 47 | while(i < x.length() || j < y.length()) 48 | { 49 | if(i==x.length()){i = a; b = j;} 50 | 51 | if(j==y.length()){j = b; a = i;} 52 | 53 | if(x.charAt(i)!=y.charAt(j)) 54 | return x.charAt(i) - y.charAt(j); 55 | 56 | i++;j++; 57 | } 58 | return y.length() - x.length(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /v109/HowDoYouAdd_UVa10943.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class HowDoYouAdd_UVa10943 { 10 | 11 | static final int UNCAL = -1; 12 | 13 | static int[][] memo; 14 | 15 | public static int dp(int k, int sum) 16 | { 17 | if(sum<0) 18 | return 0; 19 | if(k==0) 20 | return sum==0?1:0; 21 | if(memo[k][sum]!=UNCAL) 22 | return memo[k][sum]; 23 | int cur = 0; 24 | for(int i = 0; i <= sum; i++) 25 | cur = (cur+dp(k-1,sum-i))%1000000; 26 | return memo[k][sum] = cur; 27 | } 28 | 29 | public static void main(String[] args) throws IOException { 30 | 31 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 32 | StringBuilder sb = new StringBuilder(); 33 | while(true) 34 | { 35 | StringTokenizer st = new StringTokenizer(br.readLine()); 36 | int N = Integer.parseInt(st.nextToken()); 37 | int K = Integer.parseInt(st.nextToken()); 38 | if(N==0 && K==0) 39 | break; 40 | memo = new int[K+1][N+1]; 41 | for(int i = 0; i <= K; i++) 42 | Arrays.fill(memo[i], UNCAL); 43 | sb.append(dp(K,N)+"\n"); 44 | } 45 | System.out.print(sb); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v109/LetsPlayMagic_UVa10978.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class LetsPlayMagic_UVa10978{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int n; 16 | while((n=Integer.parseInt(br.readLine()))!=0) 17 | { 18 | String[] cards = new String[n]; int curP = -1; 19 | for(int i = 0; i < n; i++) 20 | { 21 | StringTokenizer st = new StringTokenizer(br.readLine()); 22 | String curCard = "" + st.nextToken(); 23 | int count = st.nextToken().length(); 24 | while(count>0) 25 | { 26 | curP = (curP+1)%n; 27 | if(cards[curP]==null) 28 | count--; 29 | } 30 | cards[curP] = curCard; 31 | } 32 | sb.append(cards[0]); 33 | for(int i = 1; i < n; i++) 34 | sb.append(" "+cards[i]); 35 | sb.append("\n"); 36 | } 37 | System.out.print(sb); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /v109/MarksDistribution_UVa10910.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class MarksDistribution_UVa10910 { 10 | 11 | static int[][] memo; 12 | static final int UNCAL = -1; 13 | static int N, P; 14 | 15 | public static int dp(int i, int t) 16 | { 17 | if(i==N-1) 18 | return 1; 19 | if(memo[i][t]!=UNCAL) 20 | return memo[i][t]; 21 | int count = 0;int max = t - (N-i-1)*P; 22 | for(int k = P; k <= max;k++) 23 | count += dp(i+1,t - k); 24 | return memo[i][t] = count; 25 | } 26 | 27 | public static void main(String[] args) throws IOException { 28 | 29 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 30 | PrintWriter out = new PrintWriter(System.out); 31 | StringTokenizer st; 32 | int TC = Integer.parseInt(br.readLine()); 33 | while(TC-->0) 34 | { 35 | st = new StringTokenizer(br.readLine()); 36 | N = Integer.parseInt(st.nextToken()); 37 | int T = Integer.parseInt(st.nextToken()); 38 | P = Integer.parseInt(st.nextToken()); 39 | memo = new int[N][T+1]; 40 | for(int i = 0; i < N; i++) 41 | Arrays.fill(memo[i], UNCAL); 42 | out.println(dp(0,T)); 43 | } 44 | out.flush(); 45 | out.close(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v109/MotherBear_UVa10945.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class MotherBear_UVa10945{ 8 | 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | while(true) 14 | { 15 | String sentence = br.readLine(); 16 | if(sentence.equals("DONE")) 17 | break; 18 | String r = ""; 19 | for(int i = 0; i < sentence.length(); i++) 20 | { 21 | switch(sentence.charAt(i)) 22 | { 23 | case '.': case ' ': case ',': case '!': case '?': break; 24 | default: r += sentence.charAt(i); 25 | } 26 | } 27 | r = r.toUpperCase(); 28 | if(isPalindrome(r)) 29 | sb.append("You won't be eaten!\n"); 30 | else 31 | sb.append("Uh oh..\n"); 32 | } 33 | System.out.print(sb); 34 | } 35 | 36 | 37 | 38 | public static boolean isPalindrome(String s) 39 | { 40 | 41 | if(s.length()<=1) 42 | return true; 43 | if(s.charAt(0)!=s.charAt(s.length()-1)) 44 | return false; 45 | return isPalindrome(s.substring(1,s.length()-1)); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /v109/MyDearNeighbours_UVa10928.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | import java.util.StringTokenizer; 8 | 9 | public class MyDearNeighbours_UVa10928 { 10 | 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder(); 16 | int TC = Integer.parseInt(br.readLine()); 17 | while(TC-->0) 18 | { 19 | int P = Integer.parseInt(br.readLine()); 20 | int min = P; 21 | ArrayList list = new ArrayList(); 22 | for(int i = 1; i <= P; i++) 23 | { 24 | int cur = new StringTokenizer(br.readLine()).countTokens(); 25 | if(cur < min) 26 | { 27 | list = new ArrayList(); 28 | list.add(i); 29 | min = cur; 30 | } 31 | else 32 | if(cur==min) 33 | list.add(i); 34 | } 35 | for(int i = 0; i < list.size() - 1; i++) 36 | sb.append(list.get(i)+" "); 37 | sb.append(list.get(list.size()-1)).append("\n"); 38 | if(TC!=0) 39 | br.readLine(); 40 | 41 | } 42 | System.out.print(sb); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /v109/Parity_UVa10931.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | 10 | public class Parity_UVa10931 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner sc = new Scanner(System.in); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | while(true) 17 | { 18 | int N = sc.nextInt(); 19 | if(N == 0) 20 | break; 21 | out.printf("The parity of %s is %d (mod 2).\n", Integer.toBinaryString(N), Integer.bitCount(N)); 22 | } 23 | out.flush(); 24 | out.close(); 25 | } 26 | 27 | static class Scanner 28 | { 29 | StringTokenizer st; 30 | BufferedReader br; 31 | 32 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 33 | 34 | public String next() throws IOException 35 | { 36 | while (st == null || !st.hasMoreTokens()) 37 | st = new StringTokenizer(br.readLine()); 38 | return st.nextToken(); 39 | } 40 | 41 | public int nextInt() throws IOException {return Integer.parseInt(next());} 42 | 43 | public long nextLong() throws IOException {return Long.parseLong(next());} 44 | 45 | public String nextLine() throws IOException {return br.readLine();} 46 | 47 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 48 | 49 | public boolean ready() throws IOException {return br.ready();} 50 | } 51 | } -------------------------------------------------------------------------------- /v109/Prerequisties_UVa10919.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.HashSet; 7 | import java.util.StringTokenizer; 8 | 9 | public class Prerequisties_UVa10919{ 10 | 11 | public static void main(String[] args) throws IOException 12 | { 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | 17 | while(true) 18 | { 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int k = Integer.parseInt(st.nextToken()); 21 | if(k==0) 22 | break; 23 | int m = Integer.parseInt(st.nextToken()); 24 | HashSet h = new HashSet(); 25 | st = new StringTokenizer(br.readLine()); 26 | for(int i = 0; i < k; i++) 27 | h.add(st.nextToken()); 28 | boolean possible = true; 29 | for(int i = 0; i < m; i++) 30 | { 31 | st = new StringTokenizer(br.readLine()); 32 | int c = Integer.parseInt(st.nextToken()); 33 | int r = Integer.parseInt(st.nextToken()); 34 | while(r!=0&&c>0) 35 | { 36 | String course = st.nextToken(); 37 | if(h.contains(course)) 38 | r--; 39 | c--; 40 | } 41 | if(r!=0) 42 | possible = false; 43 | } 44 | if(possible) 45 | sb.append("yes\n"); 46 | else 47 | sb.append("no\n"); 48 | 49 | } 50 | System.out.print(sb); 51 | 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /v109/RockPaperScissorsTournament_UVa10903.java: -------------------------------------------------------------------------------- 1 | package v109; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.text.DecimalFormat; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | public class RockPaperScissorsTournament_UVa10903 { 10 | 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder(); 16 | boolean first = true; 17 | while(true) 18 | { 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int n = Integer.parseInt(st.nextToken()); 21 | if(n == 0) 22 | break; 23 | if(first) 24 | first = false; 25 | else 26 | sb.append("\n"); 27 | int k = Integer.parseInt(st.nextToken()) * n * (n-1) / 2; 28 | 29 | int[] w =new int[n], l = new int[n]; 30 | while(k-->0) 31 | { 32 | st = new StringTokenizer(br.readLine()); 33 | int p1 = Integer.parseInt(st.nextToken()) - 1; 34 | char c1 = st.nextToken().charAt(0); 35 | int p2 = Integer.parseInt(st.nextToken()) - 1; 36 | char c2 = st.nextToken().charAt(0); 37 | if(c1 != c2) 38 | if(c1 == 's' && c2 == 'p' || c1 == 'p' && c2 == 'r' || c1 == 'r' && c2 == 's') {w[p1]++; l[p2]++;} 39 | else {w[p2]++; l[p1]++;} 40 | } 41 | for(int i = 0; i < n; i++) 42 | if(w[i] + l[i] == 0) sb.append("-\n"); 43 | else 44 | sb.append(new DecimalFormat("0.000").format(Math.round((1.0*w[i]/(w[i]+l[i]))*1000)/1000.0)+"\n"); 45 | } 46 | 47 | 48 | System.out.print(sb); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /v110/AGroupingProblem_UVa11026.java: -------------------------------------------------------------------------------- 1 | package v110; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class AGroupingProblem_UVa11026 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | while(true) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int N = Integer.parseInt(st.nextToken()); 18 | int M = Integer.parseInt(st.nextToken()); 19 | if(N==0) 20 | break; 21 | st = new StringTokenizer(br.readLine()); 22 | long[] val = new long[N]; 23 | for(int i = 0; i < N; i++) 24 | val[i] = Integer.parseInt(st.nextToken()); 25 | long max = 0; 26 | long[][] dp = new long[N+1][N+1]; 27 | for(int i = 0; i <= N; i++) 28 | dp[0][i] = 1; 29 | for(int k = 1; k <= N; k++) 30 | { 31 | long f = 0; 32 | for(int i = N - 1; i >= 0; i--) 33 | { 34 | long cur = (val[i] * dp[k-1][i+1])%M; 35 | f = (f + cur)%M; 36 | dp[k][i] = (cur + dp[k][i+1])%M; 37 | } 38 | max = Math.max(f, max); 39 | } 40 | 41 | sb.append(max).append("\n"); 42 | } 43 | System.out.print(sb); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /v110/AddBricksInTheWall_UVa11040.java: -------------------------------------------------------------------------------- 1 | package v110; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | 6 | public class AddBricksInTheWall_UVa11040 { 7 | 8 | public static void main(String[] args) { 9 | 10 | Scanner sc = new Scanner(System.in); 11 | PrintWriter out = new PrintWriter(System.out); 12 | int TC = sc.nextInt(); 13 | 14 | while(TC-->0) 15 | { 16 | int[][] wall = new int[9][]; 17 | for(int i = 0; i < 9; i ++) 18 | { 19 | wall[i] = new int[i+1]; 20 | if(i%2==1) 21 | continue; 22 | for(int j = 0; j <= i; j+=2) 23 | wall[i][j] = sc.nextInt(); 24 | } 25 | 26 | for(int i = 8; i > 1; i -=2) 27 | { 28 | for(int j = 1, k = 0; j < i; j+=2, k+=2) 29 | wall[i][j] = (wall[i-2][k] - (wall[i][j-1] + wall[i][j+1]))/2; 30 | for(int j = 0; j < i; j++) 31 | wall[i-1][j] = wall[i][j] + wall[i][j+1]; 32 | } 33 | 34 | for(int i = 0; i < 9; i++) 35 | { 36 | for(int j = 0; j < wall[i].length - 1; j++) 37 | out.print(wall[i][j]+" "); 38 | out.println(wall[i][wall[i].length-1]); 39 | } 40 | 41 | 42 | 43 | } 44 | 45 | out.flush(); 46 | out.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /v110/Necklace_UVa11001.java: -------------------------------------------------------------------------------- 1 | package v110; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.StringTokenizer; 7 | 8 | public class Necklace_UVa11001 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | PrintWriter out = new PrintWriter(System.out); 14 | double VT, Vo; 15 | StringTokenizer st; 16 | while(true) 17 | { 18 | st = new StringTokenizer(br.readLine()); 19 | VT = Integer.parseInt(st.nextToken()); 20 | Vo = Integer.parseInt(st.nextToken()); 21 | if(VT==0) 22 | break; 23 | double V,cur, maxLength = -1; int maxCount = 0; int k = 1;int count = 1; 24 | while(true) 25 | { 26 | V = VT / k; 27 | if(V<=Vo) 28 | break; 29 | cur = 0.3 * Math.sqrt(V-Vo)*k; 30 | if(Math.abs(cur-maxLength) <= 10e-7) 31 | { 32 | count++; 33 | } 34 | else 35 | if(cur>maxLength) 36 | { 37 | maxLength = cur; 38 | maxCount = k; 39 | count = 1; 40 | } 41 | 42 | 43 | 44 | k++; 45 | } 46 | out.println(count==1?maxCount:0); 47 | } 48 | out.flush(); 49 | out.close(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /v110/OpenCreditSystem_UVa11078.java: -------------------------------------------------------------------------------- 1 | package v110; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | 8 | public class OpenCreditSystem_UVa11078 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | int TC = Integer.parseInt(br.readLine()); 16 | while(TC-->0) 17 | { 18 | int N = Integer.parseInt(br.readLine()); 19 | int max = -1; int senior = -200000; 20 | while(N-->0) 21 | { 22 | int cur = Integer.parseInt(br.readLine()); 23 | if(senior>=cur) 24 | max = Math.max(max, senior-cur); 25 | else 26 | senior = cur; 27 | } 28 | out.println(max); 29 | } 30 | out.flush(); 31 | out.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /v111/GreyCodes_UVa11173.java: -------------------------------------------------------------------------------- 1 | package v111; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class GreyCodes_UVa11173 { 9 | 10 | public static void main(String[] args) throws NumberFormatException, IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int TC = Integer.parseInt(br.readLine()); 15 | while(TC-->0) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int n = Integer.parseInt(st.nextToken()) + 1; 19 | int k = Integer.parseInt(st.nextToken()); 20 | int res = 0; 21 | while(n-->1) 22 | if(k >= 1<i) 27 | memo[j][i] = memo[j-1][i]; 28 | else 29 | memo[j][i] = memo[j][i-value[j]] + memo[j-1][i]; 30 | 31 | while(br.ready()) 32 | { 33 | int money = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 34 | out.println(memo[20][money]); 35 | } 36 | out.flush(); 37 | out.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v111/UncleJack_UVa11115.java: -------------------------------------------------------------------------------- 1 | package v111; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintWriter; 8 | import java.math.BigInteger; 9 | import java.util.StringTokenizer; 10 | 11 | public class UncleJack_UVa11115 { 12 | 13 | public static void main(String[] args) throws IOException 14 | { 15 | Scanner sc = new Scanner(System.in); 16 | PrintWriter out = new PrintWriter(System.out); 17 | 18 | while(true) 19 | { 20 | int N = sc.nextInt(), D = sc.nextInt(); 21 | if(N + D == 0) 22 | break; 23 | out.println(BigInteger.valueOf(N).pow(D)); 24 | } 25 | out.flush(); 26 | out.close(); 27 | } 28 | 29 | static class Scanner 30 | { 31 | StringTokenizer st; 32 | BufferedReader br; 33 | 34 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 35 | 36 | public String next() throws IOException 37 | { 38 | while (st == null || !st.hasMoreTokens()) 39 | st = new StringTokenizer(br.readLine()); 40 | return st.nextToken(); 41 | } 42 | 43 | public int nextInt() throws IOException {return Integer.parseInt(next());} 44 | 45 | public long nextLong() throws IOException {return Long.parseLong(next());} 46 | 47 | public String nextLine() throws IOException {return br.readLine();} 48 | 49 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 50 | 51 | public boolean ready() throws IOException {return br.ready();} 52 | } 53 | } -------------------------------------------------------------------------------- /v112/CoinCollector_UVa11246.java: -------------------------------------------------------------------------------- 1 | package v112; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class CoinCollector_UVa11246 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | int TC = Integer.parseInt(br.readLine()); 16 | while(TC-->0) 17 | { 18 | int N = Integer.parseInt(br.readLine()); 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int[] value = new int[N]; 21 | for(int i = 0; i < N; i++) 22 | value[i] = Integer.parseInt(st.nextToken()); 23 | Arrays.sort(value); 24 | 25 | long sum = 0; 26 | int count = 1; 27 | for(int i = 0; i < N - 1; i++) 28 | { 29 | if(value[i] + sum < value[i+1]) 30 | { 31 | count++; 32 | sum += value[i]; 33 | } 34 | } 35 | sb.append(count+"\n"); 36 | } 37 | System.out.print(sb); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /v112/CoinCollector_UVa11264.java: -------------------------------------------------------------------------------- 1 | package v112; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | 9 | public class CoinCollector_UVa11264 { 10 | 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder(); 16 | 17 | int tc = Integer.parseInt(br.readLine()); 18 | while(tc-->0) 19 | { 20 | int n = Integer.parseInt(br.readLine()); 21 | int[] coins = new int[n]; 22 | StringTokenizer st = new StringTokenizer(br.readLine()); 23 | for(int i = 0; i < n; i++) 24 | coins[i] = Integer.parseInt(st.nextToken()); 25 | int currentSum = 0; 26 | int maxCoins = 0; 27 | for(int i = 0; i < n; i++) 28 | { 29 | if(coins[i]<=currentSum) 30 | continue; 31 | maxCoins++; 32 | currentSum += coins[i]; 33 | 34 | } 35 | sb.append(maxCoins+"\n"); 36 | } 37 | 38 | System.out.print(sb); 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /v112/GroceryStore_UVa11236.java: -------------------------------------------------------------------------------- 1 | package v112; 2 | 3 | 4 | import java.io.IOException; 5 | import java.text.DecimalFormat; 6 | public class GroceryStore_UVa11236{ 7 | 8 | public static void main(String[] args) throws IOException 9 | { 10 | 11 | StringBuilder sb = new StringBuilder(); 12 | 13 | for(int a = 1; a * 4 <= 2000; a ++) 14 | for(int b = a ; a + b * 3<= 2000; b ++) 15 | for(int c = b ; a + b + c * 2 <= 2000; c ++) 16 | { 17 | if(a*b*c <=1000000) 18 | continue; 19 | int d = (a+b+c)*1000000/(a*b*c-1000000); 20 | if(d>=c && a+b+c+d <= 2000 && (a+b+c+d)*1000000 == a*b*c*d) 21 | { 22 | sb.append(new DecimalFormat("0.00").format(a/100.0) + " "+ new DecimalFormat("0.00").format(b/100.0)+ " " + new DecimalFormat("0.00").format(c/100.0)+ " " + new DecimalFormat("0.00").format(d/100.0)+"\n"); 23 | } 24 | 25 | } 26 | 27 | System.out.print(sb); 28 | 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /v112/TarotScores_UVa11225.java: -------------------------------------------------------------------------------- 1 | package v112; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.StringTokenizer; 7 | import java.util.TreeMap; 8 | 9 | public class TarotScores_UVa11225 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | TreeMap map = new TreeMap(); 16 | map.put("king", 4);map.put("fool", 4);map.put("one", 4);map.put("jack", 1); 17 | map.put("twenty-one", 4);map.put("queen", 3);map.put("knight", 2); 18 | int[] x = new int[]{56,51,41,36}; 19 | 20 | int TC = Integer.parseInt(br.readLine()); 21 | for(int k = 1; k <= TC; k++) 22 | { 23 | int N = Integer.parseInt(br.readLine()); 24 | int score = N/2; 25 | int target = 0; 26 | while(N-->0) 27 | { 28 | String card = new StringTokenizer(br.readLine()).nextToken(); 29 | if(map.containsKey(card)) 30 | { 31 | score += map.get(card); 32 | if(card.equals("fool") || card.equals("twenty-one") || card.equals("one")) 33 | target++; 34 | } 35 | } 36 | out.printf("Hand #%d\n",k); 37 | if(score < x[target]) 38 | out.printf("Game lost by %d point(s).\n",x[target]-score); 39 | else 40 | out.printf("Game won by %d point(s).\n",score-x[target]); 41 | if(k!=TC) 42 | out.println(); 43 | } 44 | out.flush(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /v113/ClawDecomposition_UVa11396.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | 3 | 4 | import java.util.*; 5 | import java.io.*; 6 | 7 | public class ClawDecomposition_UVa11396 { 8 | 9 | static boolean[][] adjMatrix; 10 | static int[] visited; 11 | static int N; 12 | 13 | 14 | public static boolean bfs(int u) 15 | { 16 | visited[u] = 1; 17 | LinkedList q = new LinkedList(); 18 | q.add(u); 19 | while(!q.isEmpty()) 20 | { 21 | u = q.remove(); 22 | for(int i = 0; i < N; i++) 23 | { 24 | if(!adjMatrix[u][i]) 25 | continue; 26 | if(visited[i]!=0) 27 | if(visited[i]==visited[u]) 28 | return false; 29 | else 30 | continue; 31 | else 32 | { 33 | visited[i] = -visited[u]; 34 | q.add(i); 35 | } 36 | } 37 | } 38 | return true; 39 | } 40 | public static void main(String[] args) throws NumberFormatException, IOException { 41 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 42 | StringBuilder sb = new StringBuilder(); 43 | while(true) 44 | { 45 | N = Integer.parseInt(br.readLine()); 46 | if(N==0) 47 | break; 48 | adjMatrix = new boolean[N][N]; 49 | while(true) 50 | { 51 | StringTokenizer st = new StringTokenizer(br.readLine()); 52 | int u = Integer.parseInt(st.nextToken()) - 1; 53 | int v = Integer.parseInt(st.nextToken()) - 1; 54 | if(u==-1) 55 | break; 56 | adjMatrix[u][v] = true; 57 | adjMatrix[v][u] = true; 58 | } 59 | visited = new int[N]; 60 | 61 | sb.append(bfs(0)?"YES":"NO").append("\n"); 62 | 63 | } 64 | System.out.print(sb); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /v113/CountingChaos_UVa11309.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.text.DecimalFormat; 7 | 8 | public class CountingChaos_UVa11309{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 0; i < t; i++) 16 | { 17 | String s = br.readLine(); 18 | sb.append(nextTime(incrementTime(s))+"\n"); 19 | } 20 | System.out.print(sb); 21 | 22 | } 23 | 24 | public static String nextTime(String s) 25 | { 26 | String tmp = "" + Integer.parseInt(s.substring(0,2) + s.substring(3)); 27 | if(isPalindrome(tmp)) 28 | return s; 29 | return nextTime(incrementTime(s)); 30 | 31 | } 32 | 33 | public static String incrementTime(String s) 34 | { 35 | int hours = Integer.parseInt(s.substring(0,2)); 36 | int minutes = Integer.parseInt(s.substring(3)); 37 | minutes++; 38 | if(minutes==60) 39 | { 40 | minutes = 0; 41 | hours = (hours+1)%24; 42 | 43 | 44 | } 45 | return new DecimalFormat("00").format(hours) + ":" + new DecimalFormat("00").format(minutes); 46 | } 47 | 48 | public static boolean isPalindrome(String s) 49 | { 50 | if(s.length()<=1) 51 | return true; 52 | if(s.charAt(0)!=s.charAt(s.length()-1)) 53 | return false; 54 | return isPalindrome(s.substring(1,s.length()-1)); 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /v113/Newspaper_UVa11340.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.text.DecimalFormat; 7 | import java.util.HashMap; 8 | import java.util.StringTokenizer; 9 | 10 | public class Newspaper_UVa11340{ 11 | 12 | public static void main(String[] args) throws IOException 13 | { 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder();StringTokenizer st; 16 | int t = Integer.parseInt(br.readLine()); 17 | for(int i = 1; i <= t; i++) 18 | { 19 | int k = Integer.parseInt(br.readLine()); 20 | HashMap h = new HashMap(); 21 | for(int j = 0; j < k; j++) 22 | { 23 | st = new StringTokenizer(br.readLine()); 24 | h.put(st.nextToken().charAt(0),Integer.parseInt(st.nextToken())); 25 | } 26 | int m = Integer.parseInt(br.readLine()); 27 | int cost = 0; 28 | for(int j = 0; j < m; j++) 29 | { 30 | String s = br.readLine(); 31 | for(int x = 0; x < s.length(); x++) 32 | { 33 | Integer z = h.get(s.charAt(x)); 34 | if(z!=null) 35 | cost += z; 36 | } 37 | 38 | } 39 | 40 | sb.append(new DecimalFormat("0.00").format(cost/100.0)+"$\n"); 41 | } 42 | System.out.print(sb); 43 | } 44 | 45 | 46 | 47 | } -------------------------------------------------------------------------------- /v113/PhoneList_UVa11362.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | 3 | 4 | 5 | import java.io.BufferedReader; 6 | import java.io.IOException; 7 | import java.io.InputStreamReader; 8 | import java.util.Arrays; 9 | 10 | public class PhoneList_UVa11362 { 11 | 12 | public static void main(String[] args) throws IOException{ 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | int t = Integer.parseInt(br.readLine()); 15 | StringBuilder sb = new StringBuilder(); 16 | while(t-->0) 17 | { 18 | int n = Integer.parseInt(br.readLine()); 19 | String[] num = new String[n]; 20 | for(int i = 0; i < n; i++) 21 | num[i] = br.readLine(); 22 | 23 | Arrays.sort(num); 24 | boolean yes = true; 25 | for(int i = 0; i < n - 1 && yes; i++) 26 | if(num[i].length() <= num[i+1].length() && num[i].equals(num[i+1].substring(0, num[i].length()))) 27 | yes = false; 28 | sb.append(yes?"YES\n":"NO\n"); 29 | 30 | } 31 | System.out.print(sb); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /v113/Shopaholic_UVa11369.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | 3 | 4 | import java.io.BufferedReader; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.util.Arrays; 8 | import java.util.StringTokenizer; 9 | 10 | public class Shopaholic_UVa11369 { 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | StringBuilder sb = new StringBuilder(); 16 | int tc = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 17 | while(tc-->0) 18 | { 19 | int n = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 20 | int[] p = new int[n]; 21 | StringTokenizer st= new StringTokenizer(br.readLine()); 22 | for(int i = 0; i < n; i++) 23 | p[i] = Integer.parseInt(st.nextToken()); 24 | Arrays.sort(p); 25 | int max = 0; 26 | for(int i = n - 3; i>= 0; i -= 3) 27 | max += p[i]; 28 | sb.append(max+"\n"); 29 | } 30 | 31 | System.out.print(sb); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /v113/SymmetricMatrix_UVa11349.java: -------------------------------------------------------------------------------- 1 | package v113; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.util.StringTokenizer; 7 | 8 | public class SymmetricMatrix_UVa11349 { 9 | 10 | static int N; static long matrix[][]; 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | PrintWriter out = new PrintWriter(System.out); 16 | int TC = Integer.parseInt(br.readLine()); 17 | StringTokenizer st; 18 | for(int k = 1; k <= TC; k++) 19 | { 20 | st = new StringTokenizer(br.readLine()); 21 | st.nextToken();st.nextToken(); 22 | N = Integer.parseInt(st.nextToken()); 23 | boolean flag = true; 24 | matrix = new long[N][N]; 25 | for(int i = 0; i < N; i++) 26 | { 27 | st = new StringTokenizer(br.readLine()); 28 | for(int j = 0; j < N; j++) 29 | { 30 | matrix[i][j] = Long.parseLong(st.nextToken()); 31 | if(matrix[i][j]<0) 32 | flag = false; 33 | } 34 | } 35 | out.printf("Test #%d: %s.\n",k,flag&&check()?"Symmetric":"Non-symmetric"); 36 | } 37 | out.flush(); 38 | out.close(); 39 | } 40 | 41 | public static boolean check() 42 | { 43 | int x,y; 44 | for(int i = 0; i < (N + 1) >> 1; i++) 45 | for(int j = 0; j < N; j++) 46 | { 47 | x = N - 1 - i; 48 | y = N - 1 - j; 49 | if(matrix[x][y]!=matrix[i][j]) 50 | return false; 51 | } 52 | 53 | return true; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /v114/ChestOfDrawers_UVa11420.java: -------------------------------------------------------------------------------- 1 | package v114; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.math.BigInteger; 8 | import java.util.StringTokenizer; 9 | 10 | public class ChestOfDrawers_UVa11420 { 11 | 12 | static BigInteger[][][] memo; 13 | static int N; 14 | 15 | static BigInteger dp(int top, int idx, int rem) 16 | { 17 | if(rem < 0) 18 | return BigInteger.ZERO; 19 | if(idx==N) 20 | if(rem==0) 21 | return BigInteger.ONE; 22 | else 23 | return BigInteger.ZERO; 24 | if(memo[top][idx][rem]!=null) 25 | return memo[top][idx][rem]; 26 | if(top==1) 27 | memo[top][idx][rem] = dp(1,idx+1,rem-1).add(dp(0,idx+1,rem)); 28 | else 29 | memo[top][idx][rem] = dp(1,idx+1,rem).add(dp(0,idx+1,rem)); 30 | return memo[top][idx][rem]; 31 | } 32 | 33 | public static void main(String[] args) throws IOException { 34 | 35 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 36 | PrintWriter out = new PrintWriter(System.out); 37 | 38 | while(true) 39 | { 40 | StringTokenizer st= new StringTokenizer(br.readLine()); 41 | N = Integer.parseInt(st.nextToken()); 42 | int S = Integer.parseInt(st.nextToken()); 43 | if(N<0) 44 | break; 45 | memo = new BigInteger[2][N+10][S+10]; 46 | out.println(dp(1,0,S).toString()); 47 | } 48 | 49 | out.flush(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /v114/MusicalLoop_UVa11496.java: -------------------------------------------------------------------------------- 1 | package v114; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class MusicalLoop_UVa11496{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder();StringTokenizer st; 14 | int n; 15 | while((n = Integer.parseInt(br.readLine()))!=0) 16 | { 17 | st = new StringTokenizer(br.readLine()); 18 | int[] x = new int[n];int count = 0; 19 | for(int i = 0; i < n; i++) 20 | x[i] = Integer.parseInt(st.nextToken()); 21 | for(int i = 0; i < n; i++) 22 | { 23 | int pre = i - 1; 24 | if(pre<0) 25 | pre = n - 1; 26 | int next = (i+1)%n; 27 | if(x[i]>x[pre]&&x[i]>x[next]||x[i]=n) 35 | enoughBeds = true; 36 | } 37 | if(enoughBeds&&affordable) 38 | { 39 | if(r.equals("stay home")||Integer.parseInt(r)>cost) 40 | r = ""+cost; 41 | } 42 | } 43 | sb.append(r+"\n"); 44 | 45 | } 46 | System.out.print(sb); 47 | 48 | 49 | 50 | } 51 | 52 | 53 | 54 | } -------------------------------------------------------------------------------- /v115/MoliuNumberGenerator_UVa11567.java: -------------------------------------------------------------------------------- 1 | package v115; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | import java.util.Scanner; 6 | 7 | public class MoliuNumberGenerator_UVa11567 { 8 | 9 | public static void main(String[] args) throws IOException { 10 | 11 | Scanner sc = new Scanner(System.in); 12 | PrintWriter out = new PrintWriter(System.out); 13 | 14 | while(sc.hasNext()) 15 | { 16 | long s = sc.nextLong(), ans = 0; 17 | boolean up = false; 18 | 19 | while(s > 1) 20 | { 21 | 22 | boolean oldUp = up; 23 | if((s & 1) == 1) 24 | { 25 | if(up) 26 | { 27 | up = false; 28 | --s; 29 | } 30 | else 31 | if(s == 3) 32 | { 33 | --s; 34 | ++ans; 35 | } 36 | else 37 | { 38 | up = true; 39 | ++ans; 40 | ++s; 41 | } 42 | } 43 | if(up == oldUp) 44 | up = false; 45 | ++ans; 46 | s >>= 1; 47 | 48 | } 49 | 50 | out.println(ans + s); 51 | 52 | } 53 | 54 | out.flush(); 55 | out.close(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /v115/SimpleEquations_UVa11565.java: -------------------------------------------------------------------------------- 1 | package v115; 2 | 3 | 4 | 5 | import java.io.BufferedReader; 6 | import java.io.IOException; 7 | import java.io.InputStreamReader; 8 | import java.util.StringTokenizer; 9 | public class SimpleEquations_UVa11565{ 10 | 11 | public static void main(String[] args) throws IOException 12 | { 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | int t = Integer.parseInt(br.readLine()); 17 | for(int k = 0; k < t; k++) 18 | { 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int A = Integer.parseInt(st.nextToken()); 21 | int B = Integer.parseInt(st.nextToken()); 22 | int C = Integer.parseInt(st.nextToken()); 23 | boolean found = false; 24 | for(int a = -57; a * a * 3 <= 10000 && !found; a++) 25 | for(int b = a + 1; a * a + b * b * 2 <= 10000 && !found; b++) 26 | for(int c = b + 1; a * a+ b * b + c * c <= 10000 && !found; c++) 27 | if(a+b+c==A && a*b*c==B && a*a+b*b+c*c==C) 28 | { 29 | found = true; 30 | sb.append(a+" "+b+" "+c+"\n"); 31 | } 32 | 33 | if(!found) 34 | sb.append("No solution.\n"); 35 | } 36 | 37 | 38 | System.out.print(sb); 39 | 40 | 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /v115/TrainTracks_UVa11586.java: -------------------------------------------------------------------------------- 1 | package v115; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class TrainTracks_UVa11586{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 0; i < t; i++) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int tokens = st.countTokens(); 19 | if(tokens==1) 20 | { 21 | sb.append("NO LOOP\n");continue; 22 | } 23 | int m = 0; 24 | int f = 0; 25 | for(int j = 0; j < tokens; j++) 26 | { 27 | String s = st.nextToken(); 28 | if(s.charAt(0)=='M') 29 | m++; 30 | else 31 | f++; 32 | if(s.charAt(1)=='M') 33 | m++; 34 | else 35 | f++; 36 | } 37 | if(m==f&&m==tokens) 38 | sb.append("LOOP\n"); 39 | else 40 | sb.append("NO LOOP\n"); 41 | } 42 | System.out.print(sb); 43 | 44 | 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /v115/UniqueSnowflakes_UVa11572.java: -------------------------------------------------------------------------------- 1 | package v115; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.util.StringTokenizer; 6 | import java.util.TreeMap; 7 | 8 | 9 | public class UniqueSnowflakes_UVa11572 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | int TC = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 16 | while(TC-->0) 17 | { 18 | int N = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 19 | TreeMap map = new TreeMap(); 20 | int first = 1; int last = 1;int max = 0; 21 | while(N-->0) 22 | { 23 | int cur = Integer.parseInt(new StringTokenizer(br.readLine()).nextToken()); 24 | if(map.containsKey(cur) && map.get(cur)>=first) 25 | { 26 | max = Math.max(last-first,max); 27 | first = map.get(cur) + 1; 28 | map.put(cur, last++); 29 | } 30 | else 31 | map.put(cur, last++); 32 | 33 | 34 | } 35 | max = Math.max(last-first, max); 36 | sb.append(max+"\n"); 37 | } 38 | 39 | System.out.print(sb); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /v116/AlarmClock_UVa11677.java: -------------------------------------------------------------------------------- 1 | package v116; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class AlarmClock_UVa11677 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | while(true) 16 | { 17 | int h1 = sc.nextInt(), m1 = sc.nextInt(), h2 = sc.nextInt(), m2 = sc.nextInt(); 18 | if(h1 + h2 + m1 + m2 == 0) 19 | break; 20 | int t1 = h1 * 60 + m1, t2 = h2 * 60 + m2; 21 | out.println((t2 - t1 + 60 * 24) % (60 * 24)); 22 | } 23 | out.flush(); 24 | out.close(); 25 | } 26 | 27 | static class Scanner 28 | { 29 | StringTokenizer st; 30 | BufferedReader br; 31 | 32 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 33 | 34 | public String next() throws IOException 35 | { 36 | while (st == null || !st.hasMoreTokens()) 37 | st = new StringTokenizer(br.readLine()); 38 | return st.nextToken(); 39 | } 40 | 41 | public int nextInt() throws IOException {return Integer.parseInt(next());} 42 | 43 | public long nextLong() throws IOException {return Long.parseLong(next());} 44 | 45 | public String nextLine() throws IOException {return br.readLine();} 46 | 47 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 48 | 49 | public boolean ready() throws IOException {return br.ready();} 50 | } 51 | } -------------------------------------------------------------------------------- /v116/BurgerTime_UVa11661.java: -------------------------------------------------------------------------------- 1 | package v116; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class BurgerTime_UVa11661{ 8 | 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | int l = Integer.parseInt(br.readLine()); 15 | while(l!=0) 16 | { 17 | String s = br.readLine(); 18 | int minD = l; 19 | int curD = 0; 20 | char lastP = '.'; 21 | for(int i = 0; i < l; i++) 22 | { 23 | char curP = s.charAt(i); 24 | if(curP=='Z') 25 | { 26 | minD = 0;break; 27 | } 28 | if(curP=='R') 29 | { 30 | if(lastP=='D') 31 | if(curD (1<0) 43 | { 44 | StringTokenizer st = new StringTokenizer(br.readLine()); 45 | N = Integer.parseInt(st.nextToken()); 46 | A = Integer.parseInt(st.nextToken()); 47 | B = Integer.parseInt(st.nextToken()); 48 | c1 = c2 = 0; 49 | 50 | find(1); 51 | 52 | sb.append((1<pre) 29 | { 30 | turns = turns + cur - pre; 31 | } 32 | pre = cur; 33 | } 34 | sb.append(turns+"\n"); 35 | 36 | } 37 | System.out.print(sb); 38 | 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /v116/MirrorClock_UVa11650.java: -------------------------------------------------------------------------------- 1 | package v116; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | 6 | 7 | public class MirrorClock_UVa11650 { 8 | 9 | public static void main(String[] args) throws NumberFormatException, IOException { 10 | 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | 13 | int tc = Integer.parseInt(br.readLine()); 14 | while(tc-->0) 15 | { 16 | 17 | String[] time = br.readLine().split("\\:"); 18 | int h = Integer.parseInt(time[0]), m = Integer.parseInt(time[1]); 19 | 20 | h = (11 - h + 12)%12; 21 | if(m != 0) m = 60 - m; 22 | else h = (h+1)%12; 23 | if(h==0) h = 12; 24 | System.out.printf("%02d:%02d\n",h,m); 25 | 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /v116/NoProblem_UVa11608.java: -------------------------------------------------------------------------------- 1 | package v116; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class NoProblem_UVa11608{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int curN; int k = 1; 16 | 17 | while((curN=Integer.parseInt(br.readLine()))>-1) 18 | { 19 | sb.append("Case "+k+++":\n"); 20 | StringTokenizer s1 = new StringTokenizer(br.readLine()); 21 | StringTokenizer s2 = new StringTokenizer(br.readLine()); 22 | for(int i = 0; i < 12; i++) 23 | { 24 | int required = Integer.parseInt(s2.nextToken()); 25 | if(curN>=required) 26 | { 27 | sb.append("No problem! :D\n"); curN = curN - required; 28 | } 29 | else 30 | sb.append("No problem. :(\n"); 31 | 32 | curN += Integer.parseInt(s1.nextToken()); 33 | } 34 | } 35 | System.out.print(sb); 36 | 37 | 38 | } 39 | 40 | 41 | } -------------------------------------------------------------------------------- /v116/SubPrimeUVa11679.java: -------------------------------------------------------------------------------- 1 | package v116; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class SubPrimeUVa11679 { 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringTokenizer st; 14 | StringBuilder sb = new StringBuilder(); 15 | while(br.ready()) 16 | { 17 | st = new StringTokenizer(br.readLine()); 18 | int b = Integer.parseInt(st.nextToken()); 19 | int n = Integer.parseInt(st.nextToken()); 20 | if(b==0) 21 | break; 22 | st = new StringTokenizer(br.readLine()); 23 | int[] reserves = new int[b]; 24 | for(int i = 0; i < b; i++) 25 | reserves[i] = Integer.parseInt(st.nextToken()); 26 | boolean possible = true; 27 | for(int i = 0; i < n; i++) 28 | { 29 | st = new StringTokenizer(br.readLine()); 30 | int db = Integer.parseInt(st.nextToken())-1; 31 | int cb = Integer.parseInt(st.nextToken())-1; 32 | int v = Integer.parseInt(st.nextToken()); 33 | reserves[db] -= v;reserves[cb] += v; 34 | } 35 | for(int i = 0; i < b; i++) 36 | if(reserves[i]<0) 37 | { 38 | possible = false;break; 39 | } 40 | if(possible) 41 | sb.append("S\n"); 42 | else 43 | sb.append("N\n"); 44 | } 45 | System.out.print(sb); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v117/AutomateTheGrades_UVa11777.java: -------------------------------------------------------------------------------- 1 | package v117; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | 6 | public class AutomateTheGrades_UVa11777 { 7 | 8 | public static void main(String[] args) { 9 | 10 | Scanner sc = new Scanner(System.in); 11 | PrintWriter out = new PrintWriter(System.out); 12 | int tc = sc.nextInt(); 13 | char[] grade = new char[101]; 14 | for(int i = 0; i < 60; i++) 15 | grade[i] = 'F'; 16 | for(int i = 60; i < 70; i++) 17 | grade[i] = 'D'; 18 | for(int i = 70; i < 80; i++) 19 | grade[i] = 'C'; 20 | for(int i = 80; i < 90; i++) 21 | grade[i] = 'B'; 22 | for(int i = 90; i < 101; i++) 23 | grade[i] = 'A'; 24 | 25 | for(int i = 1; i <= tc; i++) 26 | { 27 | int f = sc.nextInt(); 28 | int t1 = sc.nextInt(); int t2 = sc.nextInt(); 29 | int att = sc.nextInt(); 30 | int[] tests = new int[]{sc.nextInt(),sc.nextInt(),sc.nextInt()}; 31 | int max1 = 0, max2 = 0; 32 | if(tests[0]>tests[1]) 33 | { 34 | max1 = tests[0]; 35 | if(tests[1]>tests[2]) 36 | max2 = tests[1]; 37 | else 38 | max2 = tests[2]; 39 | } 40 | else 41 | { 42 | max1 = tests[1]; 43 | if(tests[0]>tests[2]) 44 | max2 = tests[0]; 45 | else 46 | max2 = tests[2]; 47 | } 48 | max1 = (max1+max2)/2; 49 | int sum = f + t1 + t2 + att + max1; 50 | out.printf("Case %d: %c\n",i,grade[sum]); 51 | } 52 | out.flush(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /v117/HorrorDash_UVa11799.java: -------------------------------------------------------------------------------- 1 | package v117; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class HorrorDash_UVa11799{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 1; i <= t; i++) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int n = Integer.parseInt(st.nextToken()); 19 | int min = Integer.parseInt(st.nextToken()); 20 | for(int j = 1; j < n; j++) 21 | { 22 | int cur = Integer.parseInt(st.nextToken()); 23 | if(cur>min) 24 | min = cur; 25 | } 26 | sb.append("Case "+i+": "+min+"\n"); 27 | } 28 | System.out.print(sb); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /v117/JumpingMario_UVa11764.java: -------------------------------------------------------------------------------- 1 | package v117; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class JumpingMario_UVa11764{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 0; i < t; i++) 16 | { 17 | int n = Integer.parseInt(br.readLine());int ups = 0; int downs = 0; 18 | StringTokenizer st = new StringTokenizer(br.readLine()); 19 | int current = Integer.parseInt(st.nextToken()); 20 | for(int j = 0; j < n-1; j++) 21 | { 22 | int next = Integer.parseInt(st.nextToken()); 23 | if(next>current) 24 | ups++; 25 | else 26 | if(next200) 24 | break; 25 | if(i==n-1&&(1422-stations[i])*2<=200) 26 | sb.append("POSSIBLE\n"); 27 | else 28 | sb.append("IMPOSSIBLE\n"); 29 | } 30 | 31 | System.out.print(sb); 32 | 33 | 34 | } 35 | 36 | 37 | } -------------------------------------------------------------------------------- /v118/Multipleof17_UVa11879.java: -------------------------------------------------------------------------------- 1 | package v118; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.math.BigInteger; 8 | import java.util.StringTokenizer; 9 | 10 | public class Multipleof17_UVa11879 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner sc = new Scanner(System.in); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | BigInteger m = BigInteger.valueOf(17); 17 | while(true) 18 | { 19 | String n = sc.next(); 20 | if(n.equals("0")) 21 | break; 22 | out.println(new BigInteger(n).mod(m).compareTo(BigInteger.ZERO) == 0 ? 1 : 0); 23 | } 24 | out.flush(); 25 | out.close(); 26 | } 27 | 28 | static class Scanner 29 | { 30 | StringTokenizer st; 31 | BufferedReader br; 32 | 33 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 34 | 35 | public String next() throws IOException 36 | { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine()); 39 | return st.nextToken(); 40 | } 41 | 42 | public int nextInt() throws IOException {return Integer.parseInt(next());} 43 | 44 | public long nextLong() throws IOException {return Long.parseLong(next());} 45 | 46 | public String nextLine() throws IOException {return br.readLine();} 47 | 48 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 49 | 50 | public boolean ready() throws IOException {return br.ready();} 51 | } 52 | } -------------------------------------------------------------------------------- /v119/AChangeinThermalUnit_UVa11984.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class AChangeinThermalUnit_UVa11984 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | Scanner sc = new Scanner(System.in); 13 | PrintWriter out = new PrintWriter(System.out); 14 | 15 | int tc = sc.nextInt(); 16 | for(int t = 1; t <= tc; ++t) 17 | out.printf("Case %d: %.2f\n", t, sc.nextInt() + sc.nextInt() * 5 / 9.0); 18 | out.flush(); 19 | out.close(); 20 | } 21 | 22 | static class Scanner 23 | { 24 | StringTokenizer st; 25 | BufferedReader br; 26 | 27 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 28 | 29 | public String next() throws IOException 30 | { 31 | while (st == null || !st.hasMoreTokens()) 32 | st = new StringTokenizer(br.readLine()); 33 | return st.nextToken(); 34 | } 35 | 36 | public int nextInt() throws IOException {return Integer.parseInt(next());} 37 | 38 | public long nextLong() throws IOException {return Long.parseLong(next());} 39 | 40 | public String nextLine() throws IOException {return br.readLine();} 41 | 42 | public double nextDouble() throws IOException { return Double.parseDouble(next()); } 43 | 44 | public boolean ready() throws IOException {return br.ready();} 45 | } 46 | } -------------------------------------------------------------------------------- /v119/Battleships_UVa11953.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Battleships_UVa11953 { 8 | 9 | static char[][] grid; 10 | static int[] dx = new int[]{0,0,1,-1}; 11 | static int[] dy = new int[]{1,-1,0,0}; 12 | static int N; 13 | 14 | static boolean dfs(int i, int j) 15 | { 16 | boolean flag = grid[i][j] == 'x'; 17 | grid[i][j] = '.'; 18 | for(int k = 0; k < 4; k++) 19 | { 20 | int x = i + dx[k]; 21 | int y = j + dy[k]; 22 | if(valid(x,y)) 23 | flag = dfs(x,y) || flag; 24 | } 25 | return flag; 26 | } 27 | 28 | static boolean valid(int i, int j) 29 | { 30 | if(i == -1 || j == -1 || i == N || j == N || grid[i][j] =='.') 31 | return false; 32 | return true; 33 | } 34 | public static void main(String[] args) throws NumberFormatException, IOException { 35 | 36 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 37 | StringBuilder sb = new StringBuilder(); 38 | int tc = Integer.parseInt(br.readLine()); 39 | for(int t = 1; t <= tc; t++) 40 | { 41 | N = Integer.parseInt(br.readLine()); 42 | grid = new char[N][]; 43 | for(int i = 0; i < N; i++) 44 | grid[i] = br.readLine().toCharArray(); 45 | 46 | int count = 0; 47 | for(int i = 0; i < N; i++) 48 | for(int j = 0; j < N; j++) 49 | if(grid[i][j] != '.' && dfs(i,j)) 50 | count++; 51 | sb.append("Case "+t+": "+count+"\n"); 52 | } 53 | System.out.print(sb); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /v119/BrokenKeyboard_UVa11988.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.LinkedList; 8 | 9 | public class BrokenKeyboard_UVa11988 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | while(br.ready()) 16 | { 17 | LinkedList words = new LinkedList(); 18 | String line = br.readLine(); 19 | int i = 0, j = 0;char last = ']'; 20 | while(j < line.length()) 21 | { 22 | if(line.charAt(j)=='[') 23 | { 24 | String word = line.substring(i,j); 25 | if(last==']') 26 | words.addLast(word); 27 | else 28 | words.addFirst(word); 29 | last = '['; 30 | i = j + 1; 31 | } 32 | else 33 | if(line.charAt(j)==']') 34 | { 35 | String word = line.substring(i,j); 36 | if(last==']') 37 | words.addLast(word); 38 | else 39 | words.addFirst(word); 40 | last = ']'; 41 | i = j + 1; 42 | } 43 | j++; 44 | } 45 | String word = line.substring(i,j); 46 | if(last==']') 47 | words.addLast(word); 48 | else 49 | words.addFirst(word); 50 | StringBuilder sb = new StringBuilder(); 51 | while(!words.isEmpty()) 52 | sb.append(words.remove()); 53 | out.println(sb); 54 | } 55 | out.flush(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /v119/DoYourOwnHomework_UVa11917.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | import java.util.TreeMap; 9 | 10 | public class DoYourOwnHomework_UVa11917 { 11 | 12 | public static void main(String[] args) throws NumberFormatException, IOException { 13 | 14 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | PrintWriter out = new PrintWriter(System.out); 16 | StringTokenizer st; 17 | int TC = Integer.parseInt(br.readLine()); 18 | for(int k = 1; k <= TC; k++) 19 | { 20 | TreeMap map = new TreeMap(); 21 | int N = Integer.parseInt(br.readLine()); 22 | while(N-->0) 23 | { 24 | st = new StringTokenizer(br.readLine()); 25 | map.put(st.nextToken(), Integer.parseInt(st.nextToken())); 26 | } 27 | int D = Integer.parseInt(br.readLine()); 28 | String s = br.readLine(); 29 | if(!map.containsKey(s)) 30 | { 31 | out.printf("Case %d: Do your own homework!\n",k);continue; 32 | } 33 | int X = map.get(s); 34 | if(X<=D) 35 | out.printf("Case %d: Yesss\n",k); 36 | else 37 | if(X <= D + 5) 38 | out.printf("Case %d: Late\n",k); 39 | else 40 | out.printf("Case %d: Do your own homework!\n",k); 41 | } 42 | out.flush(); 43 | out.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /v119/LumberjackSequencing_UVa11942.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class LumberjackSequencing_UVa11942{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder("Lumberjacks:\n"); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 0; i < t; i++) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int first = Integer.parseInt(st.nextToken()); 19 | int second = Integer.parseInt(st.nextToken()); 20 | boolean increasing; 21 | if(firstprevious) 30 | { 31 | if(!increasing) break; 32 | } 33 | else 34 | if(increasing) break; 35 | } 36 | if(j==10) 37 | sb.append("Ordered\n"); 38 | else 39 | sb.append("Unordered\n"); 40 | } 41 | 42 | System.out.print(sb); 43 | 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /v119/SplittingNumbers_UVa11933.java: -------------------------------------------------------------------------------- 1 | package v119; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | 7 | public class SplittingNumbers_UVa11933 { 8 | 9 | public static void main(String[] args) throws NumberFormatException, IOException { 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | PrintWriter out = new PrintWriter(System.out); 12 | 13 | while(true) 14 | { 15 | int n = Integer.parseInt(br.readLine()); 16 | if(n==0) 17 | break; 18 | int a = 0, b = 0; 19 | int count = 0; 20 | while(n!=0) 21 | { 22 | int low = n & (-n); 23 | if(count++%2==0) 24 | a |= low; 25 | else 26 | b |= low; 27 | n ^= low; 28 | } 29 | out.printf("%d %d\n",a,b); 30 | } 31 | 32 | out.flush(); 33 | out.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /v120/GoogleIsFeelingLucky_UVa12015.java: -------------------------------------------------------------------------------- 1 | package v120; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class GoogleIsFeelingLucky_UVa12015{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 1; i <= t; i++) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | StringBuilder tmp = new StringBuilder(st.nextToken()+"\n"); 19 | int maxRelevance = Integer.parseInt(st.nextToken()); 20 | for(int j = 1; j < 10; j++) 21 | { 22 | st = new StringTokenizer(br.readLine()); 23 | String s = st.nextToken(); 24 | int curRelevance = Integer.parseInt(st.nextToken()); 25 | if(curRelevance>maxRelevance) 26 | { 27 | maxRelevance = curRelevance; 28 | tmp = new StringBuilder(s+"\n"); 29 | } 30 | else 31 | if(curRelevance==maxRelevance) 32 | tmp.append(s+"\n"); 33 | } 34 | sb.append("Case #"+i+":\n"+tmp); 35 | } 36 | System.out.print(sb); 37 | 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /v121/PolePosition_UVa12150.java: -------------------------------------------------------------------------------- 1 | package v121; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class PolePosition_UVa12150{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | int n; 16 | while((n=Integer.parseInt(br.readLine()))!=0) 17 | { 18 | int[] grid = new int[n]; 19 | boolean possible = getGrid(grid, br); 20 | if(!possible) 21 | sb.append("-1\n"); 22 | else 23 | { 24 | for(int i = 0; i < n - 1; i++) 25 | sb.append(grid[i]+" "); 26 | sb.append(grid[n-1]+"\n"); 27 | } 28 | } 29 | 30 | 31 | System.out.print(sb); 32 | 33 | 34 | } 35 | 36 | public static boolean getGrid(int[] grid, BufferedReader br) throws IOException 37 | { 38 | StringTokenizer st;boolean flag = true; 39 | for(int i = 0; i < grid.length; i++) 40 | { 41 | st = new StringTokenizer(br.readLine()); 42 | int c = Integer.parseInt(st.nextToken()); 43 | int p = Integer.parseInt(st.nextToken()); 44 | int pos = i + p; 45 | if(pos<0||pos>grid.length-1||grid[pos]!=0) 46 | flag = false; 47 | else 48 | grid[pos] = c; 49 | } 50 | return flag; 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /v121/TariffPlan_UVa12157.java: -------------------------------------------------------------------------------- 1 | package v121; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class TariffPlan_UVa12157{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt(br.readLine()); 15 | for(int i = 1; i <= t; i++) 16 | { 17 | int costMile = 0; int costJuice = 0; 18 | int n = Integer.parseInt(br.readLine()); 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | for(int j = 1; j <=n; j++) 21 | { 22 | int callDuration = Integer.parseInt(st.nextToken()); 23 | costMile += 10*(callDuration/30+1); 24 | costJuice += 15*(callDuration/60+1); 25 | } 26 | sb.append("Case "+i+": "); 27 | if(costMile<=costJuice) 28 | sb.append("Mile "); 29 | if(costMile>=costJuice) 30 | sb.append("Juice "); 31 | sb.append(Math.min(costMile, costJuice)+"\n"); 32 | } 33 | System.out.print(sb); 34 | 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /v123/ArmyBuddies_UVa12356.java: -------------------------------------------------------------------------------- 1 | package v123; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.LinkedList; 7 | import java.util.StringTokenizer; 8 | public class ArmyBuddies_UVa12356{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | StringTokenizer st; 15 | while(true) 16 | { 17 | st = new StringTokenizer(br.readLine()); 18 | int s = Integer.parseInt(st.nextToken()); 19 | if(s==0) 20 | break; 21 | int b = Integer.parseInt(st.nextToken()); 22 | 23 | LinkedList x = new LinkedList(); 24 | for(int j = 1; j <= s; j++) 25 | x.add(j); 26 | for(int i = 0; i < b; i++) 27 | { 28 | st = new StringTokenizer(br.readLine()); 29 | int l = Integer.parseInt(st.nextToken()); 30 | int r = Integer.parseInt(st.nextToken()); 31 | 32 | int index = x.indexOf(l); 33 | if(index==0) 34 | sb.append("* "); 35 | else 36 | sb.append(x.get(index-1)+" "); 37 | if(index + r - l==x.size() - 1) 38 | sb.append("*\n"); 39 | else 40 | sb.append(x.get(index + r - l + 1)+"\n"); 41 | 42 | 43 | for(int j = 1; j <= r - l + 1; j++) 44 | x.remove(index); 45 | 46 | 47 | } 48 | sb.append("-\n"); 49 | } 50 | 51 | 52 | 53 | System.out.print(sb); 54 | 55 | 56 | } 57 | 58 | 59 | 60 | } -------------------------------------------------------------------------------- /v124/LittleNephew_UVa12463.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | 4 | import java.io.BufferedReader; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | import java.io.PrintWriter; 9 | import java.util.StringTokenizer; 10 | 11 | public class LittleNephew_UVa12463 { 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | Scanner sc = new Scanner(System.in); 16 | PrintWriter out = new PrintWriter(System.out); 17 | while(true) 18 | { 19 | int[] in = new int[5]; 20 | int ans = 1; 21 | for(int i = 0; i < 5; ++i) 22 | ans *= in[i] = sc.nextInt(); 23 | ans *= in[3] * in[4]; 24 | if(ans == 0) 25 | break; 26 | out.println(ans); 27 | 28 | } 29 | out.flush(); 30 | 31 | } 32 | 33 | 34 | static class Scanner 35 | { 36 | StringTokenizer st; 37 | BufferedReader br; 38 | 39 | public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 40 | 41 | public String next() throws IOException 42 | { 43 | while (st == null || !st.hasMoreTokens()) 44 | st = new StringTokenizer(br.readLine()); 45 | return st.nextToken(); 46 | } 47 | 48 | public int nextInt() throws IOException {return Integer.parseInt(next());} 49 | 50 | public long nextLong() throws IOException {return Long.parseLong(next());} 51 | 52 | public String nextLine() throws IOException {return br.readLine();} 53 | 54 | public boolean ready() throws IOException {return br.ready();} 55 | 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /v124/PerfectChoir_UVa12485.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintWriter; 7 | import java.util.StringTokenizer; 8 | 9 | public class PerfectChoir_UVa12485 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | PrintWriter out = new PrintWriter(System.out); 15 | 16 | 17 | while(br.ready()) 18 | { 19 | int N = Integer.parseInt(br.readLine()); 20 | StringTokenizer st = new StringTokenizer(br.readLine()); 21 | int[] notes = new int[N];int sum = 0; 22 | for(int i = 0; i < N; i++) 23 | { 24 | notes[i] = Integer.parseInt(st.nextToken()); 25 | sum += notes[i]; 26 | } 27 | if(sum%N!=0){out.println(-1);continue;} 28 | int finalNote = sum/N; 29 | 30 | int up = 0; int down = 0; 31 | for(int i = 0; i < N; i++) 32 | if(notes[i]==finalNote) 33 | continue; 34 | else 35 | if(notes[i]>finalNote) 36 | down += notes[i] - finalNote; 37 | else 38 | up += finalNote - notes[i]; 39 | if(up==down) 40 | out.println(down+1); 41 | else 42 | out.println(-1); 43 | } 44 | out.flush(); 45 | out.close(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v124/Scarecrow_UVa12405.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | 8 | public class Scarecrow_UVa12405 { 9 | 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | int t = Integer.parseInt(br.readLine()); 17 | for(int k = 1; k <= t; k++) 18 | { 19 | int n = Integer.parseInt(br.readLine()); 20 | String field = br.readLine(); 21 | int count = 0; 22 | for(int i = 0; i < n; i++) 23 | if(field.charAt(i)=='.') 24 | { 25 | i+=2; 26 | count++; 27 | } 28 | 29 | 30 | sb.append("Case "+k+": "+count+"\n"); 31 | } 32 | 33 | System.out.print(sb); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /v124/ShortStoryCompetition_UVa12482.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class ShortStoryCompetition_UVa12482 { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | while(br.ready()) 16 | { 17 | StringTokenizer st = new StringTokenizer(br.readLine()); 18 | int N = Integer.parseInt(st.nextToken()); 19 | int L = Integer.parseInt(st.nextToken()); 20 | int C = Integer.parseInt(st.nextToken()); 21 | 22 | int lines = 0, remC = 0; 23 | st = new StringTokenizer(br.readLine()); 24 | while(N-->0) 25 | { 26 | int cur = st.nextToken().length(); 27 | if(cur > remC) 28 | { 29 | remC = C; 30 | lines++; 31 | } 32 | remC -= cur + 1; 33 | } 34 | sb.append((lines+L-1)/L).append("\n"); 35 | } 36 | System.out.print(sb); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /v124/StartGrid_UVa12488.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class StartGrid_UVa12488 { 9 | 10 | static void shift(int[] a, int from, int to) 11 | { 12 | int tmp = a[from]; 13 | for(int i = from; i > to; i--) 14 | a[i] = a[i-1]; 15 | a[to] = tmp; 16 | } 17 | public static void main(String[] args) throws IOException { 18 | 19 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 20 | StringBuilder sb = new StringBuilder(); 21 | while(br.ready()) 22 | { 23 | 24 | int n = Integer.parseInt(br.readLine()); 25 | int[] a = new int[n]; 26 | int[] b = new int[n]; 27 | StringTokenizer st = new StringTokenizer(br.readLine()); 28 | for(int i = 0; i < n; i++) 29 | a[i] = Integer.parseInt(st.nextToken()); 30 | st = new StringTokenizer(br.readLine()); 31 | for(int i = 0; i < n; i++) 32 | b[i] = Integer.parseInt(st.nextToken()); 33 | int count = 0; 34 | for(int i = 0; i < n; i++) 35 | for(int j = i; j < n; j++) 36 | if(b[i]==a[j]) 37 | { 38 | count += j - i; 39 | shift(a,j,i); 40 | break; 41 | } 42 | sb.append(count).append("\n"); 43 | } 44 | 45 | System.out.print(sb); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v124/Zapping_UVa12468.java: -------------------------------------------------------------------------------- 1 | package v124; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class Zapping_UVa12468{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | while(true) 15 | { 16 | StringTokenizer st = new StringTokenizer(br.readLine()); 17 | int a = Integer.parseInt(st.nextToken()); 18 | int b = Integer.parseInt(st.nextToken()); 19 | 20 | if(a==-1) 21 | break; 22 | 23 | int presses = Math.abs(b-a); 24 | if(presses>50) 25 | presses = 100 - presses; 26 | 27 | sb.append(presses+"\n"); 28 | } 29 | System.out.print(sb); 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /v125/ASpecialHappyBirthdaySong_UVa12554.java: -------------------------------------------------------------------------------- 1 | package v125; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class ASpecialHappyBirthdaySong_UVa12554{ 8 | 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | int n = Integer.parseInt(br.readLine()); 15 | String[] people = new String[n]; 16 | for(int i = 0; i < n; i++) 17 | { 18 | people[i] = br.readLine(); 19 | } 20 | 21 | for(int i = 1; i <= n || i%16-1 != 0; i++) 22 | { 23 | sb.append(people[(i-1)%n]+": "); 24 | if(i%4==0) 25 | { 26 | if(i%16!=0&&i%16%12==0) 27 | sb.append("Rujia\n"); 28 | else 29 | sb.append("you\n"); 30 | } 31 | else 32 | if(i%4==3) 33 | sb.append("to\n"); 34 | else 35 | if(i%4==2) 36 | sb.append("birthday\n"); 37 | else 38 | sb.append("Happy\n"); 39 | } 40 | 41 | System.out.print(sb); 42 | 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /v125/Birthdates_UVa12541.java: -------------------------------------------------------------------------------- 1 | package v125; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class Birthdates_UVa12541 { 10 | 11 | public static void main(String[] args) throws IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | int N = Integer.parseInt(br.readLine()); 15 | Person[] people = new Person[N]; 16 | for(int i = 0; i < N; i++) 17 | { 18 | StringTokenizer st= new StringTokenizer(br.readLine()); 19 | people[i] = new Person(st.nextToken(),getInt(st.nextToken()),getInt(st.nextToken()),getInt(st.nextToken())); 20 | } 21 | Arrays.sort(people); 22 | System.out.println(people[N-1].name); 23 | System.out.println(people[0].name); 24 | } 25 | 26 | static int getInt(String x) 27 | { 28 | return Integer.parseInt(x); 29 | } 30 | } 31 | 32 | class Person implements Comparable 33 | { 34 | String name; 35 | int dd,mm,yy; 36 | Person(String s, int x, int y, int z) 37 | { 38 | name = s; dd = x; mm = y; yy = z; 39 | } 40 | @Override 41 | public int compareTo(Person o) { 42 | if(yy!=o.yy) 43 | return yy - o.yy; 44 | if(mm!=o.mm) 45 | return mm - o.mm; 46 | return dd - o.dd; 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /v125/MemoryOverflow_UVa12583.java: -------------------------------------------------------------------------------- 1 | package v125; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.Arrays; 7 | import java.util.StringTokenizer; 8 | 9 | public class MemoryOverflow_UVa12583 { 10 | 11 | public static void main(String[] args) throws NumberFormatException, IOException { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | int tc = Integer.parseInt(br.readLine()); 17 | for(int t = 1; t <= tc; t++) 18 | { 19 | StringTokenizer st = new StringTokenizer(br.readLine()); 20 | int n = Integer.parseInt(st.nextToken()); 21 | int k = Integer.parseInt(st.nextToken()); 22 | String ppl = st.nextToken(); 23 | int[] last = new int[26]; 24 | Arrays.fill(last, -50000); 25 | int ans = 0; 26 | for(int i = 0; i < n; i++) 27 | { 28 | int p = ppl.charAt(i) - 'A'; 29 | if(i - k <= last[p]) 30 | ans++; 31 | last[p] = i; 32 | } 33 | sb.append("Case "+t+": "+ans+"\n"); 34 | } 35 | System.out.print(sb); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /v125/RobotInstructions_UVa12503.java: -------------------------------------------------------------------------------- 1 | package v125; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.StringTokenizer; 7 | 8 | public class RobotInstructions_UVa12503{ 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | StringBuilder sb = new StringBuilder(); 14 | int t = Integer.parseInt((new StringTokenizer(br.readLine())).nextToken()); 15 | for(int i = 1; i <= t; i++) 16 | { 17 | int p = 0; 18 | int n = Integer.parseInt((new StringTokenizer(br.readLine())).nextToken()); 19 | String[] s = new String[n]; 20 | for(int j = 0; j < n; j++) 21 | s[j] = br.readLine(); 22 | for(int j = 0; j < n; j++) 23 | p += instruction(s,j); 24 | 25 | sb.append(p+"\n"); 26 | } 27 | 28 | System.out.print(sb); 29 | 30 | } 31 | 32 | public static int instruction(String[] s, int index) 33 | { 34 | 35 | StringTokenizer st = new StringTokenizer(s[index]); 36 | String tmp = st.nextToken(); 37 | if(tmp.equals("LEFT")) 38 | return -1; 39 | if(tmp.equals("RIGHT")) 40 | return 1; 41 | st.nextToken(); 42 | return instruction(s,Integer.parseInt(st.nextToken())-1); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /v125/WeddingOfSultan_UVa12582.java: -------------------------------------------------------------------------------- 1 | package v125; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class WeddingOfSultan_UVa12582 { 8 | 9 | public static void main(String[] args) throws NumberFormatException, IOException { 10 | 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | StringBuilder sb = new StringBuilder(); 13 | int TC = Integer.parseInt(br.readLine()); 14 | boolean[][] adjMatrix; 15 | int[] parent; 16 | for(int k = 1; k <= TC; k++) 17 | { 18 | sb.append("Case ").append(k).append("\n"); 19 | adjMatrix = new boolean[26][26]; 20 | parent = new int[26]; 21 | String traverse = br.readLine(); 22 | int u = traverse.charAt(0) -'A'; 23 | for(int i = 1, length = traverse.length() - 1; i < length; i++) 24 | { 25 | int v = traverse.charAt(i) - 'A'; 26 | if(v==u) u = parent[u]; 27 | else 28 | { 29 | adjMatrix[u][v] = adjMatrix[v][u] = true; 30 | parent[v] = u; 31 | u = v; 32 | } 33 | } 34 | for(int i = 0; i < 26; i++) 35 | { 36 | int count = 0; 37 | for(int j = 0; j < 26; j++) 38 | if(adjMatrix[i][j]) 39 | count++; 40 | if(count!=0) 41 | sb.append((char)(i + 'A')).append(" = ").append(count).append("\n"); 42 | } 43 | 44 | } 45 | System.out.print(sb); 46 | } 47 | } 48 | --------------------------------------------------------------------------------