├── UVa - 10219 - Find the ways.cpp ├── UVa 10004 - Bicoloring.cpp ├── UVa 10009 - All Roads Lead Where.cpp ├── UVa 10018 - Reverse and Add.cpp ├── UVa 10020 - Minimal coverage.cpp ├── UVa 10026 - Shoemakers Problem.cpp ├── UVa 10029 - Edit Step Ladders.cpp ├── UVa 10032 - Tug of War.cpp ├── UVa 10036 - Divisibility.cpp ├── UVa 10037 - Bridge.cpp ├── UVa 10038 - Jolly Jumpers.cpp ├── UVa 10041 - Vitos Family.cpp ├── UVa 10070 - Leap Year or Not Leap Year and.cpp ├── UVa 10081 - Tight Words.cpp ├── UVa 10098 - Generating Fast.cpp ├── UVa 10099 - The Tourist Guide.cpp ├── UVa 10105 - Polynomial Coefficients.cpp ├── UVa 10107 - What is the Median.cpp ├── UVa 10132 - File Fragmentation.cpp ├── UVa 10140 - Prime Distance.cpp ├── UVa 10152 - ShellSort.cpp ├── UVa 10154 - Weights and Measures.cpp ├── UVa 10164 - Number Game.cpp ├── UVa 10189 - Minesweeper.cpp ├── UVa 10260 - Soundex.cpp ├── UVa 10261 - Ferry Loading.cpp ├── UVa 10264 - The Most Potent Corner.cpp ├── UVa 10279 - Mine Sweeper.cpp ├── UVa 10282 - Babelfish.cpp ├── UVa 10295 - Hay Points.cpp ├── UVa 10304 - Optimal Binary Search Tree.cpp ├── UVa 10305 - Ordering Tasks.cpp ├── UVa 10313 - Pay the Price.cpp ├── UVa 10324 - Zeros and Ones.cpp ├── UVa 10341 - Solve It.cpp ├── UVa 10364 - Square.cpp ├── UVa 10377 - Maze Traversal.cpp ├── UVa 10382 - Watering Grass.cpp ├── UVa 10394 - Twin Primes.cpp ├── UVa 10407 - Simple division.cpp ├── UVa 10409 - Die Game.cpp ├── UVa 10419 - Sum-up the Primes.cpp ├── UVa 10424 - Love Calculator.cpp ├── UVa 10450 - World Cup Noise.cpp ├── UVa 10453 - Make Palindrome.cpp ├── UVa 10465 - Homer Simpson.cpp ├── UVa 10474 - Where is the Marble.cpp ├── UVa 10487 - Closest Sums.cpp ├── UVa 10491 - Cows and Cars.cpp ├── UVa 10520 - Determine it.cpp ├── UVa 10530 - Guessing Game.cpp ├── UVa 10534 - Wavio Sequence.cpp ├── UVa 10550 - Combination Lock.cpp ├── UVa 10566 - Crossed Ladders.cpp ├── UVa 10591 - Happy Number.cpp ├── UVa 10611 - The Playboy Chimp.cpp ├── UVa 10617 - Again Palindrome.cpp ├── UVa 10664 - Luggage.cpp ├── UVa 10690 - Expression Again.cpp ├── UVa 10703 - Free spots.cpp ├── UVa 10706 - Number Sequence.cpp ├── UVa 10721 - Bar Codes.cpp ├── UVa 10763 - Foreign Exchange.cpp ├── UVa 10801 - Lift Hopping.cpp ├── UVa 10819 - Trouble of 13-Dots.cpp ├── UVa 10892 - LCM Cardinality.cpp ├── UVa 10901 - Ferry Loading III.cpp ├── UVa 10905 - Children's Game.cpp ├── UVa 10912 - Simple Minded Hashing.cpp ├── UVa 10919 - Prerequisites.cpp ├── UVa 10945 - Mother bear.cpp ├── UVa 10946 - You want what filled.cpp ├── UVa 10976 - Fractions Again.cpp ├── UVa 10980 - Lowest Price in Town.cpp ├── UVa 11003 - Boxes.cpp ├── UVa 11034 - Ferry Loading IV.cpp ├── UVa 11057 - Exact Sum.cpp ├── UVa 11059 - Maximum Product.cpp ├── UVa 11080 - Place the Guards.cpp ├── UVa 11084 - Anagram Division.cpp ├── UVa 11151 - Longest Palindrome.cpp ├── UVa 11192 - Group Reverse.cpp ├── UVa 11218 - KTV.cpp ├── UVa 11219 - How old are you.cpp ├── UVa 11221 - Magic square palindromes.cpp ├── UVa 1124 - Celebrity jeopardy.cpp ├── UVa 11264 - Coin Collector.cpp ├── UVa 11309 - Counting Chaos.cpp ├── UVa 11321 - Sort Sort and Sort.cpp ├── UVa 11349 - Symmetric Matrix.cpp ├── UVa 11364 - Parking.cpp ├── UVa 11389 - The Bus Driver Problem.cpp ├── UVa 11391 - Blobs in the Board.cpp ├── UVa 11407 - Squares.cpp ├── UVa 11413 - Fill the Containers.cpp ├── UVa 11456 - Trainsorting.cpp ├── UVa 11463 - Commandos.cpp ├── UVa 11470 - Square Sums.cpp ├── UVa 11472 - Beautiful Numbers.cpp ├── UVa 11498 - Division of Nlogonia.cpp ├── UVa 11504 - Dominos.cpp ├── UVa 11517 - Exact Change.cpp ├── UVa 11559 - Event Planning.cpp ├── UVa 11586 - Train Tracks.cpp ├── UVa 11608 - No Problem.cpp ├── UVa 11631 - Dark roads.cpp ├── UVa 11661 - Burger Time.cpp ├── UVa 11687 - Digits.cpp ├── UVa 11703 - sqrt log sin.cpp ├── UVa 11850 - Alaska.cpp ├── UVa 11876 - N + NOD (N).cpp ├── UVa 11881 - Internal Rate of Return.cpp ├── UVa 119 - Greedy Gift Givers.cpp ├── UVa 11900 - Boiled Eggs.cpp ├── UVa 11926 - Multitasking.cpp ├── UVa 11942 - Lumberjack Sequencing.cpp ├── UVa 11988 - Broken Keyboard.cpp ├── UVa 12015 - Google is Feeling Lucky.cpp ├── UVa 12250 - Language Detection.cpp ├── UVa 12289 - One-Two-Three.cpp ├── UVa 12372 - Packing for Holiday.cpp ├── UVa 1238 - Free Parentheses.cpp ├── UVa 124 - Following Orders.cpp ├── UVa 12405 - Scarecrow.cpp ├── UVa 125 - Numbering Paths.cpp ├── UVa 12503 - Robot Instructions.cpp ├── UVa 1252 - Twenty Questions.cpp ├── UVa 128 - Software CRC.cpp ├── UVa 148 - Anagram checker.cpp ├── UVa 156 - Ananagrams.cpp ├── UVa 164 - String Computer.cpp ├── UVa 183 - Bit Maps.cpp ├── UVa 191 - Intersection.cpp ├── UVa 195 - Anagram.cpp ├── UVa 200 - Rare Order.cpp ├── UVa 216 - Getting in Line.cpp ├── UVa 256 - Quirksome Squares.cpp ├── UVa 257 - Palinwords.cpp ├── UVa 311 - Packets.cpp ├── UVa 315 - Network.cpp ├── UVa 324 - Factorial Frequencies.cpp ├── UVa 336 - A Node Too Far.cpp ├── UVa 340 - Master-Mind Hints.cpp ├── UVa 352 - The Seasonal War.cpp ├── UVa 353 - Pesky Palindromes.cpp ├── UVa 400 - Unix ls.cpp ├── UVa 401 - Palindromes.cpp ├── UVa 414 - Machined Surfaces.cpp ├── UVa 417 - Word Index.cpp ├── UVa 429 - Word Transformation.cpp ├── UVa 454 - Anagrams.cpp ├── UVa 459 - Graph Connectivity.cpp ├── UVa 466 - Mirror Mirror.cpp ├── UVa 481 - What Goes Up.cpp ├── UVa 484 - The Department of Redundancy Department.cpp ├── UVa 489 - Hangman Judge.cpp ├── UVa 497 - Strategic Defense Initiative.cpp ├── UVa 514 - Rails.cpp ├── UVa 534 - Frogger.cpp ├── UVa 541 - Error Correction.cpp ├── UVa 567 - Risk.cpp ├── UVa 579 - ClockHands.cpp ├── UVa 591 - Box of Bricks.cpp ├── UVa 621 - Secret Research.cpp ├── UVa 674 - Coin Change - Bottom-Up DP.cpp ├── UVa 674 - Coin Change - Top-Down DP.cpp ├── UVa 714 - Copying Books.cpp ├── UVa 763 - Fibinary Numberscpp.cpp ├── UVa 787 - Maximum Sub-sequence Product.cpp ├── UVa 796 - Critical Links.cpp ├── UVa 820 - Internet Bandwidth.cpp ├── UVa 821 - Page Hopping.cpp ├── UVa 836 - Largest Submatrix.cpp ├── UVa 869 - Airline Comparison.cpp ├── UVa 908 - Re-connecting Computer Sites.cpp ├── UVa 920 - Sunny Mountains.cpp ├── UVa 929 - Number Maze.cpp ├── Uva 10176 - Ocean Deep ! - Make it shallow !!.cpp ├── Uva 10986 - Sending email.cpp ├── Uva 11088 - End up with More Teams.cpp ├── Uva 532 - Dungeon Master.cpp └── utility └── BigInt.cpp /UVa - 10219 - Find the ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | unsigned long long n, k; 8 | while (cin >> n >> k) 9 | { 10 | // log(C(n, k)) = log(n!/(n - k)!k!) 11 | // = log(n!) - log((n - k)!k!) 12 | long double logSum = 0; 13 | for (unsigned long long i = n - k + 1; i <= n; ++i) 14 | logSum += log10((double)i); 15 | for (unsigned long long i = 1; i <= k; ++i) 16 | logSum -= log10((double)i); 17 | 18 | cout << (int)logSum + 1 << endl; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /UVa 10004 - Bicoloring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int BLACK = 0; 7 | static const int WHITE = 1; 8 | static const int UNKNOWN = 2; 9 | 10 | int main() 11 | { 12 | int n; 13 | while (cin >> n, n != 0) 14 | { 15 | vector > G(n); 16 | vector color(n, UNKNOWN); 17 | bool isBipartite = true; 18 | 19 | int l; 20 | cin >> l; 21 | while ( l-- ) 22 | { 23 | int u, v; 24 | cin >> u >> v; 25 | G[u].push_back(v); 26 | G[v].push_back(u); 27 | } 28 | 29 | queue q; 30 | // Since the graph is strongly connected, we can reach all the 31 | // vertices starting from any vertex. 32 | color[0] = BLACK; 33 | q.push(0); 34 | while (!q.empty() && isBipartite) 35 | { 36 | int u = q.front(); 37 | q.pop(); 38 | for (int i = 0; i < G[u].size(); ++i) 39 | { 40 | int v = G[u][i]; 41 | if (color[v] == color[u]) 42 | { 43 | isBipartite = false; 44 | break; 45 | } 46 | else if (color[v] == UNKNOWN) 47 | { 48 | color[v] = 1 - color[u]; 49 | q.push(v); 50 | } 51 | } 52 | } 53 | if (isBipartite) 54 | cout << "BICOLORABLE." << endl; 55 | else 56 | cout << "NOT BICOLORABLE." << endl; 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /UVa 10009 - All Roads Lead Where.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void printPath(map &prev, string v) 9 | { 10 | const string &u = prev[v]; 11 | if (u != v) 12 | printPath(prev, u); 13 | cout << v[0]; 14 | } 15 | 16 | int main() 17 | { 18 | int T; 19 | cin >> T; 20 | while ( T-- ) 21 | { 22 | int m, n; 23 | cin >> m >> n; 24 | 25 | map > adjList; 26 | 27 | for (int i = 0; i < m; ++i) 28 | { 29 | string u, v; 30 | cin >> u >> v; 31 | adjList[u].push_back(v); 32 | adjList[v].push_back(u); 33 | } 34 | for (int i = 0; i < n; ++i) 35 | { 36 | string s, t; 37 | cin >> s >> t; 38 | // BFS 39 | queue q; 40 | map prev; 41 | 42 | prev[s] = s; 43 | q.push(s); 44 | while (!q.empty() && !prev.count(t)) 45 | { 46 | string u = q.front(); 47 | q.pop(); 48 | for (size_t j = 0; j < adjList[u].size(); ++j) 49 | { 50 | const string &v = adjList[u][j]; 51 | if (! prev.count(v)) 52 | { 53 | prev[v] = u; 54 | q.push(v); 55 | } 56 | } 57 | } 58 | printPath(prev, t); 59 | cout << endl; 60 | } 61 | if (T) 62 | cout << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /UVa 10018 - Reverse and Add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int reverseAndAdd(string &n) 8 | { 9 | string rn(n); 10 | reverse(rn.begin(), rn.end()); 11 | unsigned int n_i, rn_i; 12 | stringstream ss(n); 13 | ss >> n_i; 14 | ss.clear(); 15 | ss.str(rn); 16 | ss >> rn_i; 17 | ss.clear(); 18 | ss << n_i + rn_i; 19 | string sum = ss.str(); 20 | string r_sum(sum); 21 | reverse(r_sum.begin(), r_sum.end()); 22 | 23 | n = sum; 24 | // If the sum is a palindrome. 25 | if (sum == r_sum) 26 | return 1; 27 | else 28 | return 1 + reverseAndAdd(n); 29 | 30 | } 31 | 32 | int main() 33 | { 34 | size_t N; 35 | cin >> N; 36 | while ( N-- ) 37 | { 38 | string number; 39 | cin >> number; 40 | cout << reverseAndAdd(number) 41 | << " " 42 | << number 43 | << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa 10020 - Minimal coverage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Segment 7 | { 8 | int L, R; 9 | }; 10 | 11 | bool comp(const Segment &s1, const Segment &s2) 12 | { 13 | return s1.L < s2.L; 14 | } 15 | 16 | int main() 17 | { 18 | int T; 19 | cin >> T; 20 | while ( T-- ) 21 | { 22 | int M; 23 | cin >> M; 24 | vector S(1); 25 | while (cin >> S[0].L >> S[0].R, !(S[0].L == 0 && S[0].R == 0)) 26 | S.push_back(S[0]); 27 | // Sort S by increasing left endpoint. 28 | sort(S.begin() + 1, S.end(), comp); 29 | int curL = 0, rReach = 0; 30 | size_t i = 1; 31 | vector segments; 32 | while (rReach < M) 33 | { 34 | // Choose the segment which L <= curL and extends the farthest 35 | // to right. 36 | int newL = curL; 37 | size_t farthest = 0; 38 | for ( ; i < S.size(); ++i) 39 | { 40 | if (S[i].L > curL) 41 | break; 42 | else if (S[i].R >= newL) 43 | { 44 | newL = S[i].R; 45 | farthest = i; 46 | } 47 | } 48 | if (farthest == 0) 49 | break; 50 | segments.push_back(S[farthest]); 51 | curL = rReach = newL; 52 | } 53 | if (rReach >= M) 54 | { 55 | cout << segments.size() << endl; 56 | for (size_t i = 0; i < segments.size(); ++i) 57 | cout << segments[i].L << " " << segments[i].R << endl; 58 | } 59 | else 60 | cout << "0" << endl; 61 | if (T) 62 | cout << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /UVa 10026 - Shoemakers Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Job 7 | { 8 | int time; 9 | int fine; 10 | int id; 11 | }; 12 | 13 | bool comp(const Job &j1, const Job&j2) 14 | { 15 | return j1.time * j2.fine < j2.time * j1.fine; 16 | } 17 | 18 | int main() 19 | { 20 | int T; 21 | cin >> T; 22 | while ( T-- ) 23 | { 24 | int N; 25 | cin >> N; 26 | 27 | vector job(N); 28 | for (int i = 0; i < N; ++i) 29 | { 30 | cin >> job[i].time >> job[i].fine; 31 | job[i].id = i + 1; 32 | } 33 | 34 | // Greedily pick a next job imposing the minimum fine. 35 | // Keep lexicographical order for equivalent jobs. 36 | stable_sort(job.begin(), job.end(), comp); 37 | 38 | cout << job[0].id; 39 | for (int i = 1; i < N; ++i) 40 | cout << " " << job[i].id; 41 | cout << endl; 42 | 43 | if (T) 44 | cout << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa 10029 - Edit Step Ladders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | map L; 8 | 9 | int main() 10 | { 11 | int maxLength = 0; 12 | string w2; 13 | while (cin >> w2) 14 | { 15 | int length = 1; 16 | // Insert c before w2[i]. 17 | for (int i = 0; i <= w2.size(); ++i) 18 | for (char c = 'a'; c <= 'z'; ++c) 19 | { 20 | string w1(w2); 21 | w1.insert(i, 1, c); 22 | if (w1 > w2) 23 | break; 24 | if (L.count(w1)) 25 | length = max(length, L[w1] + 1); 26 | } 27 | // Delete w2[i]. 28 | for (int i = 0; i < w2.size(); ++i) 29 | { 30 | string w1(w2); 31 | w1.erase(i, 1); 32 | if (L.count(w1)) 33 | length = max(length, L[w1] + 1); 34 | } 35 | // Change w2[i]. 36 | for (int i = 0; i < w2.size(); ++i) 37 | for (char c = 'a'; c <= 'z' && c != w2[i]; ++c) 38 | { 39 | string w1(w2); 40 | w1[i] = c; 41 | if (w1 > w2) 42 | break; 43 | if (L.count(w1)) 44 | length = max(length, L[w1] + 1); 45 | } 46 | L[w2] = length; 47 | maxLength = max(maxLength, length); 48 | } 49 | cout << maxLength << endl; 50 | } 51 | -------------------------------------------------------------------------------- /UVa 10032 - Tug of War.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int N; 9 | int W[100 + 5]; 10 | long long dp[450 * 100 + 5]; 11 | 12 | void solve() 13 | { 14 | int sum = accumulate(W + 1, W + N + 1, 0); 15 | 16 | // If (dp[i] << j) & 1 is 1, that means it is possible 17 | // to select j out of the N people so that the sum of 18 | // their weight is i. 19 | memset(dp, 0, sizeof(dp)); 20 | dp[0] = 1 << 0; 21 | 22 | for (int i = 1; i <= N; ++i) 23 | for (int j = sum; j >= 0; --j) 24 | if (dp[j]) 25 | dp[j + W[i]] |= dp[j] << 1; 26 | 27 | int minDiff = 450 * 100; 28 | int teamOneWeight = 0, teamTwoWeight = 0; 29 | for (int i = 0; i <= sum; ++i) 30 | if (dp[i] & (1ll << (N / 2))) 31 | { 32 | int diff = abs(i - (sum - i)); 33 | if (diff < minDiff) 34 | { 35 | minDiff = diff; 36 | teamOneWeight = min(i, sum - i); 37 | teamTwoWeight = max(i, sum - i); 38 | } 39 | } 40 | cout << teamOneWeight << " " << teamTwoWeight << endl; 41 | } 42 | 43 | int main() 44 | { 45 | int T, Case = 0; 46 | cin >> T; 47 | while ( T-- ) 48 | { 49 | cin >> N; 50 | for (int i = 1; i <= N; ++i) 51 | cin >> W[i]; 52 | 53 | if (++Case > 1) 54 | cout << endl; 55 | solve(); 56 | } 57 | } -------------------------------------------------------------------------------- /UVa 10036 - Divisibility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int MAX_K = 100; 7 | 8 | int main() 9 | { 10 | int M; 11 | cin >> M; 12 | while ( M-- ) 13 | { 14 | int N, K; 15 | cin >> N >> K; 16 | 17 | // remainders[indexAnswer][remainder] is true if the sequence is divisble 18 | // by remainder. 19 | vector > remainders(2, vector(MAX_K, false)); 20 | int indexTemp = 0, indexAnswer = 1; 21 | // 0 is reachable for empty sequence. 22 | remainders[indexTemp][0] = remainders[indexAnswer][0] = true; 23 | for (int i = 1; i <= N; ++i) 24 | { 25 | swap(indexTemp, indexAnswer); 26 | remainders[indexAnswer].assign(MAX_K, false); 27 | 28 | int integer; 29 | cin >> integer; 30 | 31 | /** If the current sequence is divisible by remainder, then adding integer 32 | or -integer will reach two new remainders. So, for each integer, scan 33 | remainder from 0 to K - 1 and set reachable remainders accordingly. */ 34 | for (int remainder = 0; remainder < K; ++remainder) 35 | { 36 | if (remainders[indexTemp][remainder]) 37 | { 38 | remainders[indexAnswer][(remainder + (integer % K) + K) % K] = true; 39 | remainders[indexAnswer][(remainder + (-integer % K) + K) % K] = true; 40 | } 41 | } 42 | } 43 | cout << (remainders[indexAnswer][0]? "Divisible" : "Not divisible") 44 | << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa 10037 - Bridge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void print(stringstream &ss, int t1, int t2 = -1) 9 | { 10 | if (t2 == -1) 11 | ss << t1 << endl; 12 | else 13 | ss << t1 << " " << t2 << endl; 14 | } 15 | 16 | int main() 17 | { 18 | int T; 19 | cin >> T; 20 | while ( T-- ) 21 | { 22 | int n; 23 | cin >> n; 24 | 25 | vector t(n + 1); 26 | for (int i = 1; i <= n; ++i) 27 | cin >> t[i]; 28 | 29 | sort(t.begin(), t.end()); 30 | 31 | stringstream ss; 32 | int c = n; 33 | int totalTime = 0; 34 | // In each round consider the fastest 2 people (A and B) and 35 | // the slowest 2 people (Y and Z). 36 | while (c >= 4) 37 | { 38 | // 39 | int t1 = t[2] + t[1] + t[c] + t[2]; 40 | // 41 | int t2 = t[c] + t[1] + t[c - 1] + t[1]; 42 | if (t1 < t2) 43 | { 44 | totalTime += t1; 45 | print(ss, t[1], t[2]); 46 | print(ss, t[1]); 47 | print(ss, t[c - 1], t[c]); 48 | print(ss, t[2]); 49 | } 50 | else 51 | { 52 | totalTime += t2; 53 | print(ss, t[1], t[c]); 54 | print(ss, t[1]); 55 | print(ss, t[1], t[c - 1]); 56 | print(ss, t[1]); 57 | } 58 | c -= 2; 59 | } 60 | // 61 | if (c == 3) 62 | { 63 | totalTime += t[2] + t[1] + t[3]; 64 | print(ss, t[1], t[2]); 65 | print(ss, t[1]); 66 | print(ss, t[1], t[3]); 67 | } 68 | // 69 | else if (c == 2) 70 | { 71 | totalTime += t[2]; 72 | print(ss, t[1], t[2]); 73 | } 74 | // 75 | else 76 | { 77 | totalTime += t[1]; 78 | print(ss, t[1]); 79 | } 80 | 81 | cout << totalTime << endl; 82 | cout << ss.str(); 83 | 84 | if (T) 85 | cout << endl; 86 | } 87 | return 0; 88 | } -------------------------------------------------------------------------------- /UVa 10038 - Jolly Jumpers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | while (cin >> n) 12 | { 13 | vector sequence; 14 | for (int i = 1; i <= n; ++i) 15 | { 16 | int integer; 17 | cin >> integer; 18 | sequence.push_back(integer); 19 | } 20 | vector::iterator iter(sequence.begin() + 1); 21 | map memo; 22 | bool jolly = true; 23 | for ( ; iter != sequence.end(); ++iter) 24 | { 25 | // "absolute values of the difference between successive elements.." 26 | int diff = abs(*iter - *(iter - 1)); 27 | if (diff > n - 1) 28 | { 29 | jolly = false; 30 | break; 31 | } 32 | ++memo[diff]; 33 | } 34 | // "..take on all the values 1 through n-1." 35 | if (jolly && memo.size() == n - 1) 36 | cout << "Jolly" << endl; 37 | else 38 | cout << "Not jolly" << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa 10041 - Vitos Family.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t T; 9 | cin >> T; 10 | while ( T-- ) 11 | { 12 | size_t r; 13 | cin >> r; 14 | vector streets(r); 15 | for (size_t i = 0; i < r; ++i) 16 | cin >> streets[i]; 17 | 18 | sort(streets.begin(), streets.end()); 19 | 20 | int median = streets[streets.size() / 2]; 21 | int distance = 0; 22 | for (size_t i = 0; i < r; ++i) 23 | distance += (median >= streets[i]? 24 | median - streets[i] : streets[i] - median); 25 | 26 | cout << distance << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /UVa 10081 - Tight Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int K, N; 9 | double dp[105][10]; 10 | 11 | double solve() 12 | { 13 | memset(dp, 0, sizeof (dp)); 14 | 15 | for (int i = 0; i <= K; ++i) 16 | dp[1][i] = 1; 17 | 18 | for (int i = 2; i <= N; ++i) 19 | for (int j = 0; j <= K; ++j) 20 | { 21 | if (j == 0) 22 | dp[i][j] = dp[i - 1][j] + dp[i - 1][j + 1]; 23 | else if (j == K) 24 | dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 25 | else 26 | dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1];; 27 | } 28 | 29 | 30 | return 100.0 * accumulate(dp[N], dp[N] + K + 1, 0.0) / pow((double)K + 1, N); 31 | } 32 | 33 | int main() 34 | { 35 | 36 | while (cin >> K >> N) 37 | { 38 | cout << setprecision(5) << fixed << solve() << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /UVa 10098 - Generating Fast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t T; 9 | cin >> T; 10 | while ( T-- ) 11 | { 12 | string s; 13 | cin >> s; 14 | sort(s.begin(), s.end()); 15 | do 16 | { 17 | cout << s << endl; 18 | } while (next_permutation(s.begin(), s.end())); 19 | cout << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /UVa 10099 - The Tourist Guide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Bus 7 | { 8 | int u, v, limit; 9 | }; 10 | 11 | bool comp(const Bus &s1, const Bus &s2) 12 | { 13 | return s1.limit > s2.limit; 14 | } 15 | 16 | int getGroup(const vector &groups, int u) 17 | { 18 | return groups[u] == u? 19 | u : getGroup(groups, groups[u]); 20 | } 21 | 22 | int main() 23 | { 24 | int Case = 1, N, R; 25 | while (cin >> N >> R, !(N == 0 && R == 0)) 26 | { 27 | vector buses; 28 | vector groups(N + 1); 29 | 30 | for (int i = 0; i < R; ++i) 31 | { 32 | Bus bus; 33 | cin >> bus.u >> bus.v >> bus.limit; 34 | // Mr. G. needs a seat for himself. 35 | --bus.limit; 36 | buses.push_back(bus); 37 | } 38 | 39 | int S, D, T; 40 | cin >> S >> D >> T; 41 | 42 | sort(buses.begin(), buses.end(), comp); 43 | for (int i = 1; i <= N; ++i) 44 | groups[i] = i; 45 | 46 | // Kruskal's algorithm. 47 | for (int i = 0; i < R; ++i) 48 | { 49 | Bus &bus = buses[i]; 50 | int groupU = getGroup(groups, bus.u); 51 | int groupV = getGroup(groups, bus.v); 52 | if (groupU != groupV) 53 | { 54 | groups[groupV] = groupU; 55 | if (getGroup(groups, S) == getGroup(groups, D)) 56 | { 57 | cout << "Scenario #" 58 | << Case++ 59 | << endl 60 | << "Minimum Number of Trips = " 61 | << (T + bus.limit - 1) / bus.limit 62 | << endl << endl; 63 | break; 64 | } 65 | } 66 | } 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /UVa 10105 - Polynomial Coefficients.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int fac[13]; 7 | fac[0] = 1; 8 | for (int i = 1; i < 13; ++i) 9 | fac[i] = i * fac[i - 1]; 10 | 11 | int n, k; 12 | while (cin >> n >> k) 13 | { 14 | // Coefficient = n!/(n1! n2! n3!...) 15 | int product = 1; 16 | for (int i = 1; i <= k; ++i) 17 | { 18 | int n_i; 19 | cin >> n_i; 20 | product *= fac[n_i]; 21 | } 22 | cout << fac[n] / product << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa 10107 - What is the Median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int X; 9 | vector values; 10 | while (cin >> X) 11 | { 12 | values.push_back(X); 13 | nth_element(values.begin(), 14 | values.begin() + values.size() / 2, 15 | values.end()); 16 | 17 | // Using values[values.size() / 2] would be wrong if values.size() is 1. 18 | int m1 = *(values.begin() + values.size() / 2); 19 | int m2 = m1; 20 | // If size is even, then call nth_element to find the other middle number. 21 | if (values.size() % 2 == 0) 22 | { 23 | nth_element(values.begin(), 24 | values.begin() + values.size() / 2 - 1, 25 | values.end()); 26 | m2 = *(values.begin() + values.size() / 2 - 1); 27 | } 28 | // "you have to print only the integer part, not the fractional." 29 | cout << (m1 + m2) / 2 << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa 10132 - File Fragmentation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | string s; 11 | getline(cin, s); 12 | size_t T; 13 | istringstream ss(s); 14 | ss >> T; 15 | // Skip the first empty line. 16 | getline(cin, s); 17 | while ( T-- ) 18 | { 19 | vector fragments; 20 | while (getline(cin, s) && !s.empty()) 21 | { 22 | ss.clear(); 23 | ss.str(s); 24 | string fragment; 25 | ss >> fragment; 26 | fragments.push_back(fragment); 27 | } 28 | 29 | // Consider all concatenations of any two strings. 30 | map memo; 31 | for (size_t i = 0; i < fragments.size(); ++i) 32 | for (size_t j = i + 1; j < fragments.size(); ++j) 33 | { 34 | ++memo[fragments[i] + fragments[j]]; 35 | ++memo[fragments[j] + fragments[i]]; 36 | } 37 | 38 | // Search for the string of highest count. 39 | map::iterator iter(memo.begin()); 40 | map::iterator file(memo.begin()); 41 | for (; iter != memo.end(); ++iter) 42 | { 43 | if (iter->second > file->second) 44 | file = iter; 45 | } 46 | cout << file->first << endl; 47 | if (T) 48 | cout << endl; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /UVa 10152 - ShellSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | map position; 10 | 11 | bool comp(const string &s1, const string &s2) 12 | { 13 | return position[s1] > position[s2]; 14 | } 15 | 16 | int main() 17 | { 18 | int K; 19 | cin >> K; 20 | while ( K-- ) 21 | { 22 | int n; 23 | cin >> n; 24 | cin.ignore(); 25 | 26 | vector turtles(n), ordering(n); 27 | for (int i = 0; i < n; ++i) 28 | getline(cin, turtles[i]); 29 | position.clear(); 30 | for (int i = 0; i < n; ++i) 31 | { 32 | getline(cin, ordering[i]); 33 | position[ordering[i]] = i; 34 | } 35 | /** For example, 36 | original: 1 2 3 4 5 37 | desired: 2 3 1 4 5 38 | 39 | (1)ops.push_back(1), shift = 1 40 | Done. 41 | */ 42 | int shift = 0; 43 | vector ops; 44 | for (int i = n - 1; i >= 0; --i) 45 | if (i + shift != position[turtles[i]]) 46 | { 47 | ops.push_back(turtles[i]); 48 | ++shift; 49 | } 50 | 51 | sort(ops.begin(), ops.end(), comp); 52 | copy(ops.begin(), ops.end(), ostream_iterator(cout, "\n")); 53 | cout << endl; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /UVa 10154 - Weights and Measures.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Turtle 9 | { 10 | int w, s; 11 | }; 12 | 13 | bool comp(const Turtle &t1, const Turtle &t2) 14 | { 15 | return t1.s < t2.s; 16 | } 17 | 18 | vector T; 19 | int dp[5607]; 20 | 21 | void solve() 22 | { 23 | sort(T.begin(), T.end(), comp); 24 | 25 | fill(dp, dp + 5607, numeric_limits::max()); 26 | dp[0] = 0; 27 | 28 | int maxNTurtles = 0; 29 | 30 | for (int i = 0; i < T.size(); ++i) 31 | for (int j = maxNTurtles; j >= 0; --j) 32 | // Try to put turtle i below the current stack of j turtles. 33 | // 1.Turtle i can carry the stack plus itself. 34 | // 2.They produce a smaller weight for the current stack of j+1 turtles. 35 | if (T[i].s >= dp[j] + T[i].w 36 | && T[i].w + dp[j] < dp[j + 1]) 37 | { 38 | dp[j + 1] = dp[j] + T[i].w; 39 | maxNTurtles = max(maxNTurtles, j + 1); 40 | } 41 | 42 | cout << maxNTurtles << endl; 43 | } 44 | 45 | int main() 46 | { 47 | Turtle t; 48 | while (cin >> t.w >> t.s) 49 | T.push_back(t); 50 | 51 | solve(); 52 | } -------------------------------------------------------------------------------- /UVa 10164 - Number Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int TRIALS = 1000000; 8 | 9 | int N; 10 | vector numbers; 11 | 12 | void solve() 13 | { 14 | for (int trial = 1; trial <= TRIALS; ++trial) 15 | { 16 | vector S; 17 | vector used(2 * N - 1, false); 18 | for (int i = 1; i <= N; ++i) 19 | { 20 | int select = rand() % (2 * N - 1); 21 | if (used[select]) 22 | { 23 | --i; 24 | continue; 25 | } 26 | used[select] = true; 27 | S.push_back(numbers[select]); 28 | } 29 | if (accumulate(S.begin(), S.end(), 0) % N == 0) 30 | { 31 | cout << "Yes" << endl; 32 | cout << S[0]; 33 | for (int i = 1; i < S.size(); ++i) 34 | cout << " " << S[i]; 35 | cout << endl; 36 | return; 37 | } 38 | } 39 | cout << "No" << endl; 40 | } 41 | 42 | int main() 43 | { 44 | while (cin >> N, N > 0) 45 | { 46 | numbers.clear(); 47 | for (int i = 0; i < 2 * N - 1; ++i) 48 | { 49 | int n; 50 | cin >> n; 51 | numbers.push_back(n); 52 | } 53 | solve(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /UVa 10189 - Minesweeper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int Case = 1; 8 | int n, m; 9 | while (cin >> n >> m, n > 0 && m > 0) 10 | { 11 | // "There must be an empty line between field outputs." 12 | if (Case > 1) 13 | cout << endl; 14 | 15 | // Use borders around the field. 16 | vector > field(n + 2, vector(m + 2, '.')); 17 | for (int r = 1; r <= n; ++r) 18 | for (int c = 1; c <= m; ++c) 19 | cin >> field[r][c]; 20 | 21 | cout << "Field #" << Case++ << ":" << endl; 22 | for (int r = 1; r <= n; ++r) 23 | { 24 | for (int c = 1; c <= m; ++c) 25 | { 26 | if (field[r][c] == '*') 27 | cout << '*'; 28 | else 29 | { 30 | int mines = 0; 31 | for (int i = r - 1; i <= r + 1; ++i) 32 | for (int j = c - 1; j <= c + 1; ++j) 33 | if (field[i][j] == '*') 34 | ++mines; 35 | cout << mines; 36 | } 37 | } 38 | cout << endl; 39 | } 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /UVa 10260 - Soundex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Prepare the mapping of Soundex coding. 8 | string soundex[6] = { "BFPV", 9 | "CGJKQSXZ", 10 | "DT", 11 | "L", 12 | "MN", 13 | "R" }; 14 | size_t soundexSize = sizeof(soundex) / sizeof(soundex[0]); 15 | 16 | string word; 17 | while (cin >> word) 18 | { 19 | string code; 20 | size_t prevIndex = soundexSize; 21 | for (size_t i = 0; i < word.size(); ++i) 22 | { 23 | size_t j = 0; 24 | for ( ; j < soundexSize; ++j) 25 | { 26 | if (soundex[j].find(word[i]) != string::npos) 27 | { 28 | if (j != prevIndex) 29 | code.push_back(static_cast('0' + j + 1)); 30 | prevIndex = j; 31 | break; 32 | } 33 | } 34 | if (j == soundexSize) 35 | prevIndex = soundexSize; 36 | } 37 | cout << code << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa 10264 - The Most Potent Corner.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int N; 9 | while (cin >> N) 10 | { 11 | vector weights; 12 | for (int corner = 0; corner < 1 << N; ++corner) 13 | { 14 | int weight; 15 | cin >> weight; 16 | weights.push_back(weight); 17 | } 18 | // Compute each corner's potency. 19 | vector potencies; 20 | for (int corner = 0; corner < 1 << N; ++corner) 21 | { 22 | int potency = 0; 23 | // "Potency of the corner is the sum of weights of all neighbouring corners." 24 | for (int digit = 0; digit < N; ++digit) 25 | potency += weights[corner ^ (1 << digit)] ; 26 | potencies.push_back(potency); 27 | } 28 | // Find the maximum potencies sum. 29 | int maxPotencySum = 0; 30 | for (int corner = 0; corner < 1 << N; ++corner) 31 | { 32 | for (int digit = 0; digit < N; ++digit) 33 | maxPotencySum = max(maxPotencySum, potencies[corner] + potencies[corner ^ (1 << digit)]); 34 | } 35 | cout << maxPotencySum << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 10279 - Mine Sweeper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | size_t T; 8 | cin >> T; 9 | while ( T-- ) 10 | { 11 | size_t n; 12 | cin >> n; 13 | vector > board(n + 2, vector((n + 2), '.')); 14 | vector > game(n + 2, vector((n + 2), '.')); 15 | bool gameOver = false; 16 | for (size_t r = 1; r <= n; ++r) 17 | for (size_t c = 1; c <= n; ++c) 18 | cin >> board[r][c]; 19 | 20 | for (size_t r = 1; r <= n; ++r) 21 | for (size_t c = 1; c <= n; ++c) 22 | { 23 | char g; 24 | cin >> g; 25 | // Oops, a mine has been touched. 26 | if (g == 'x' && board[r][c] == '*') 27 | gameOver = true; 28 | game[r][c] = g; 29 | } 30 | for (size_t r = 1; r <= n; ++r) 31 | { 32 | for (size_t c = 1; c <= n; ++c) 33 | { 34 | if (board[r][c] == '*') 35 | cout << (gameOver? '*' : '.'); 36 | else if (game[r][c] == '.') 37 | cout << '.'; 38 | else 39 | { 40 | int mines = 0; 41 | for (size_t i = r - 1; i <= r + 1; ++i) 42 | for (size_t j = c - 1; j <= c + 1; ++j) 43 | { 44 | if (board[i][j] == '*') 45 | ++mines; 46 | } 47 | cout << mines; 48 | } 49 | } 50 | cout << endl; 51 | } 52 | if (T) 53 | cout << endl; 54 | } 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /UVa 10282 - Babelfish.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | map memo; 10 | string line; 11 | while (getline(cin, line), line != "") 12 | { 13 | stringstream ss(line); 14 | string foreignWord, englishWord; 15 | ss >> englishWord >> foreignWord;; 16 | memo[foreignWord] = englishWord; 17 | } 18 | while (getline(cin, line)) 19 | { 20 | if (memo.find(line) == memo.end()) 21 | cout << "eh" << endl; 22 | else 23 | cout << memo[line] << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa 10295 - Hay Points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t m, n; 9 | cin >> m >> n; 10 | map memo; 11 | for (size_t i = 0; i < m; ++i) 12 | { 13 | string word; 14 | int value; 15 | cin >> word >> value; 16 | memo[word] = value; 17 | } 18 | for (size_t i = 0; i < n; ++i) 19 | { 20 | string word; 21 | int salary = 0; 22 | while (cin >> word, word != ".") 23 | salary += memo[word]; 24 | 25 | cout << salary << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /UVa 10304 - Optimal Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int N; 8 | int F[250 + 5]; 9 | int sum[250 + 5][250 + 5]; 10 | int dp[250 + 5][250 + 5];; 11 | 12 | int solve() 13 | { 14 | memset(dp, 0, sizeof(dp)); 15 | for (int i = 1; i <= N; ++i) 16 | for (int j = i + 1; j <= N; ++j) 17 | dp[i][j] = numeric_limits::max(); 18 | 19 | for (int interval = 1; interval < N; ++interval) 20 | for (int i = 1; i + interval <= N; ++i) 21 | { 22 | // Try to take each node between i and j as the root 23 | // and store the minimum cost in dp[i][j]. 24 | int j = i + interval; 25 | for (int root = i; root <= j; ++root) 26 | dp[i][j] = min(dp[i][j], 27 | dp[i][root - 1] + dp[root + 1][j] 28 | + sum[i][j] - F[root]); 29 | } 30 | 31 | return dp[1][N]; 32 | } 33 | 34 | int main() 35 | { 36 | while (cin >> N) 37 | { 38 | for (int i = 1; i <= N; ++i) 39 | { 40 | cin >> F[i]; 41 | sum[i][i] = F[i]; 42 | for (int j = 1; j <= i - 1; ++ j) 43 | sum[j][i] = sum[j][i - 1] + F[i]; 44 | } 45 | cout << solve() << endl; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /UVa 10305 - Ordering Tasks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void DFS(vector &tasks, 8 | const vector > &adjList, 9 | vector &order, 10 | int t) 11 | { 12 | tasks[t] = true; 13 | for (int i = 0; i < adjList[t].size(); ++i) 14 | { 15 | int next = adjList[t][i]; 16 | if (! tasks[next]) 17 | DFS(tasks, adjList, order, next); 18 | } 19 | order.push_back(t); 20 | } 21 | 22 | int main() 23 | { 24 | int m, n; 25 | while (cin >> n >> m, !(n == 0 && m == 0)) 26 | { 27 | vector tasks(n + 1, false); 28 | vector > adjList(n + 1); 29 | vector order; 30 | for (int i = 0; i < m; ++i) 31 | { 32 | int t1, t2; 33 | cin >> t1 >> t2; 34 | adjList[t1].push_back(t2); 35 | } 36 | // Typical topological sort. 37 | for (int t = 1; t <= n; ++t) 38 | if (! tasks[t]) 39 | DFS(tasks, adjList, order, t); 40 | 41 | copy(order.rbegin(), order.rend(), ostream_iterator(cout, " ")); 42 | cout << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /UVa 10313 - Pay the Price.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static const int MAX_VALUE = 300; 8 | 9 | int main() 10 | { 11 | // dp[i][j] stores the number of ways i dollars can be 12 | // paid using 0..j coins. 13 | typedef unsigned long long ull_t; 14 | vector > dp(MAX_VALUE + 1, 15 | vector(MAX_VALUE + 1, 0)); 16 | // Base cases. 17 | for (int j = 0; j <= MAX_VALUE; ++j) 18 | dp[0][j] = 1; 19 | for (int i = 1; i <= MAX_VALUE; ++i) 20 | for (int j = 1; j <= MAX_VALUE; ++j) 21 | dp[i][j] = dp[i][j - 1] + (i >= j? dp[i - j][j] : 0); 22 | 23 | string s; 24 | while (getline(cin, s)) 25 | { 26 | vector N; 27 | stringstream ss(s); 28 | int input; 29 | while (ss >> input) 30 | N.push_back(input); 31 | 32 | int L1, L2; 33 | switch(N.size()) 34 | { 35 | case 1: 36 | L1 = 0; 37 | L2 = N[0]; 38 | break; 39 | case 2: 40 | L1 = 0; 41 | L2 = N[1]; 42 | break; 43 | default: 44 | L1 = N[1]; 45 | L2 = N[2]; 46 | } 47 | // Can be improved by checking cases like 48 | // 0 100 200 49 | if (N[0] == 0) 50 | cout << "1" << endl; 51 | else 52 | cout << dp[N[0]][min(300, L2)] - dp[N[0]][min(300, max(0, L1 - 1))] << endl; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /UVa 10324 - Zeros and Ones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t Case = 1; 9 | string bitString; 10 | while (cin >> bitString) 11 | { 12 | size_t n; 13 | cin >> n; 14 | cout << "Case " 15 | << Case++ 16 | << ":" 17 | << endl; 18 | while ( n-- ) 19 | { 20 | size_t i, j; 21 | cin >> i >> j; 22 | if (i > j) 23 | swap(i, j); 24 | // Get the sub-string between position i and j. 25 | string subString = bitString.substr(i, j - i + 1); 26 | if (subString.size() <= 1 27 | || unique(subString.begin(), subString.end()) == 28 | subString.begin() + 1 29 | ) 30 | { 31 | cout << "Yes" << endl; 32 | } 33 | else 34 | cout << "No" << endl; 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 10341 - Solve It.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double f(double x, 7 | double p, 8 | double q, 9 | double r, 10 | double s, 11 | double t, 12 | double u) 13 | { 14 | return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u; 15 | } 16 | 17 | int main() 18 | { 19 | double p, q, r, s, t, u; 20 | while (cin >> p >> q >> r >> s >> t >> u) 21 | { 22 | // Perform Bisection Method. 23 | // f(0) = 1, f(1) = -1. 24 | double U = 1.0, L = 0.0; 25 | while (U - L >= 1e-9) 26 | { 27 | double m = (L + U) / 2.0; 28 | double value = f(m, p, q, r, s, t, u); 29 | if (value < 0) 30 | U = m; 31 | else 32 | L = m; 33 | } 34 | if (abs(f((L + U) / 2, p, q, r, s, t, u)) <= 1e-4) 35 | cout << setprecision(4) 36 | << fixed 37 | << (L + U) / 2.0 38 | << endl; 39 | else 40 | cout << "No solution" << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa 10364 - Square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int M, sum, sticks[20]; 7 | int memo[1 << 20]; 8 | 9 | int search(int length, int bitMask) 10 | { 11 | if (memo[bitMask] != -1) 12 | return memo[bitMask]; 13 | 14 | if (length > sum / 4) 15 | return 0; 16 | else if (length == sum / 4) 17 | { 18 | if (bitMask == (1 << M) - 1) 19 | return 1; 20 | length = 0; 21 | } 22 | 23 | for (int i = 0; i < M; ++i) 24 | if ((bitMask & 1 << i) == 0 25 | && search(length + sticks[i], bitMask | 1 << i)) 26 | { 27 | return 1; 28 | } 29 | return memo[bitMask] = 0; 30 | } 31 | 32 | int solve() 33 | { 34 | sum = accumulate(sticks, sticks + M, 0); 35 | if (sum % 4 != 0) 36 | return 0; 37 | 38 | memset(memo, -1, sizeof memo); 39 | return search(0, 0); 40 | } 41 | 42 | int main() 43 | { 44 | int N; 45 | cin >> N; 46 | while ( N-- ) 47 | { 48 | cin >> M; 49 | for (int i = 0; i < M; ++i) 50 | cin >> sticks[i]; 51 | 52 | cout << (solve()? "yes" : "no") << endl; 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /UVa 10377 - Maze Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static const char WALL = '*'; 8 | static const char DIR[] = {'N', 'E', 'S', 'W'}; 9 | static const int DX[] = {-1, 0, 1, 0}; 10 | static const int DY[] = { 0, 1, 0, -1}; 11 | 12 | void F(const vector > &maze, 13 | int &x, int &y, int dir) 14 | { 15 | int tx = x + DX[dir]; 16 | int ty = y + DY[dir]; 17 | if (maze[tx][ty] == WALL) 18 | return; 19 | else 20 | { 21 | x = tx; 22 | y = ty; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int T; 29 | cin >> T; 30 | while ( T-- ) 31 | { 32 | int m, n; 33 | cin >> m >> n; 34 | cin.ignore(); 35 | 36 | vector > maze(m + 1, vector(n + 1)); 37 | for (int x = 1; x <= m; ++x) 38 | { 39 | string s; 40 | getline(cin, s); 41 | for (int y = 1; y <= n; ++y) 42 | maze[x][y] = s[y - 1]; 43 | } 44 | 45 | int posX, posY; 46 | int dir = 0; 47 | cin >> posX >> posY; 48 | cin.ignore(); 49 | 50 | string s; 51 | while (getline(cin, s)) 52 | { 53 | // Remove all empty spaces. 54 | s.erase(remove(s.begin(), s.end(), ' '), s.end()); 55 | for (size_t i = 0; i < s.size(); ++i) 56 | { 57 | if (s[i] == 'R') 58 | dir = (dir + 1) % 4; 59 | else if (s[i] == 'L') 60 | dir = (dir + 3) % 4; 61 | else if (s[i] == 'F') 62 | F(maze, posX, posY, dir); 63 | else 64 | cout << posX << " " << posY << " " << DIR[dir] << endl; 65 | } 66 | if (s[s.size() - 1] == 'Q') 67 | break; 68 | } 69 | if (T) 70 | cout << endl; 71 | } 72 | return 0; 73 | } -------------------------------------------------------------------------------- /UVa 10382 - Watering Grass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Interval 8 | { 9 | double L, R; 10 | }; 11 | 12 | bool comp(const Interval &i1, const Interval &i2) 13 | { 14 | return i1.L < i2.L; 15 | } 16 | 17 | int main() 18 | { 19 | size_t n, l, w; 20 | while (cin >> n >> l >> w) 21 | { 22 | vector circles(1); 23 | for (size_t i = 0; i < n; ++i) 24 | { 25 | double pos, radius; 26 | cin >> pos >> radius; 27 | // Calculate this circle's effective interval [L, R]. 28 | // Then this problem is identical to 10020 - Minimal coverage. 29 | double range = sqrt(radius * radius - (w / 2.0) * (w / 2.0)); 30 | circles[0].L = pos - range; 31 | circles[0].R = pos + range; 32 | circles.push_back(circles[0]); 33 | } 34 | // Sort the circles by increasing left endpoint. 35 | sort(circles.begin() + 1, circles.end(), comp); 36 | 37 | double curL = 0, rReach = 0; 38 | size_t i = 1; 39 | size_t nSprinklers = 0; 40 | while (rReach < l) 41 | { 42 | double newCurL = curL; 43 | size_t farthest = 0; 44 | // Take the interval that covers as far right as possible. 45 | for (; i < circles.size(); ++i) 46 | { 47 | if (circles[i].L > curL) 48 | break; 49 | if (circles[i].R >= newCurL) 50 | { 51 | newCurL = circles[i].R; 52 | farthest = i; 53 | } 54 | } 55 | if (farthest == 0) 56 | break; 57 | ++nSprinklers; 58 | rReach = curL = newCurL; 59 | } 60 | if (rReach < l) 61 | cout << "-1" << endl; 62 | else 63 | cout << nSprinklers << endl; 64 | } 65 | return 0; 66 | } -------------------------------------------------------------------------------- /UVa 10394 - Twin Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int BITSET_SIZE = 20000000; 7 | bitset isPrime; 8 | 9 | int main() 10 | { 11 | isPrime.set(); 12 | isPrime.reset(0); 13 | isPrime.reset(1); 14 | 15 | // Maximal int is 2,147,483,647, larger than 20,000,000. 16 | vector > twinPrimes; 17 | int lastPrime = 2; 18 | 19 | for (unsigned n = 2; n < BITSET_SIZE; ++n) 20 | { 21 | if (isPrime[n]) 22 | { 23 | // Need to handle n * n carefully. 24 | if ((unsigned long long)n * n < BITSET_SIZE) 25 | { 26 | for (int m = n * n; m < BITSET_SIZE; m += n) 27 | isPrime.reset(m); 28 | } 29 | 30 | if (n - lastPrime == 2) 31 | twinPrimes.push_back(pair(lastPrime, n)); 32 | 33 | lastPrime = n; 34 | } 35 | } 36 | 37 | int S; 38 | while (cin >> S) 39 | { 40 | cout << "(" 41 | << twinPrimes[S - 1].first 42 | << ", " 43 | << twinPrimes[S - 1].second 44 | << ")" 45 | << endl; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /UVa 10407 - Simple division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool comp(const int &a, const int &b) 8 | { 9 | return abs(a) < abs(b); 10 | } 11 | 12 | inline int gcd(int a, int b) 13 | { 14 | return b == 0? a : gcd(b, a % b); 15 | } 16 | 17 | int main() 18 | { 19 | vector sequence; 20 | int s; 21 | while (cin >> s, s != 0) 22 | { 23 | sequence.clear(); 24 | sequence.push_back(s); 25 | while (cin >> s, s != 0) 26 | sequence.push_back(s); 27 | 28 | // Sort the sequence by absolute values. 29 | sort(sequence.begin(), sequence.end(), comp); 30 | 31 | /** 32 | Rearrange the sequence by their differences. 33 | For example, 34 | 35 | sequence = { 2, 5, 11, 26 } 36 | differences = { 3, 6, 15 } 37 | 38 | where 3 = 5 - 2, 6 = 11 - 5, 15 = 26 - 11. 39 | */ 40 | for (size_t i = 0; i < sequence.size() - 1; ++i) 41 | sequence[i] = abs(sequence[i + 1] - sequence[i]); 42 | 43 | // Erase the last element. 44 | sequence.erase(sequence.end() - 1); 45 | 46 | int g = sequence[0]; 47 | for (size_t i = 1; i < sequence.size(); ++i) 48 | g = gcd(g, sequence[i]); 49 | 50 | cout << g << endl; 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /UVa 10409 - Die Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | size_t T; 8 | while (cin >> T, T > 0) 9 | { 10 | int top = 1, north = 2, west = 3; 11 | while ( T-- ) 12 | { 13 | string s; 14 | cin >> s; 15 | // 2 16 | // 3 1 4 17 | // 5 18 | // 6 is on 1's opposite side. 19 | int lastTop = top; 20 | if (s == "north") 21 | { 22 | top = 7 - north; 23 | north = lastTop; 24 | } 25 | else if (s == "south") 26 | { 27 | top = north; 28 | north = 7 - lastTop; 29 | } 30 | else if (s == "west") 31 | { 32 | top = 7 - west; 33 | west = lastTop; 34 | } 35 | else if (s == "east") 36 | { 37 | top = west; 38 | west = 7 - lastTop; 39 | } 40 | } 41 | cout << top << endl; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa 10419 - Sum-up the Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int N, t; 10 | vector primes; 11 | bool dp[150][1000+5][14]; 12 | int path[14]; 13 | 14 | bool comp(int a, int b) 15 | { 16 | stringstream ss1; 17 | stringstream ss2; 18 | ss1 << a; 19 | ss2 << b; 20 | return ss1.str() < ss2.str(); 21 | } 22 | 23 | // idx: index of "primes". 24 | // sum: the current sum. 25 | // r: the number of chosen primes. 26 | bool search(int idx, int sum, int r) 27 | { 28 | if (idx == primes.size()) 29 | return false; 30 | 31 | if (r == t) 32 | { 33 | if (sum == N) 34 | return true; 35 | return false; 36 | } 37 | 38 | if (sum >= N) 39 | return false; 40 | 41 | if (!dp[idx][sum][r]) 42 | return false; 43 | 44 | path[r] = primes[idx]; 45 | if (search(idx + 1, sum + primes[idx], r + 1)) 46 | return true; 47 | if (search(idx + 1, sum, r)) 48 | return true; 49 | 50 | return dp[idx][sum][r] = false; 51 | } 52 | 53 | void solve() 54 | { 55 | memset(dp, true, sizeof(dp)); 56 | if (search(0, 0, 0)) 57 | { 58 | cout << path[0]; 59 | for (int i = 1; i < t; ++i) 60 | cout << "+" << path[i]; 61 | cout << endl; 62 | } 63 | else 64 | cout << "No Solution." << endl; 65 | } 66 | 67 | int main() 68 | { 69 | bool isPrime[300]; 70 | memset(isPrime, true, sizeof(isPrime)); 71 | 72 | for (int i = 2; i * i < 300; ++i) 73 | if (isPrime[i]) 74 | for (int j = i + i; j < 300; j += i) 75 | isPrime[j] = false; 76 | 77 | primes.push_back(2); 78 | for (int i = 3; i < 300; ++i) 79 | if (isPrime[i]) 80 | { 81 | primes.push_back(i); 82 | primes.push_back(i); 83 | } 84 | 85 | sort(primes.begin(), primes.end(), comp); 86 | 87 | int Case = 1; 88 | while (cin >> N >> t, !(N == 0 && t == 0)) 89 | { 90 | cout << "CASE " << Case++ << ":" << endl; 91 | solve(); 92 | } 93 | } -------------------------------------------------------------------------------- /UVa 10424 - Love Calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int love(string s) 9 | { 10 | int sum = 0; 11 | for (size_t i = 0; i < s.size(); ++i) 12 | { 13 | if (s[i] >= 'a' && s[i] <= 'z') 14 | sum += s[i] - 'a' + 1; 15 | else if (s[i] >= 'A' && s[i] <= 'Z') 16 | sum += s[i] - 'A' + 1; 17 | } 18 | // Using modulo 9 to efficiently compute digit's sum. 19 | if (sum % 9 == 0) 20 | return 9; 21 | else 22 | return sum % 9; 23 | } 24 | 25 | int main() 26 | { 27 | string s1, s2; 28 | // Note that the input can be an empty string, but 29 | // cin >> s1 won't take an empty string. 30 | while (getline(cin, s1)) 31 | { 32 | getline(cin, s2); 33 | int r1 = love(s1), r2 = love(s2); 34 | double ratio = min((double)r1 / r2, (double)r2 / r1); 35 | 36 | cout << setprecision(2) 37 | << setiosflags(ios_base::fixed) 38 | << ratio * 100.0 39 | << " %" 40 | << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa 10450 - World Cup Noise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | unsigned long long pattern[51]; 8 | pattern[1] = 2; 9 | pattern[2] = 3; 10 | for (int i = 3; i < 51; ++i) 11 | pattern[i] = pattern[i - 1] + pattern[i - 2]; 12 | 13 | int T, Case = 1; 14 | cin >> T; 15 | while ( T-- ) 16 | { 17 | cout << "Scenario #" << Case++ << ":" << endl; 18 | 19 | int n; 20 | cin >> n; 21 | cout << pattern[n] << endl << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa 10465 - Homer Simpson.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | static const int ARRAY_SIZE = 18432; 6 | 7 | /** memo[i] stores the maximum number of burgers Homer can eat 8 | without having beer. */ 9 | int memo[ARRAY_SIZE]; 10 | 11 | int main() 12 | { 13 | int m, n, t; 14 | while (cin >> m >> n >> t) 15 | { 16 | memset(memo, -1, sizeof memo); 17 | memo[0] = 0; 18 | for (int i = 1; i <= t; ++i) 19 | { 20 | // Each number i can be reached by eating a burger for 21 | // m or n seconds. 22 | int burger1 = (i - m >= 0? memo[i - m] : -1); 23 | if (burger1 >= 0) 24 | ++burger1; 25 | 26 | int burger2 = (i - n >= 0? memo[i - n] : -1); 27 | if (burger2 >= 0) 28 | ++burger2; 29 | 30 | memo[i] = max(burger1, burger2); 31 | } 32 | if (memo[t] != -1) 33 | cout << memo[t] << endl; 34 | else 35 | { 36 | // Search the number of burgers closest to t. 37 | int i = t; 38 | while (memo[--i] == -1) 39 | {} 40 | cout << memo[i] 41 | << " " 42 | << t - i // The time Homer gets for drinking. 43 | << endl; 44 | } 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa 10474 - Where is the Marble.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | size_t Case = 1; 10 | size_t N, Q; 11 | while (cin >> N >> Q, !(N == 0 && Q == 0)) 12 | { 13 | vector marbles(N); 14 | for (size_t i = 0; i < N; ++i) 15 | cin >> marbles[i]; 16 | 17 | sort(marbles.begin(), marbles.end()); 18 | cout << "CASE# " << Case++ << ":" << endl; 19 | for (size_t i = 0; i < Q; ++i) 20 | { 21 | int query; 22 | cin >> query; 23 | pair::iterator, vector::iterator> range; 24 | range = equal_range(marbles.begin(), marbles.end(), query); 25 | if (range.first == range.second) 26 | cout << query << " not found" << endl; 27 | else 28 | cout << query 29 | << " found at " 30 | << distance(marbles.begin(), range.first) + 1 31 | << endl; 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa 10487 - Closest Sums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int dist(int n1, int n2) 7 | { 8 | return n1 >= n2? n1 - n2 : n2 - n1; 9 | } 10 | 11 | int main() 12 | { 13 | size_t Case = 1, n; 14 | while (cin >> n, n > 0) 15 | { 16 | cout << "Case " << Case++ << ":" << endl; 17 | 18 | vector numbers(n); 19 | for (size_t i = 0; i < n; ++i) 20 | cin >> numbers[i]; 21 | 22 | sort(numbers.begin(), numbers.end()); 23 | 24 | size_t m; 25 | cin >> m; 26 | while ( m-- ) 27 | { 28 | int query; 29 | cin >> query; 30 | size_t L = 0, U = numbers.size() - 1; 31 | 32 | int sum = 0; 33 | if (numbers[0] != numbers.back()) 34 | { 35 | sum = numbers[0] + numbers.back(); 36 | // Find a sum as close to query as possible. 37 | while (L < U) 38 | { 39 | // "a sum of two distinct numbers" 40 | if (numbers[L] != numbers[U] 41 | && dist(query, numbers[L] + numbers[U]) 42 | < dist(query, sum)) 43 | { 44 | sum = numbers[L] + numbers[U]; 45 | } 46 | if (numbers[L] + numbers[U] == query) 47 | break; 48 | else if (numbers[L] + numbers[U] < query) 49 | ++L; 50 | else 51 | --U; 52 | } 53 | } 54 | cout << "Closest sum to " 55 | << query 56 | << " is " 57 | << sum 58 | << "." 59 | << endl; 60 | } 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /UVa 10491 - Cows and Cars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int NCOWS, NCARS, NSHOW; 8 | while (cin >> NCOWS >> NCARS >> NSHOW) 9 | { 10 | // Two cases. 11 | // 1.Pick a cow and switch to a car: 12 | // NCOWS / (NCOWS + NCARS) 13 | // * NCARS / (NCOWS + NCARS - NSHOW - 1) 14 | // 2.Pick a car and switch to a car: 15 | // NCARS / (NCOWS + NCARS) 16 | // * (NCARS - 1) / (NCOWS + NCARS - NSHOW - 1) 17 | cout << setprecision(5) 18 | << fixed 19 | << ((double)NCARS * (NCOWS + NCARS - 1)) / ((NCOWS + NCARS) * (NCOWS + NCARS - NSHOW - 1)) 20 | << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /UVa 10520 - Determine it.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static const int MAX_N = 19; 8 | 9 | typedef unsigned long long ull_t; 10 | ull_t memo[MAX_N + 2][MAX_N + 2]; 11 | int n; 12 | 13 | ull_t a(int i, int j) 14 | { 15 | if (memo[i][j] != -1) 16 | return memo[i][j]; 17 | 18 | if (i >= j) 19 | { 20 | ull_t v1 = 0, v2 = 0; 21 | for (int k = i + 1; k <= n; ++k) 22 | v1 = max(v1, a(k, 1) + a(k, j)); 23 | for (int k = 1; k < j; ++k) 24 | v2 = max(v2, a(i, k) + a(n, k)); 25 | return memo[i][j] = v1 + v2; 26 | } 27 | else 28 | { 29 | ull_t v = a(i, i) + a(i + 1, j); 30 | for (int k = i + 1; k < j; ++k) 31 | v = max(v, a(i, k) + a(k + 1, j)); 32 | return memo[i][j] = v; 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | int a_n1; 39 | while (cin >> n >> a_n1) 40 | { 41 | memset(memo, -1, sizeof memo); 42 | memo[n][1] = a_n1; 43 | cout << a(1, n) << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa 10530 - Guessing Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Lower bound and upper bound. 8 | int L = 1, U = 10; 9 | int guess; 10 | while (cin >> guess, guess != 0) 11 | { 12 | string response; 13 | cin.ignore(); 14 | getline(cin, response); 15 | if (response == "too high") 16 | U = min(U, guess - 1); 17 | else if (response == "too low") 18 | L = max(L, guess + 1); 19 | else if (response == "right on") 20 | { 21 | if (guess <= U && guess >= L) 22 | cout << "Stan may be honest" << endl; 23 | else 24 | cout << "Stan is dishonest" << endl; 25 | L = 1; 26 | U = 10; 27 | } 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /UVa 10534 - Wavio Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // LIS[i] stores the maximum length of S's LIS ending at i. 7 | void doLIS(const vector &S, vector &LIS) 8 | { 9 | vector L(S.size()); 10 | size_t lisCount = 0; 11 | for (size_t i = 0; i < S.size(); ++i) 12 | { 13 | size_t pos = lower_bound(L.begin(), L.begin() + lisCount, S[i]) 14 | - L.begin(); 15 | L[pos] = S[i]; 16 | if (pos == lisCount) 17 | ++lisCount; 18 | LIS[i] = pos + 1; 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | size_t N; 25 | while (cin >> N) 26 | { 27 | vector S(N); 28 | for (int i = 0; i < N; ++i) 29 | cin >> S[i]; 30 | 31 | vector LIS(N), LDS(N); 32 | doLIS(S, LIS); 33 | reverse(S.begin(), S.end()); 34 | doLIS(S, LDS); 35 | reverse(LDS.begin(), LDS.end()); 36 | 37 | size_t maxLIS = 0; 38 | for (size_t i = 0; i < LIS.size(); ++i) 39 | { 40 | /** Suppose S[] = 1 3 6 5 41 | LIS[] = 1 2 3 3 42 | LDS[] = 1 1 2 1 43 | then we can pick 1 6 5 or 3 6 5. 44 | */ 45 | maxLIS = max(maxLIS, 2 * min(LIS[i], LDS[i]) - 1); 46 | } 47 | cout << maxLIS << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa 10550 - Combination Lock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | static const int DEGREE_PER_MARK = 360 / 40; 5 | enum DIRECTION { 6 | CLOCKWISE, 7 | COUNTER_CLOCKWISE 8 | }; 9 | 10 | 11 | int turn(int from, int to, DIRECTION dir) 12 | { 13 | if (dir == CLOCKWISE) 14 | { 15 | if (to >= from) 16 | return (from + (40 - to)) * DEGREE_PER_MARK; 17 | else 18 | return (from - to) * DEGREE_PER_MARK; 19 | } 20 | else 21 | { 22 | if (to >= from) 23 | return (to - from) * DEGREE_PER_MARK; 24 | else 25 | return ((40 - from) + to) * DEGREE_PER_MARK; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int p1, p2, p3, p4; 32 | while (cin >> p1 >> p2 >> p3 >> p4, p1 + p2 + p3 + p4 > 0) 33 | { 34 | // Turn the dial clockwise 2 full turns. 35 | int totalDegree = 360 * 2; 36 | totalDegree += turn(p1, p2, CLOCKWISE); 37 | // Turn the dial counter-clockwise 1 full turn. 38 | totalDegree += 360; 39 | totalDegree += turn(p2, p3, COUNTER_CLOCKWISE); 40 | totalDegree += turn(p3, p4, CLOCKWISE); 41 | cout << totalDegree << endl; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa 10566 - Crossed Ladders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const double EPS = 1e-9; 7 | 8 | int main() 9 | { 10 | double x, y, c; 11 | while (cin >> x >> y >> c) 12 | { 13 | double U = x < y? x : y; 14 | double L = 0; 15 | while (U >= L) 16 | { 17 | // M is the guessed width. 18 | // W is the accordingly calculated width. 19 | double M = (L + U) / 2; 20 | double W = c / sqrt(x * x - M * M) * M 21 | + c / sqrt(y * y - M * M) * M; 22 | 23 | if (abs(W - M) < EPS) 24 | { 25 | cout << setprecision(3) << fixed << W << endl; 26 | break; 27 | } 28 | (W > M? U : L) = M; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa 10591 - Happy Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int f(int n) 6 | { 7 | int sum = 0; 8 | while (n) 9 | { 10 | int d = n % 10; 11 | sum += d * d; 12 | n /= 10; 13 | } 14 | return sum; 15 | } 16 | 17 | int main() 18 | { 19 | int T, Case = 1; 20 | cin >> T; 21 | while ( T-- ) 22 | { 23 | int N, n; 24 | cin >> N; 25 | n = N; 26 | 27 | map memo; 28 | while (memo.count(n) == 0 29 | && n != 1) 30 | { 31 | memo[n] = 1; 32 | n = f(n); 33 | } 34 | cout << "Case #" << Case++ << ": " << N; 35 | if (n == 1) 36 | cout << " is a Happy number." << endl; 37 | else 38 | cout << " is an Unhappy number." << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /UVa 10611 - The Playboy Chimp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template 7 | int searchLastSmallerK(const vector &A, T k) 8 | { 9 | int l = 0, r = (int)A.size() - 1, res = -1; 10 | while (l <= r) 11 | { 12 | int m = l + ((r - l) >> 1); 13 | if (A[m] < k) 14 | { 15 | res = A[m]; 16 | l = m + 1; 17 | } 18 | else 19 | r = m - 1; 20 | } 21 | return res; 22 | } 23 | 24 | template 25 | int searchFirstLargerK(const vector &A, T k) 26 | { 27 | int l = 0, r = (int)A.size() - 1, res = -1; 28 | while (l <= r) 29 | { 30 | int m = l + ((r - l) >> 1); 31 | if (A[m] > k) 32 | { 33 | res = A[m]; 34 | r = m - 1; 35 | } 36 | else 37 | l = m + 1; 38 | } 39 | return res; 40 | } 41 | 42 | int main() 43 | { 44 | int N; 45 | while (cin >> N) 46 | { 47 | vector chimps(N); 48 | for (int i = 0; i < N; ++i) 49 | cin >> chimps[i]; 50 | 51 | sort(chimps.begin(), chimps.end()); 52 | 53 | int Q; 54 | cin >> Q; 55 | while ( Q-- ) 56 | { 57 | unsigned int q; 58 | cin >> q; 59 | 60 | unsigned int s, h; 61 | s = searchLastSmallerK(chimps, q); 62 | h = searchFirstLargerK(chimps, q); 63 | 64 | if (s == -1) 65 | cout << "X"; 66 | else 67 | cout << s; 68 | 69 | cout << " "; 70 | 71 | if (h == -1) 72 | cout << "X"; 73 | else 74 | cout << h; 75 | 76 | cout << endl; 77 | } 78 | } 79 | return 0; 80 | } -------------------------------------------------------------------------------- /UVa 10617 - Again Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef vector > vvi_t; 7 | 8 | int main() 9 | { 10 | size_t T; 11 | cin >> T; 12 | while ( T-- ) 13 | { 14 | string s; 15 | cin >> s; 16 | // dp[i][j] stores the number of subsequences of s[i..j] 17 | // that are palidromes. 18 | vvi_t dp(s.size(), vector(s.size(), 1)); 19 | // Initialize Base cases. 20 | for (size_t i = 0; i < s.size() - 1; ++i) 21 | dp[i][i + 1] = (s[i] == s[i + 1]? 3 : 2); 22 | 23 | // Bottom-up DP. 24 | for (size_t len = 2; len < s.size(); ++len) 25 | for (size_t i = 0; i + len < s.size(); ++i) 26 | { 27 | size_t j = i + len; 28 | // 1. i j 29 | // 2. i [i+1... j-1] j <= cancelled out with 3 + 4. 30 | // 3.[i ....... j-1] 31 | // 4. [i+1 ....... j] 32 | if (s[i] == s[j]) 33 | dp[i][j] = 1 + dp[i][j - 1] + dp[i + 1][j]; 34 | // 1.[i ............ j-1] 35 | // 2. [i+1 ............ j] 36 | // [i+1 ........ j-1] <= added twice 37 | else 38 | dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1]; 39 | } 40 | 41 | cout << dp[0][s.size() - 1] << endl; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa 10664 - Luggage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int m; 11 | cin >> m; 12 | cin.ignore(); 13 | while ( m-- ) 14 | { 15 | string s; 16 | getline(cin, s); 17 | stringstream ss(s); 18 | 19 | vector sums(1, 0); 20 | int sum = 0; 21 | int k; 22 | while (ss >> k) 23 | { 24 | sum += k; 25 | size_t size = sums.size(); 26 | // Consider all possible sums. 27 | for (size_t i = 0; i < size; ++i) 28 | sums.push_back(sums[i] + k); 29 | } 30 | if (sum % 2 == 0 31 | && find(sums.begin(), sums.end(), sum / 2) != sums.end()) 32 | { 33 | cout << "YES" << endl; 34 | } 35 | else 36 | cout << "NO" << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /UVa 10690 - Expression Again.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool dp[100 + 5][100 * 100 + 5]; 8 | 9 | int main() 10 | { 11 | int N, M; 12 | while (cin >> N >> M) 13 | { 14 | vector ints(N + M + 1); 15 | int sum = 0; 16 | for (int i = 1; i <= N + M; ++i) 17 | { 18 | cin >> ints[i]; 19 | sum += ints[i]; 20 | // Shift all integers by +50. 21 | ints[i] += 50; 22 | } 23 | 24 | memset(dp, false, sizeof(dp)); 25 | dp[0][0] = true; 26 | 27 | // If dp[i][j] is true, that means it is possible to 28 | // use i out of the N + M integers to sum to j. 29 | for (int i = 1; i <= N + M; ++i) 30 | for (int k = min(i, N); k >= 1; --k) 31 | for (int j = 0; j <= 10000; ++j) 32 | if (dp[k - 1][j]) 33 | dp[k][j + ints[i]] = true; 34 | 35 | int maximum = -5000; 36 | int minimum = 5000; 37 | for (int i = 0; i <= 10000; ++i) 38 | if (dp[N][i]) 39 | { 40 | int nSum = i - 50 * N; 41 | maximum = max(maximum, nSum * (sum - nSum)); 42 | minimum = min(minimum, nSum * (sum - nSum)); 43 | } 44 | 45 | cout << maximum << " " << minimum << endl; 46 | } 47 | } -------------------------------------------------------------------------------- /UVa 10703 - Free spots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int W, H, N; 9 | while (cin >> W >> H >> N, W != 0) 10 | { 11 | // Initialize and fill the board with 1. 12 | vector > board(W, vector(H, 0)); 13 | int emptySpots = W * H; 14 | for (int i = 0; i < N; ++i) 15 | { 16 | int X1, Y1, X2, Y2; 17 | cin >> X1 >> Y1 >> X2 >> Y2; 18 | // Reset the sub-portion to 0. 19 | for (int x = min(X1, X2) - 1; x <= max(X1, X2) - 1; ++x) 20 | for (int y = min(Y1, Y2) - 1; y <= max(Y1, Y2) - 1; ++y) 21 | { 22 | if (board[x][y] == 0) 23 | { 24 | --emptySpots; 25 | board[x][y] = 1; 26 | } 27 | } 28 | } 29 | if (emptySpots == 0) 30 | cout << "There is no empty spots." << endl; 31 | else if (emptySpots == 1) 32 | cout << "There is one empty spot." << endl; 33 | else 34 | cout << "There are " 35 | << emptySpots 36 | << " empty spots." 37 | << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa 10706 - Number Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Return the number of digits of k. 7 | size_t nDigits(size_t k) 8 | { 9 | return static_cast(log10(static_cast(k)) + 0e-9) + 1; 10 | } 11 | 12 | int main() 13 | { 14 | vector S(1, 0); 15 | S.push_back(1); 16 | size_t digitCount = 1; 17 | for (size_t k = 2; ; ++k) 18 | { 19 | digitCount += nDigits(k); 20 | S.push_back(S.back() + digitCount); 21 | // "1 <= i <= 2147483647" 22 | if (S.back() + digitCount >= 2147483647) 23 | break; 24 | } 25 | 26 | size_t t; 27 | cin >> t; 28 | while ( t-- ) 29 | { 30 | size_t i; 31 | cin >> i; 32 | 33 | size_t L = 0, U = S.size() - 1, d = 1; 34 | while (L <= U) 35 | { 36 | size_t m = (L + U) / 2; 37 | if (S[m] < i) 38 | { 39 | d = i - S[m]; 40 | L = m + 1; 41 | } 42 | else 43 | U = m - 1; 44 | } 45 | // Locate the integer where the dth digit resides. 46 | size_t integer = 1; 47 | while (d > nDigits(integer)) 48 | d -= nDigits(integer++); 49 | // Get the value of the digit. 50 | d = nDigits(integer) - d + 1; 51 | size_t value = 0; 52 | while (d--) 53 | { 54 | value = integer % 10; 55 | integer /= 10; 56 | } 57 | cout << value << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /UVa 10721 - Bar Codes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /** 6 | memo[i][j] stores the number of symbols that the ith bar ends at 7 | the jth unit. 8 | */ 9 | long long memo[64][64]; 10 | 11 | int main() 12 | { 13 | int n, k, m; 14 | while (cin >> n >> k >> m) 15 | { 16 | memset(memo, 0, sizeof memo); 17 | // There is only 1 symbol that the 1st bar ends at any unit 18 | // equal or less than n and m. 19 | for (int unit = 1; unit <= n && unit <= m; ++unit) 20 | memo[1][unit] = 1; 21 | 22 | // Every memo[bar][unit] can be reached by adding one bar of 23 | // at most m units wide. 24 | for (int bar = 2; bar <= k; ++bar) 25 | for (int unit = bar; unit <= n; ++unit) 26 | for (int wide = 1; wide <= m && unit > wide; ++wide) 27 | memo[bar][unit] += memo[bar - 1][unit - wide]; 28 | 29 | cout << memo[k][n] << endl; 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /UVa 10763 - Foreign Exchange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | while (cin >> n, n != 0) 10 | { 11 | vector original(n), target(n); 12 | for (int i = 0; i < n; ++i) 13 | cin >> original[i] >> target[i]; 14 | 15 | sort(original.begin(), original.end()); 16 | sort(target.begin(), target.end()); 17 | 18 | if (original == target) 19 | cout << "YES" << endl; 20 | else 21 | cout << "NO" << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa 10819 - Trouble of 13-Dots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int m, n; 9 | while (cin >> m >> n) 10 | { 11 | vector P(n), F(n); 12 | for (size_t i = 0; i < n; ++i) 13 | cin >> P[i] >> F[i]; 14 | 15 | int U = m; 16 | if (U > 1800) 17 | U += 200; 18 | 19 | // dp[i] stores the maximum favour value of spending exactly 20 | // $i. Consider the case: 21 | // 1900 3 22 | // 2000 5 23 | // 1950 1 * 24 | // 101 1 * 25 | // The answer is 2 not 5 because even if 13-Dots has $1900 26 | // she must spend more than $2000 to get the $200 refund. 27 | vector dp(U + 1, 0); 28 | for (int i = 0; i < n; ++i) 29 | for (int j = U; j >= P[i]; --j) 30 | { 31 | if (dp[j - P[i]] > 0 || j == P[i]) 32 | dp[j] = max(dp[j], dp[j - P[i]] + F[i]); 33 | } 34 | 35 | int maxF = 0; 36 | // If 13-Dots has money between $1801 and $2000, check $2001 37 | // up to U. 38 | if (m >= 1801 && m <= 2000) 39 | { 40 | maxF = *max_element(dp.begin(), dp.begin() + m); 41 | for (int j = 2001; j <= U; ++j) 42 | maxF = max(maxF, dp[j]); 43 | } 44 | else 45 | maxF = *max_element(dp.begin(), dp.end()); 46 | 47 | cout << maxF << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa 10892 - LCM Cardinality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef unsigned long long ull; 6 | 7 | int main() 8 | { 9 | ull N; 10 | while (cin >> N, N != 0) 11 | { 12 | // Backup N since we are going to work on N directly. 13 | ull NBackUp = N; 14 | 15 | vector factors; 16 | vector powers; 17 | for (ull f = 2; f * f <= N; ++f) 18 | { 19 | int p = 0; 20 | while (N % f == 0) 21 | { 22 | ++p; 23 | N /= f; 24 | } 25 | // If f is N's prime factor, its power p must be larger than 0. 26 | if (p > 0) 27 | { 28 | factors.push_back(f); 29 | powers.push_back(p); 30 | } 31 | } 32 | // If N is a prime number itself. 33 | if (N > 1) 34 | { 35 | factors.push_back(N); 36 | powers.push_back(1); 37 | } 38 | 39 | /** 40 | Loop over each factor to compute N's LCM cardinality. 41 | For example, 42 | 43 | Suppose N = 12 = 2^2 * 3 44 | Then C = (2 * 3) + (1 * 1) + 1 = 8 45 | 46 | where the last 1 is the pair (12, 12). 47 | Refer to the explanation at http://www.algorithmist.com/index.php/UVa_10892. 48 | */ 49 | ull C = 0; 50 | for (size_t i = 0; i < factors.size(); ++i) 51 | { 52 | ull sum = powers[i]; 53 | for (size_t j = i + 1; j < factors.size(); ++j) 54 | { 55 | sum *= 2 * powers[j] + 1; 56 | } 57 | C += sum; 58 | } 59 | C += 1; 60 | 61 | cout << NBackUp << " " << C << endl; 62 | } 63 | return 0; 64 | } -------------------------------------------------------------------------------- /UVa 10901 - Ferry Loading III.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | static const int LEFT = 0; 9 | static const int RIGHT = 1; 10 | 11 | int main() 12 | { 13 | int c; 14 | cin >> c; 15 | while ( c-- ) 16 | { 17 | int n, t, m; 18 | cin >> n >> t >> m; 19 | 20 | // Use two queues to store the cars. 21 | vector > > cars(2); 22 | vector uploadedTime(m); 23 | for (int i = 0; i < m; ++i) 24 | { 25 | int arrivalTime; 26 | string bank; 27 | cin >> arrivalTime >> bank; 28 | if (bank == "left") 29 | cars[LEFT].push(pair(i, arrivalTime)); 30 | else 31 | cars[RIGHT].push(pair(i, arrivalTime)); 32 | } 33 | 34 | int time = 0; 35 | // "The ferry is initially on the left bank." 36 | int index = LEFT; 37 | while (! (cars[LEFT].empty() && cars[RIGHT].empty())) 38 | { 39 | // "If there are no cars waiting on either bank, 40 | // the ferry waits until one arrives,.." 41 | int earliest_car = numeric_limits::max(); 42 | if (! cars[LEFT].empty()) 43 | earliest_car = cars[LEFT].front().second; 44 | if (! cars[RIGHT].empty()) 45 | earliest_car = min(earliest_car, cars[RIGHT].front().second); 46 | 47 | time = max(time, earliest_car); 48 | 49 | int carCount = 0; 50 | while (! cars[index].empty() 51 | && carCount < n 52 | && cars[index].front().second <= time) 53 | { 54 | uploadedTime[cars[index].front().first] = time + t; 55 | cars[index].pop(); 56 | ++carCount; 57 | } 58 | time += t; 59 | index ^= 1; 60 | } 61 | 62 | for (int i = 0; i < m; ++i) 63 | cout << uploadedTime[i] << endl; 64 | // "Output an empty line between cases." 65 | if (c) 66 | cout << endl; 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /UVa 10905 - Children's Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool comp(const string &s1, const string &s2) 9 | { 10 | // Suppose s1 = 900, s2 = 9, then it compares 11 | // 9900 with 9009. 12 | return s2 + s1 < s1 + s2; 13 | } 14 | 15 | int main() 16 | { 17 | size_t N; 18 | while (cin >> N, N > 0) 19 | { 20 | vector integers(N); 21 | for (size_t i = 0; i < N; ++i) 22 | cin >> integers[i]; 23 | 24 | sort(integers.begin(), integers.end(), comp); 25 | cout << accumulate(integers.begin(), integers.end(), static_cast("")) << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /UVa 10912 - Simple Minded Hashing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | static const int MAX_VALUE = ((1 + 26) * 26) / 2; 6 | int memo[27][27][MAX_VALUE + 1]; 7 | 8 | int main() 9 | { 10 | memset(memo, 0, sizeof memo); 11 | for (int i = 1; i <= 26; ++i) 12 | memo[i][1][i] = 1; 13 | /** 14 | memo[i][l][s] stores the number of strings of length l 15 | and value s, considering up to letter i. So, 16 | memo[i][l][s] = memo[i - 1][l][s] 17 | + memo[i - 1][l - 1][s - i] 18 | 19 | The base cases are memo[i][1][i] = 1 for i = 1 to 26. 20 | */ 21 | for (int i = 2; i <= 26; ++i) 22 | for (int l = 1; l <= i; ++l) 23 | for (int s = 1; s <= MAX_VALUE; ++s) 24 | { 25 | // Letter i is not selected. 26 | memo[i][l][s] += memo[i - 1][l][s]; 27 | // Letter i is selected. 28 | if (l >= 1 && s >= i) 29 | memo[i][l][s] += memo[i - 1][l - 1][s - i]; 30 | } 31 | 32 | int T = 1, L, S; 33 | while (cin >> L >> S, !(L == 0 && S == 0)) 34 | { 35 | cout << "Case " 36 | << T++ 37 | << ": "; 38 | if (L > 26 || S > MAX_VALUE) 39 | cout << "0" << endl; 40 | else 41 | cout << memo[26][L][S] << endl; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa 10919 - Prerequisites.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int k, m; 9 | while (cin >> k, k > 0) 10 | { 11 | cin >> m; 12 | map courses; 13 | // Freddie's chosen courses. 14 | for (int i = 0; i < k; ++i) 15 | { 16 | string course; 17 | cin >> course; 18 | ++courses[course]; 19 | } 20 | // Check each category. 21 | bool graduate = true; 22 | for (int i = 0; i < m; ++i) 23 | { 24 | int c, r; 25 | cin >> c >> r; 26 | for (int j = 0; j < c; ++j) 27 | { 28 | string course; 29 | cin >> course; 30 | if (courses.find(course) != courses.end()) 31 | --r; 32 | } 33 | if (r > 0) 34 | graduate = false; 35 | } 36 | cout << (graduate? "yes" : "no") << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /UVa 10945 - Mother bear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isPalindrome(const string &s) 7 | { 8 | string r_s(s); 9 | reverse(r_s.begin(), r_s.end()); 10 | return s == r_s; 11 | } 12 | 13 | bool isIgnoredChar(char c) 14 | { 15 | switch(c) 16 | { 17 | case ' ': 18 | case '.': 19 | case ',': 20 | case '!': 21 | case '?': 22 | return true; 23 | default: 24 | return false; 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | string s; 31 | while (getline(cin, s), s != "DONE") 32 | { 33 | // Remove ignored chars. 34 | s.erase(remove_if(s.begin(), s.end(), isIgnoredChar), s.end()); 35 | // Convert all chars to lowercase. 36 | transform(s.begin(), s.end(), s.begin(), ::tolower); 37 | if (isPalindrome(s)) 38 | cout << "You won't be eaten!" << endl; 39 | else 40 | cout << "Uh oh.." << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa 10946 - You want what filled.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef vector > vvc_t; 7 | typedef vector > vvi_t; 8 | 9 | const int UNVISITED = 0; 10 | const int VISITED = 1; 11 | 12 | int dx[] = {-1, 0, 1, 0}; 13 | int dy[] = { 0, 1, 0, -1}; 14 | 15 | int dfs(vvc_t &map, char h, int x, int y) 16 | { 17 | map[x][y] = '.'; 18 | int holeSize = 1; 19 | for (int i = 0; i < 4; ++i) 20 | { 21 | int tx = x + dx[i]; 22 | int ty = y + dy[i]; 23 | if (tx < 0 || tx >= map.size() || ty < 0 || ty >= map[0].size()) 24 | continue; 25 | if (map[tx][ty] == h) 26 | holeSize += dfs(map, h, tx, ty); 27 | } 28 | return holeSize; 29 | } 30 | 31 | bool comp(const pair &h1, const pair &h2) 32 | { 33 | if (h1.first > h2.first) 34 | return true; 35 | else if (h1.first == h2.first && h1.second < h2.second) 36 | return true; 37 | 38 | return false; 39 | } 40 | 41 | int main() 42 | { 43 | int Case = 1; 44 | int x, y; 45 | while (cin >> x >> y, !(x == 0 && y == 0)) 46 | { 47 | vvc_t map(x, vector(y)); 48 | for (int r = 0; r < x; ++r) 49 | for (int c = 0; c < y; ++c) 50 | cin >> map[r][c]; 51 | 52 | vector > holes; 53 | for (int r = 0; r < x; ++r) 54 | for (int c = 0; c < y; ++c) 55 | if (map[r][c] != '.') 56 | { 57 | char h = map[r][c]; 58 | holes.push_back(make_pair(dfs(map, map[r][c], r, c), h)); 59 | } 60 | // "sorted by the size of the hole, break ties by sorting 61 | // the characters in alphabetical order" 62 | sort(holes.begin(), holes.end(), comp); 63 | 64 | cout << "Problem " << Case++ << ":" << endl; 65 | for (int i = 0; i < holes.size(); ++i) 66 | cout << holes[i].second 67 | << " " 68 | << holes[i].first 69 | << endl; 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /UVa 10976 - Fractions Again.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int k; 8 | while (cin >> k) 9 | { 10 | /** 1/k = 1/x + 1/y 11 | When x and y are equal, they are both 2k. 12 | 13 | 1/k = 1/(2k) + 1/(2k) 14 | 15 | if x is larger than 2k, then y must be smaller than 2k. 16 | Since y can't be k, we only need to check y in [k+1, 2k]. 17 | But how to compute x? 18 | 19 | 1/x = 1/k - 1/y 20 | = (y-k) / (k*y) 21 | 22 | So if y-k divides k*y, then x = (k*y) / (y-k). 23 | */ 24 | vector > solutions; 25 | for (int y = k + 1; y <= 2 * k; ++y) 26 | { 27 | if ((k * y) % (y - k) == 0) 28 | solutions.push_back(make_pair((k * y) / (y - k), y)); 29 | } 30 | cout << solutions.size() << endl; 31 | for (size_t i = 0; i < solutions.size(); ++i) 32 | cout << "1/" 33 | << k 34 | << " = 1/" 35 | << solutions[i].first 36 | << " + 1/" 37 | << solutions[i].second 38 | << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa 10980 - Lowest Price in Town.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | static const int MAX_K = 100; 10 | 11 | int main() 12 | { 13 | int Case = 1, M; 14 | double unitPrice; 15 | while (cin >> unitPrice >> M) 16 | { 17 | // Get the schemes. 18 | typedef pair scheme_t; 19 | vector schemes(M); 20 | for (int i = 0; i < M; ++i) 21 | cin >> schemes[i].first >> schemes[i].second; 22 | 23 | // dp[k] stores the the lowest price to get k items. 24 | vector dp(MAX_K + 1, 0); 25 | // Base cases. 26 | for (int k = 1; k <= MAX_K; ++k) 27 | dp[k] = unitPrice * k; 28 | for (int i = 0; i < M; ++i) 29 | { 30 | int n = schemes[i].first; 31 | double p = schemes[i].second; 32 | for (int k = 0; k <= MAX_K - n; ++k) 33 | // Consider the case 34 | // 2 22.00 35 | // 4 40.00 36 | // For K = 3, $40 is the best deal. 37 | for (int m = 1; m <= n; ++m) 38 | dp[k + m] = min(dp[k + m], dp[k] + p); 39 | } 40 | 41 | cin.ignore(); 42 | 43 | string s; 44 | int K; 45 | getline(cin, s); 46 | stringstream ss(s); 47 | cout << "Case " << Case++ << ":" << endl; 48 | while (ss >> K) 49 | { 50 | cout << "Buy " 51 | << K 52 | << " for $" 53 | << setprecision(2) 54 | << fixed 55 | << dp[K] 56 | << endl; 57 | } 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /UVa 11003 - Boxes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int MAX_WEIGHT = 3000, MAX_LOAD = 3000; 7 | 8 | int main() 9 | { 10 | int N; 11 | while (cin >> N, N != 0) 12 | { 13 | // dp[i] stores the maximum number of boxes that can be 14 | // stacked up for exactly total weight i. 15 | vector W(N + 1), L(N + 1); 16 | for (int i = 1; i <= N; ++i) 17 | cin >> W[i] >> L[i]; 18 | 19 | vector dp(MAX_WEIGHT + MAX_LOAD + 1, 0); 20 | // Condisder box N, box N-1..to box 1. 21 | for (int box = N; box >= 1; --box) 22 | { 23 | for (int load = L[box]; load >= 0; --load) 24 | { 25 | if (dp[load]) 26 | dp[load + W[box]] = max(dp[load + W[box]], // The original best stack. 27 | dp[load] + 1); // The box stack. 28 | } 29 | if (dp[W[box]] == 0) 30 | dp[W[box]] = 1; 31 | } 32 | cout << *max_element(dp.begin(), dp.end()) << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa 11034 - Ferry Loading IV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static const int LEFT = 0; 8 | static const int RIGHT = 1; 9 | 10 | int main() 11 | { 12 | int c; 13 | cin >> c; 14 | while ( c-- ) 15 | { 16 | int l, m; 17 | cin >> l >> m; 18 | l *= 100; 19 | 20 | // Use two queues to store the cars. 21 | vector > cars(2); 22 | for (int i = 0; i < m; ++i) 23 | { 24 | int length; 25 | string bank; 26 | cin >> length >> bank; 27 | if (bank == "left") 28 | cars[LEFT].push(length); 29 | else 30 | cars[RIGHT].push(length); 31 | } 32 | 33 | // "The ferry is initially on the left bank" 34 | int index = LEFT; 35 | int crossRiver = 0; 36 | while (! (cars[LEFT].empty() && cars[RIGHT].empty())) 37 | { 38 | int totalLength = 0; 39 | while (! cars[index].empty() 40 | && totalLength + cars[index].front() <= l) 41 | { 42 | totalLength += cars[index].front(); 43 | cars[index].pop(); 44 | } 45 | ++crossRiver; 46 | index ^= 1; 47 | } 48 | cout << crossRiver << endl; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /UVa 11057 - Exact Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t N; 9 | while (cin >> N) 10 | { 11 | vector books(N); 12 | for (size_t b = 0; b < N; ++b) 13 | cin >> books[b]; 14 | 15 | int M; 16 | cin >> M; 17 | 18 | sort(books.begin(), books.end()); 19 | int i = books.front(); 20 | int j = books.back(); 21 | for (size_t b = 0; b < N; ++b) 22 | { 23 | int b1 = books[b]; 24 | int b2 = M - b1; 25 | if (b1 > b2) 26 | break; 27 | // Search the books after b. 28 | if (binary_search(books.begin() + b + 1, books.end(), b2)) 29 | { 30 | if (b2 - b1 < j - i) 31 | { 32 | i = b1; 33 | j = b2; 34 | } 35 | } 36 | } 37 | cout << "Peter should buy books whose prices are " 38 | << i 39 | << " and " 40 | << j 41 | << "." 42 | << endl 43 | << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa 11059 - Maximum Product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t Case = 1, N; 9 | while (cin >> N) 10 | { 11 | vector seq(N); 12 | for (size_t i = 0; i < N; ++i) 13 | cin >> seq[i]; 14 | 15 | // Try all possibilities. 16 | long long product = 0; 17 | for (size_t i = 0; i < seq.size(); ++i) 18 | for (size_t j = i; j < seq.size(); ++j) 19 | { 20 | long long temp = 1; 21 | for (size_t k = i; k <= j; ++k) 22 | temp *= seq[k]; 23 | 24 | product = max(product, temp); 25 | } 26 | 27 | cout << "Case #" 28 | << Case++ 29 | << ": The maximum product is " 30 | << product 31 | << "." 32 | << endl 33 | << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa 11080 - Place the Guards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int BLACK = 0; 7 | static const int WHITE = 1; 8 | static const int UNKNOWN = 2; 9 | 10 | int main() 11 | { 12 | int T; 13 | cin >> T; 14 | while ( T-- ) 15 | { 16 | int v, e; 17 | cin >> v >> e; 18 | 19 | vector > G(v); 20 | vector color(v, UNKNOWN); 21 | bool isBipartite = true; 22 | 23 | while ( e-- ) 24 | { 25 | int f, t; 26 | cin >> f >> t; 27 | G[f].push_back(t); 28 | G[t].push_back(f); 29 | } 30 | 31 | int totalCount = 0; 32 | for (int j = 0; j < G.size() && isBipartite; ++j) 33 | { 34 | if (color[j] != UNKNOWN) 35 | continue; 36 | 37 | queue q; 38 | int count[2] = {0}; 39 | color[j] = BLACK; 40 | ++count[color[j]]; 41 | q.push(j); 42 | while (!q.empty() && isBipartite) 43 | { 44 | int u = q.front(); 45 | q.pop(); 46 | for (int i = 0; i < G[u].size(); ++i) 47 | { 48 | int v = G[u][i]; 49 | if (color[v] == color[u]) 50 | { 51 | isBipartite = false; 52 | break; 53 | } 54 | else if (color[v] == UNKNOWN) 55 | { 56 | color[v] = 1 - color[u]; 57 | ++count[color[v]]; 58 | q.push(v); 59 | } 60 | } 61 | } 62 | // Each connected component needs at least 1 guard. 63 | totalCount += max(1, min(count[BLACK], count[WHITE])); 64 | } 65 | 66 | if (isBipartite) 67 | cout << totalCount << endl; 68 | else 69 | cout << -1 << endl; 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /UVa 11084 - Anagram Division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int N; 8 | int S[10]; 9 | int D; 10 | int dp[10][1 << 10][1024]; 11 | 12 | int search(int idx, int bitMask, int sum) 13 | { 14 | if (idx == N) 15 | return sum % D == 0? 1 : 0; 16 | 17 | // Key ideas: 18 | // 1. 123 % 10 = 100 % 10 + 20 % 10 + 3 % 10 19 | // 2. Suppose S is 123456 and we got 10 permutations for 20 | // 123400. Then 432100 has also 10 permutations if 21 | // 432100 % D has the same value with 123400 % D. 22 | if (sum < 1024 23 | && dp[idx][bitMask][sum] != -1) 24 | return dp[idx][bitMask][sum]; 25 | 26 | int nPermu = 0; 27 | bool used[10] = {0}; 28 | for (int i = 0; i < N; ++i) 29 | { 30 | if (bitMask & (1 << i)) 31 | continue; 32 | if (used[S[i]]) 33 | continue; 34 | used[S[i]] = true; 35 | 36 | nPermu += search(idx + 1, 37 | bitMask | (1 << i), 38 | (sum + (long long)(pow(10.0, idx) + 1e-9) * S[i]) % D); 39 | } 40 | 41 | if (sum < 1024) 42 | dp[idx][bitMask][sum] = nPermu; 43 | 44 | return nPermu; 45 | } 46 | 47 | int solve() 48 | { 49 | memset(dp, -1, sizeof(dp)); 50 | return search(0, 0, 0); 51 | } 52 | 53 | int main() 54 | { 55 | int T; 56 | cin >> T; 57 | while ( T-- ) 58 | { 59 | string s; 60 | cin >> s >> D; 61 | 62 | N = (int)s.size(); 63 | for (int i = 0; i < s.size(); ++i) 64 | S[i] = s[i] - '0'; 65 | 66 | cout << solve() << endl; 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /UVa 11151 - Longest Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t T; 9 | cin >> T; 10 | cin.ignore(); 11 | while ( T-- ) 12 | { 13 | string s; 14 | getline(cin, s); 15 | 16 | if (s == "") 17 | { 18 | cout << "0" << endl; 19 | continue; 20 | } 21 | 22 | // dp[i][j] stores the length of the longest palindrome from 23 | // string A[i..j]. 24 | // Initialize base cases. 25 | vector > dp(s.size(), vector(s.size(), 1)); 26 | for (int i = 0; i < s.size() - 1; ++i) 27 | dp[i][i + 1] = (s[i] == s[i + 1]? 2 : 1); 28 | 29 | // Bottom-up DP. 30 | for (int len = 2; len < s.size(); ++len) 31 | for (int i = 0; i + len < s.size(); ++i) 32 | { 33 | int j = i + len; 34 | if (s[i] == s[j]) 35 | dp[i][j] = 2 + dp[i + 1][j - 1]; 36 | else 37 | dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); 38 | } 39 | cout << dp[0][s.size() - 1] << endl; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /UVa 11192 - Group Reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t G; 9 | while (cin >> G, G > 0) 10 | { 11 | string s; 12 | cin >> s; 13 | 14 | string::iterator iter(s.begin()); 15 | size_t groupSize = s.size() / G; 16 | 17 | for ( ; iter != s.end(); iter += groupSize) 18 | reverse(iter, iter + groupSize); 19 | 20 | cout << s << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /UVa 11218 - KTV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int n; 7 | vector > C(81, vector(3)); 8 | int S[81]; 9 | 10 | int solve() 11 | { 12 | int maximum = -1; 13 | for (int i = 0; i < n; ++i) 14 | for (int j = i + 1; j < n; ++j) 15 | for (int k = j + 1; k < n; ++k) 16 | { 17 | // Do the teams C[i], C[j] and C[k] include all 18 | // 9 people? 19 | vector p(10, false); 20 | p[C[i][0]] = true; 21 | p[C[i][1]] = true; 22 | p[C[i][2]] = true; 23 | p[C[j][0]] = true; 24 | p[C[j][1]] = true; 25 | p[C[j][2]] = true; 26 | p[C[k][0]] = true; 27 | p[C[k][1]] = true; 28 | p[C[k][2]] = true; 29 | if (count(p.begin(), p.end(), true) == 9) 30 | maximum = max(maximum, S[i] + S[j] + S[k]); 31 | } 32 | return maximum; 33 | } 34 | 35 | int main() 36 | { 37 | int Case = 1; 38 | while (cin >> n, n > 0) 39 | { 40 | for (int i = 0; i < n; ++i) 41 | cin >> C[i][0] >> C[i][1] >> C[i][2] >> S[i]; 42 | 43 | cout << "Case " << Case++ << ": " << solve() << endl; 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /UVa 11219 - How old are you.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int diff(int nowD, int nowM, int nowY, 5 | int birthD, int birthM, int birthY) 6 | { 7 | return nowD + 30 * nowM + 360 * nowY 8 | - birthD - 30 * birthM - 360 * birthY; 9 | } 10 | 11 | int main() 12 | { 13 | size_t Case = 1; 14 | size_t T; 15 | cin >> T; 16 | while ( T-- ) 17 | { 18 | int nowD, nowM, nowY; 19 | int birthD, birthM, birthY; 20 | char c; 21 | cin >> nowD >> c >> nowM >> c >> nowY; 22 | cin >> birthD >> c >> birthM >> c >> birthY; 23 | 24 | cout << "Case #" << Case++ << ": "; 25 | int dateGap = diff(nowD, nowM, nowY, birthD, birthM, birthY); 26 | if (dateGap < 0) 27 | cout << "Invalid birth date" << endl; 28 | else if (dateGap / 360 > 130) 29 | cout << "Check birth date" << endl; 30 | else 31 | cout << dateGap / 360 << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /UVa 11221 - Magic square palindromes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | bool isMSPalindrom(const string &s, size_t K) 10 | { 11 | // s.size() must be a square number. 12 | if (K * K != s.size()) 13 | return false; 14 | if (K == 1) 15 | return true; 16 | 17 | for (size_t add = 1; add <= K; add *= K) 18 | { 19 | string::const_iterator iter(s.begin()); 20 | string::const_reverse_iterator r_iter(s.rbegin()); 21 | for (size_t addCount = 0; addCount < K; ++addCount) 22 | { 23 | if (*iter != *r_iter) 24 | return false; 25 | iter += add; 26 | r_iter += add; 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | bool isIgnoredChar(char c) 33 | { 34 | if (c < 'a' || c > 'z') 35 | return true; 36 | return false; 37 | } 38 | 39 | int main() 40 | { 41 | string s; 42 | getline(cin, s); 43 | stringstream ss(s); 44 | size_t T; 45 | ss >> T; 46 | size_t Case = 1; 47 | while ( T-- ) 48 | { 49 | getline(cin, s); 50 | // Remove all whitespace and punctuation symbols. 51 | s.erase(remove_if(s.begin(), s.end(), isIgnoredChar), s.end()); 52 | size_t K = static_cast(sqrt(static_cast(s.size()) + 0e-9)); 53 | cout << "Case #" << Case++ << ":" << endl; 54 | if (isMSPalindrom(s, K)) 55 | cout << K << endl; 56 | else 57 | cout << "No magic :(" << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /UVa 1124 - Celebrity jeopardy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string equation; 8 | while (getline(cin, equation)) 9 | cout << equation << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /UVa 11264 - Coin Collector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int T; 8 | cin >> T; 9 | while ( T-- ) 10 | { 11 | int n; 12 | cin >> n; 13 | 14 | vector values; 15 | for (int i = 0; i < n; ++i) 16 | { 17 | int value; 18 | cin >> value; 19 | values.push_back(value); 20 | } 21 | 22 | int sum = 1; 23 | int coins = values.size() > 1? 2 : 1; 24 | // Get as more coins of smaller values as possible. 25 | for (int i = 1; i < values.size() - 1; ++i) 26 | if (sum + values[i] < values[i + 1]) 27 | { 28 | sum += values[i]; 29 | ++coins; 30 | } 31 | 32 | cout << coins << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa 11309 - Counting Chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool isPalindrome(const string &s) 9 | { 10 | // Locate the first char that is greater than '0'. 11 | string::const_iterator iter(find_if(s.begin(), s.end(), 12 | bind2nd(greater(), '0'))); 13 | string::const_reverse_iterator r_iter(s.rbegin()); 14 | // Loop until iter and r_iter cross each other. 15 | for (; distance(s.begin(), r_iter.base()) 16 | > distance(s.begin(), iter); ++iter, ++r_iter) 17 | { 18 | if (*iter != *r_iter) 19 | return false; 20 | } 21 | return true; 22 | } 23 | 24 | string NextPT(int hour, int min) 25 | { 26 | for (int h = hour; h < 24; ++h) 27 | { 28 | for (int m = min + 1; m < 60; ++m) 29 | { 30 | stringstream ss; 31 | if (h < 10) 32 | ss << "0"; 33 | ss << h; 34 | if (m < 10) 35 | ss << "0"; 36 | ss << m; 37 | // Insert ":" in the middle. 38 | if (isPalindrome(ss.str())) 39 | return ss.str().insert(2, ":"); 40 | } 41 | min = -1; 42 | } 43 | return "00:00"; 44 | } 45 | 46 | int main() 47 | { 48 | size_t n; 49 | cin >> n; 50 | while ( n-- ) 51 | { 52 | int h, m; 53 | char c; 54 | cin >> h >> c >> m; 55 | cout << NextPT(h, m) << endl; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /UVa 11321 - Sort Sort and Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct comp 9 | { 10 | bool operator()(const int &n1, const int &n2) 11 | { 12 | int mod_n1 = n1 % M; 13 | int mod_n2 = n2 % M; 14 | if (mod_n1 < mod_n2) 15 | return true; 16 | else if (mod_n1 == mod_n2) 17 | { 18 | if (abs(n1 % 2) == 1) 19 | { 20 | if (n2 % 2 == 0) 21 | return true; 22 | // "larger odd number will precede the smaller odd number." 23 | return n1 > n2; 24 | } 25 | else if (n1 % 2 == 0) 26 | { 27 | // "smaller even number will precede the larger even number." 28 | if (n2 % 2 == 0) 29 | return n1 < n2; 30 | } 31 | } 32 | return false; 33 | } 34 | static int M; 35 | }; 36 | int comp::M; 37 | 38 | int main() 39 | { 40 | size_t N; 41 | while (cin >> N >> comp::M, N > 0 && comp::M > 0) 42 | { 43 | vector numbers(N); 44 | for (size_t i = 0; i < N; ++i) 45 | cin >> numbers[i]; 46 | 47 | sort(numbers.begin(), numbers.end(), comp()); 48 | cout << N << " " << comp::M << endl; 49 | copy(numbers.begin(), numbers.end(), ostream_iterator(cout, "\n")); 50 | } 51 | cout << "0 0" << endl; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /UVa 11349 - Symmetric Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Return true if M is symmetric; false, otherwise. 6 | // n can be even or odd. 7 | template 8 | bool symmetric(const vector > &M) 9 | { 10 | size_t n = M.size(); 11 | 12 | for (size_t r = 0; r < (n + 1) / 2; ++r) 13 | for (size_t c = 0; c < (r == n / 2? (n + 1) / 2 : n); ++c) 14 | { 15 | if (M[r][c] < 0 16 | || M[r][c] != M[n - 1 - r][n - 1 - c]) 17 | { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | 24 | int main() 25 | { 26 | size_t T, Test = 1; 27 | cin >> T; 28 | while ( T-- ) 29 | { 30 | char c; 31 | size_t n; 32 | cin >> c >> c >> n; 33 | vector > M(n, vector(n)); 34 | for (int r = 0; r < n; ++r) 35 | for (int c = 0; c < n; ++c) 36 | cin >> M[r][c]; 37 | 38 | cout << "Test #" << Test++ << ": "; 39 | if (symmetric(M)) 40 | cout << "Symmetric." << endl; 41 | else 42 | cout << "Non-symmetric." << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /UVa 11364 - Parking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t t; 9 | cin >> t; 10 | while ( t-- ) 11 | { 12 | size_t n; 13 | cin >> n; 14 | vector stores(n); 15 | for (size_t i = 0; i < n; ++i) 16 | { 17 | cin >> stores[i]; 18 | } 19 | cout << 2 * (*max_element(stores.begin(), stores.end()) 20 | - *min_element(stores.begin(), stores.end())) 21 | << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa 11389 - The Bus Driver Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n, d, r; 9 | while (cin >> n >> d >> r, !(n == 0 && d == 0 && r == 0)) 10 | { 11 | vector mRoute(n); 12 | vector eRoute(n); 13 | 14 | for (int i = 0; i < n; ++i) 15 | cin >> mRoute[i]; 16 | for (int i = 0; i < n; ++i) 17 | cin >> eRoute[i]; 18 | 19 | // Make each driver work as long as possible. 20 | sort(mRoute.begin(), mRoute.end()); 21 | sort(eRoute.rbegin(), eRoute.rend()); 22 | 23 | int overTimePay = 0; 24 | for (int i = 0; i < n; ++i) 25 | { 26 | int totalHours = mRoute[i] + eRoute[i]; 27 | overTimePay += totalHours > d? 28 | (totalHours - d) * r : 0; 29 | } 30 | cout << overTimePay << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa 11407 - Squares.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 1 <= N <= 10000 6 | int memo[10001]; 7 | 8 | int main() 9 | { 10 | // Initialize memo[] 11 | for (int i = 1; i <= 10000; ++i) 12 | { 13 | // If i is a square number, memo[i] = 1; Otherwise memo[i] = i; 14 | int squareRoot = static_cast(sqrt((double)i)); 15 | if (squareRoot * squareRoot == i) 16 | memo[i] = 1; 17 | else 18 | memo[i] = i; 19 | } 20 | for (int i = 2; i <= 10000; ++i) 21 | { 22 | if (memo[i] == 1) 23 | continue; 24 | // Check every square number smaller than i. 25 | for (int j = 1; j * j < i; ++j) 26 | memo[i] = min(memo[i], 1 + memo[i - j * j]); 27 | } 28 | 29 | int t; 30 | cin >> t; 31 | while ( t-- ) 32 | { 33 | int N; 34 | cin >> N; 35 | cout << memo[N] << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 11413 - Fill the Containers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Can the vessels fill all the m containers, each has capacity C? 6 | bool FillAllContainers(const vector &vessels, 7 | int m, 8 | int C) 9 | { 10 | int container = 1; 11 | int capacity = C; 12 | for (int i = 0; i < vessels.size(); ++i) 13 | { 14 | // No container can contain so much milk. 15 | if (vessels[i] > C) 16 | return false; 17 | 18 | if (vessels[i] > capacity) 19 | { 20 | // Already m containers are filled. 21 | if (container == m) 22 | return false; 23 | ++container; 24 | capacity = C; 25 | } 26 | capacity -= vessels[i]; 27 | } 28 | return true; 29 | } 30 | 31 | int main() 32 | { 33 | int n, m; 34 | while (cin >> n >> m) 35 | { 36 | vector vessels(n); 37 | for (int i = 0; i < n; ++i) 38 | cin >> vessels[i]; 39 | 40 | // The capacity 1<=c<=1000000000. 41 | int L = 1, U = 1000000000, C = 0; 42 | while (L <= U) 43 | { 44 | int mid = (L + U) / 2; 45 | if (FillAllContainers(vessels, m, mid)) 46 | { 47 | C = mid; 48 | U = mid - 1; 49 | } 50 | else 51 | L = mid + 1; 52 | } 53 | cout << C << endl; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /UVa 11456 - Trainsorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | // Return the maximum length of LIS/LDS ending at S's last element. 9 | template 10 | size_t LIDS(const vector &S, T comp) 11 | { 12 | vector L(S.size()); 13 | size_t lisCount = 0, length = 0; 14 | for (size_t i = 0; i < S.size(); ++i) 15 | { 16 | size_t pos = lower_bound(L.begin(), L.begin() + lisCount, S[i], comp) 17 | - L.begin(); 18 | L[pos] = S[i]; 19 | length = pos + 1; 20 | if (pos == lisCount) 21 | ++lisCount; 22 | } 23 | return length; 24 | } 25 | 26 | int main() 27 | { 28 | size_t T; 29 | cin >> T; 30 | while ( T-- ) 31 | { 32 | size_t n; 33 | cin >> n; 34 | vector cars(n); 35 | for (size_t i = 0; i < n; ++i) 36 | cin >> cars[i]; 37 | 38 | size_t longestTrain = 0; 39 | vector::iterator iter(cars.begin()); 40 | for (; iter != cars.end(); ++iter) 41 | { 42 | // Pass in the vector(cars.end() .. iter). 43 | size_t lis = LIDS(vector(reverse_iterator::iterator>(cars.end()), 44 | reverse_iterator::iterator>(iter)), 45 | less()); 46 | size_t lds = LIDS(vector(reverse_iterator::iterator>(cars.end()), 47 | reverse_iterator::iterator>(iter)), 48 | greater()); 49 | longestTrain = max(longestTrain, lis + lds - 1); 50 | } 51 | cout << longestTrain << endl; 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /UVa 11463 - Commandos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int INF = 1000; 7 | 8 | int main() 9 | { 10 | int Case = 1, T; 11 | cin >> T; 12 | while ( T-- ) 13 | { 14 | int N, R; 15 | cin >> N >> R; 16 | 17 | vector > buildings(N, vector(N, INF)); 18 | for (int i = 0; i < N; ++i) 19 | buildings[i][i] = 0; 20 | for (int i = 0; i < R; ++i) 21 | { 22 | int u, v; 23 | cin >> u >> v; 24 | buildings[u][v] = 1; 25 | buildings[v][u] = 1; 26 | } 27 | 28 | // Floyd-Warshall algorithm. 29 | for (int k = 0; k < N; ++k) 30 | for (int i = 0; i < N; ++i) 31 | for (int j = 0; j < N; ++j) 32 | buildings[i][j] = min(buildings[i][j], buildings[i][k] + buildings[k][j]); 33 | 34 | int s, d; 35 | cin >> s >> d; 36 | 37 | int minTime = 0; 38 | for (int i = 0; i < N; ++i) 39 | if (buildings[s][i] != INF 40 | && buildings[i][d] != INF) 41 | { 42 | minTime = max(minTime, buildings[s][i] + buildings[i][d]); 43 | } 44 | cout << "Case " << Case++ << ": " << minTime << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa 11470 - Square Sums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n, Case = 1; 8 | while (cin >> n, n != 0) 9 | { 10 | cout << "Case " << Case++ << ":"; 11 | 12 | vector > grid(n, vector(n)); 13 | for (int r = 0; r < n; ++r) 14 | for (int c = 0; c < n; ++c) 15 | cin >> grid[r][c]; 16 | 17 | for (int i = 0; i < n / 2; ++i) 18 | { 19 | int sum = 0; 20 | for (int j = i; j < n - 1 - i; ++j) 21 | { 22 | sum += grid[i][j] 23 | + grid[j][n - 1 - i] 24 | + grid[n - 1 - i][n - 1 - j] 25 | + grid[n - 1 - j][i]; 26 | } 27 | cout << " " << sum; 28 | } 29 | if (n % 2 == 1) 30 | cout << " " << grid[n / 2][n / 2]; 31 | cout << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /UVa 11472 - Beautiful Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int MOD = 1000000007; 6 | 7 | int N, M; 8 | int dp[100 + 5][(1 << 10) + 5][10 + 5]; 9 | 10 | int search(int idx, int bitMask, int lastDigit) 11 | { 12 | if (idx > M) 13 | return 0; 14 | 15 | if (dp[idx][bitMask][lastDigit] != -1) 16 | return dp[idx][bitMask][lastDigit]; 17 | 18 | int nBeautifulNumbers = 0; 19 | 20 | // If all of the digits from 0 to N-1 are used. 21 | if (bitMask == ((1 << N) - 1)) 22 | ++nBeautifulNumbers; 23 | 24 | for (int nextDigit = lastDigit - 1; nextDigit <= lastDigit + 1; nextDigit += 2) 25 | { 26 | if (nextDigit < 0 || nextDigit >= N) 27 | continue; 28 | 29 | nBeautifulNumbers += search(idx + 1, bitMask | (1 << nextDigit), nextDigit); 30 | nBeautifulNumbers %= MOD; 31 | } 32 | 33 | return dp[idx][bitMask][lastDigit] = nBeautifulNumbers; 34 | } 35 | 36 | int solve() 37 | { 38 | memset(dp, -1, sizeof(dp)); 39 | int nBeautifulNumbers = 0; 40 | for (int firstDigit = 1; firstDigit <= N - 1; ++firstDigit) 41 | { 42 | nBeautifulNumbers += search(1, 1 << firstDigit, firstDigit); 43 | nBeautifulNumbers %= MOD; 44 | } 45 | return nBeautifulNumbers; 46 | } 47 | 48 | int main() 49 | { 50 | int T; 51 | cin >> T; 52 | while ( T-- ) 53 | { 54 | cin >> N >> M; 55 | cout << solve() << endl; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /UVa 11498 - Division of Nlogonia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | size_t K; 7 | while (cin >> K, K > 0) 8 | { 9 | int N, M; 10 | cin >> N >> M; 11 | for (size_t i = 0; i < K; ++i) 12 | { 13 | int n, m; 14 | cin >> n >> m; 15 | if (n == N || m == M) 16 | cout << "divisa"; 17 | else if (m > M) 18 | { 19 | if (n > N) 20 | cout << "NE"; 21 | else 22 | cout << "NO"; 23 | } 24 | else if (n > N) 25 | cout << "SE"; 26 | else 27 | cout << "SO"; 28 | cout << endl; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa 11504 - Dominos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void DFS(const vector > &dominos, 7 | vector &visited, 8 | vector &topoSort, 9 | int u) 10 | { 11 | visited[u] = true; 12 | for (int i = 0; i < dominos[u].size(); ++i) 13 | { 14 | int v = dominos[u][i]; 15 | if (! visited[v]) 16 | DFS(dominos, visited, topoSort, v); 17 | } 18 | topoSort.push_back(u); 19 | } 20 | 21 | int main() 22 | { 23 | int T; 24 | cin >> T; 25 | while ( T-- ) 26 | { 27 | int n, m; 28 | cin >> n >> m; 29 | 30 | vector > dominos(n + 1); 31 | vector visited(n + 1, false); 32 | vector topoSort; 33 | 34 | for (int i = 0; i < m; ++i) 35 | { 36 | int u, v; 37 | cin >> u >> v; 38 | dominos[u].push_back(v); 39 | } 40 | for (int u = 1; u <= n; ++u) 41 | if (!visited[u]) 42 | DFS(dominos, visited, topoSort, u); 43 | 44 | reverse(topoSort.begin(), topoSort.end()); 45 | 46 | // Since it may not be a DAG, knock down the 47 | // dominos one by one and count the number. 48 | visited.assign(n + 1, false); 49 | int knock = 0; 50 | for (int i = 0; i < n; ++i) 51 | { 52 | int u = topoSort[i]; 53 | if (! visited[u]) 54 | { 55 | DFS(dominos, visited, topoSort, u); 56 | ++knock; 57 | } 58 | } 59 | cout << knock << endl; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /UVa 11517 - Exact Change.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int MAX_PRICE = 10000; 7 | 8 | int main() 9 | { 10 | int T; 11 | cin >> T; 12 | while ( T-- ) 13 | { 14 | int p, n; 15 | cin >> p >> n; 16 | vector coins(n); 17 | for (int i = 0; i < n; ++i) 18 | cin >> coins[i]; 19 | 20 | // dp[i] stores the total number of coins and bills used 21 | // for paying i cents. 22 | vector dp(MAX_PRICE + 1, 0); 23 | for (int i = 0; i < n; ++i) 24 | { 25 | for (int j = MAX_PRICE; j >= 0; --j) 26 | { 27 | if (dp[j] 28 | && j + coins[i] <= MAX_PRICE) 29 | { 30 | // Minimize the number of coins and bills. 31 | dp[j + coins[i]] = (dp[j + coins[i]] == 0 ? 32 | dp[j] + 1 : 33 | min(dp[j + coins[i]], dp[j] + 1)); 34 | } 35 | } 36 | if (dp[coins[i]] == 0) 37 | dp[coins[i]] = 1; 38 | } 39 | vector::iterator iter(find_if(dp.begin() + p, 40 | dp.end(), 41 | bind2nd(greater(), 0))); 42 | cout << iter - dp.begin() << " " << *iter << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /UVa 11559 - Event Planning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int N, B, H, W; 8 | while (cin >> N >> B >> H >> W) 9 | { 10 | int minCost = B + 1; 11 | for (int h = 0; h < H; ++h) 12 | { 13 | int p; 14 | cin >> p; 15 | for (int w = 0; w < W; ++w) 16 | { 17 | int a; 18 | cin >> a; 19 | if (a >= N && p * N <= minCost) 20 | minCost = p * N; 21 | } 22 | } 23 | if (minCost <= B) 24 | cout << minCost << endl; 25 | else 26 | cout << "stay home" << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /UVa 11586 - Train Tracks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s; 10 | getline(cin, s); 11 | istringstream ss(s); 12 | size_t T; 13 | ss >> T; 14 | while ( T-- ) 15 | { 16 | getline(cin, s); 17 | ss.clear(); 18 | ss.str(s); 19 | map pieces; 20 | while (ss >> s) 21 | { 22 | for (int i = 0; i <= 1; ++i) 23 | ++pieces[s[i]]; 24 | } 25 | // If the number of male connectors equals that of female, 26 | // then the pieces can form a simple loop. 27 | if (pieces['M'] > 1 && pieces['M'] == pieces['F']) 28 | cout << "LOOP" << endl; 29 | else 30 | cout << "NO LOOP" << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa 11608 - No Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int T = 1; 8 | int S; 9 | while (cin >> S, S >= 0) 10 | { 11 | vector problems(13); 12 | for (int m = 1; m <= 12; ++m) 13 | cin >> problems[m]; 14 | vector reqProblems(13); 15 | for (int m = 1; m <= 12; ++m) 16 | cin >> reqProblems[m]; 17 | 18 | cout << "Case " << T++ << ":" << endl; 19 | for (int m = 1; m <= 12; ++m) 20 | { 21 | if (reqProblems[m] > S) 22 | cout << "No problem. :(" << endl; 23 | else 24 | { 25 | cout << "No problem! :D" << endl; 26 | S -= reqProblems[m]; 27 | } 28 | S += problems[m]; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa 11631 - Dark roads.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Street 7 | { 8 | int u, v, length; 9 | }; 10 | 11 | bool comp(const Street &s1, const Street &s2) 12 | { 13 | return s1.length < s2.length; 14 | } 15 | 16 | int getGroup(const vector &groups, int u) 17 | { 18 | return groups[u] == u? 19 | u : getGroup(groups, groups[u]); 20 | } 21 | 22 | int main() 23 | { 24 | int m, n; 25 | while (cin >> m >> n, !(m == 0 && n == 0)) 26 | { 27 | vector streets; 28 | vector groups(m); 29 | int streetCount = 0; 30 | int originalCost = 0, cost = 0; 31 | 32 | for (int i = 0; i < n; ++i) 33 | { 34 | Street st; 35 | cin >> st.u >> st.v >> st.length; 36 | streets.push_back(st); 37 | originalCost += st.length; 38 | } 39 | 40 | sort(streets.begin(), streets.end(), comp); 41 | for (int i = 0; i < m; ++i) 42 | groups[i] = i; 43 | 44 | // Kruskal's algorithm. 45 | for (size_t i = 0; i < streets.size() && streetCount < m - 1; ++i) 46 | { 47 | Street &st = streets[i]; 48 | int groupU = getGroup(groups, st.u); 49 | int groupV = getGroup(groups, st.v); 50 | if (groupU != groupV) 51 | { 52 | cost += st.length; 53 | groups[groupV] = groupU; 54 | ++streetCount; 55 | } 56 | } 57 | cout << originalCost - cost << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /UVa 11661 - Burger Time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int L; 9 | while (cin >> L, L != 0) 10 | { 11 | string S; 12 | cin >> S; 13 | 14 | int minDistance = L; 15 | int lastR = -L, lastD = -L; 16 | // Linearly scan the highway. 17 | for (int i = 0; i < L; ++i) 18 | { 19 | // Restaurant and drugstore. 20 | if (S[i] == 'Z') 21 | { 22 | minDistance = 0; 23 | break; 24 | } 25 | // Restaurant. 26 | else if (S[i] == 'R') 27 | { 28 | minDistance = min(minDistance, i - lastD); 29 | lastR = i; 30 | } 31 | // Drugstore. 32 | else if (S[i] == 'D') 33 | { 34 | minDistance = min(minDistance, i - lastR); 35 | lastD = i; 36 | } 37 | } 38 | cout << minDistance << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa 11687 - Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | size_t recurrence(int i, string number) 7 | { 8 | size_t nDigits = number.size(); 9 | 10 | ostringstream ss; 11 | ss << nDigits; 12 | 13 | if (ss.str() == number) 14 | return i; 15 | return recurrence(i + 1, ss.str()); 16 | } 17 | 18 | int main() 19 | { 20 | string number; 21 | while (cin >> number, number != "END") 22 | { 23 | cout << recurrence(1, number) << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa 11703 - sqrt log sin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // i is "no less than zero and no greater than one million". 6 | int x[1000001]; 7 | 8 | int main() 9 | { 10 | // Typical dynamic programming would solve this problem. 11 | x[0] = 1; 12 | for (int i = 1; i <= 1000000; ++i) 13 | { 14 | x[i] = x[(int)floor(i - sqrt((double)i))] 15 | + x[(int)floor(log((double)i))] 16 | + x[(int)floor(i * sin((double)i) * sin((double)i))]; 17 | x[i] %= 1000000; 18 | } 19 | 20 | int i; 21 | while (cin >> i, i > -1) 22 | { 23 | cout << x[i] << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa 11850 - Alaska.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | size_t n; 10 | while (cin >> n, n > 0) 11 | { 12 | // Delta Juntion has no charing station, so make Brenda have 100 more 13 | // miles to drive for the last distance. 14 | vector stations(1, 1422 + 100); 15 | for (size_t i = 0; i < n; ++i) 16 | { 17 | int dist; 18 | cin >> dist; 19 | stations.push_back(dist); 20 | } 21 | // Using sort() and adjacent_difference() would solve this problem easily. 22 | sort(stations.begin(), stations.end()); 23 | vector diffs(stations.size()); 24 | adjacent_difference(stations.begin(), stations.end(), diffs.begin()); 25 | 26 | if (*max_element(diffs.begin(), diffs.end()) > 200) 27 | cout << "IMPOSSIBLE" << endl; 28 | else 29 | cout << "POSSIBLE" << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa 11876 - N + NOD (N).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int MAX_VALUE = 1000000; 9 | int NOD[MAX_VALUE + 1]; 10 | 11 | int main() 12 | { 13 | // Scan and compute NOD[] by all factors <= MAX_VALUE. 14 | memset(NOD, 0, sizeof NOD); 15 | for (int factor = 1; factor <= MAX_VALUE; ++factor) 16 | for (int i = factor; i <= MAX_VALUE; i += factor) 17 | ++NOD[i]; 18 | 19 | // Compute seq[] by the equations. 20 | vector seq(MAX_VALUE + 1, 0); 21 | seq[0] = 1; 22 | for (int i = 1; i <= MAX_VALUE; ++i) 23 | seq[i] = seq[i - 1] + NOD[seq[i - 1]]; 24 | 25 | int Case = 1; 26 | int T; 27 | cin >> T; 28 | while ( T-- ) 29 | { 30 | int A, B; 31 | cin >> A >> B; 32 | cout << "Case " << Case++ << ": "; 33 | cout << distance(lower_bound(seq.begin(), seq.end(), A), 34 | upper_bound(seq.begin(), seq.end(), B)) 35 | << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 11881 - Internal Rate of Return.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | static const double EPS = 1e-9; 9 | 10 | double NPV(const vector CF, int T, double IRR) 11 | { 12 | double npv = CF[0]; 13 | for (int i = 1; i <= T; ++i) 14 | npv += CF[i] / pow(1.0 + IRR, i); 15 | 16 | return npv; 17 | } 18 | 19 | int main() 20 | { 21 | int T; 22 | while (cin >> T, T != 0) 23 | { 24 | vector CF(T + 1); 25 | for (int i = 0; i <= T; ++i) 26 | cin >> CF[i]; 27 | 28 | // Bisection method. 29 | double L = -0.99, U = numeric_limits::max(), IRR = -1.0; 30 | while (L <= U + EPS) 31 | { 32 | double M = (L + U) / 2.0; 33 | double npv = NPV(CF, T, M); 34 | if (abs(npv) <= EPS) 35 | { 36 | IRR = M; 37 | break; 38 | } 39 | if (npv > EPS) 40 | L = M; 41 | else 42 | U = M; 43 | } 44 | if (abs(IRR + 1.0) <= EPS) 45 | cout << "No" << endl; 46 | else 47 | cout << setprecision (2) 48 | << fixed 49 | << IRR 50 | << endl; 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /UVa 119 - Greedy Gift Givers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | size_t T = 1; 10 | size_t N; 11 | while (cin >> N) 12 | { 13 | map balance; 14 | vector names; 15 | for (size_t i = 0; i < N; ++i) 16 | { 17 | string name; 18 | cin >> name; 19 | balance[name] = 0; 20 | names.push_back(name); 21 | } 22 | for (size_t i = 0; i < N; ++i) 23 | { 24 | string name; 25 | cin >> name; 26 | int spentMoney, nPeople; 27 | cin >> spentMoney >> nPeople; 28 | if (nPeople == 0) 29 | continue; 30 | // "Each person gives the same integer amount of money to 31 | // each friend to whom any money is given, and gives as 32 | // much as possible." 33 | int gift = spentMoney / nPeople; 34 | balance[name] -= gift * nPeople; 35 | for (size_t j = 0; j < nPeople; ++j) 36 | { 37 | cin >> name; 38 | balance[name] += gift; 39 | } 40 | } 41 | // "The output for each group should be separated from other 42 | // groups by a blank line." 43 | if (T++ > 1) 44 | cout << endl; 45 | for (size_t i = 0; i < N; ++i) 46 | cout << names[i] 47 | << " " 48 | << balance[names[i]] 49 | << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /UVa 11900 - Boiled Eggs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int T, Case = 1; 10 | cin >> T; 11 | while ( T-- ) 12 | { 13 | int n, P, Q; 14 | cin >> n >> P >> Q; 15 | vector weight(n + 1); 16 | for (int i = 1; i <= n; ++i) 17 | cin >> weight[i]; 18 | 19 | sort(weight.begin(), weight.end()); 20 | 21 | int eggs = min(P, n); 22 | for (int i = 1; i <= min(P, n); ++i) 23 | if (accumulate(weight.begin() + 1, weight.begin() + 1 + i, 0) > Q) 24 | { 25 | eggs = i - 1; 26 | break; 27 | } 28 | 29 | cout << "Case " 30 | << Case++ 31 | << ": " 32 | << eggs 33 | << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa 11926 - Multitasking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bitset<1000001> schedule; 8 | 9 | bool hasConflict(int n, int m, vector &s, vector &e, vector &r) 10 | { 11 | // One-time tasks. 12 | for (int i = 0; i < n; ++i) 13 | { 14 | int start = s[i], end = e[i]; 15 | // "Touching" is OK, so check from start + 1. 16 | for (int t = start + 1; t <= end; ++t) 17 | { 18 | if (schedule.test(t)) 19 | return true; 20 | else 21 | schedule.set(t); 22 | } 23 | } 24 | // Repeating tasks. 25 | for (int i = n; i < n + m; ++i) 26 | { 27 | int start = s[i], end = e[i], interval = r[i]; 28 | while (start < 1000000) 29 | { 30 | for (int t = start + 1; t <= end; ++t) 31 | { 32 | if (schedule.test(t)) 33 | return true; 34 | else 35 | schedule.set(t); 36 | } 37 | start += interval; 38 | end = min(end + interval, 1000000); 39 | } 40 | } 41 | return false; 42 | } 43 | 44 | int main() 45 | { 46 | int n, m; 47 | while (cin >> n >> m, !(n == 0 && m == 0)) 48 | { 49 | vector s(n + m), e(n + m), r(n + m); 50 | for (int i = 0; i < n; ++i) 51 | cin >> s[i] >> e[i]; 52 | for (int i = n; i < n + m; ++i) 53 | cin >> s[i] >> e[i] >> r[i]; 54 | 55 | schedule.reset(); 56 | if (hasConflict(n, m, s, e, r)) 57 | cout << "CONFLICT" << endl; 58 | else 59 | cout << "NO CONFLICT" << endl; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /UVa 11942 - Lumberjack Sequencing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | // Get N. 11 | string s; 12 | getline(cin, s); 13 | istringstream ss(s); 14 | size_t N; 15 | ss >> N; 16 | cout << "Lumberjacks:" << endl; 17 | while ( N-- ) 18 | { 19 | // Get workers. 20 | getline(cin, s); 21 | ss.clear(); 22 | ss.str(s); 23 | int worker; 24 | vector workers; 25 | while (ss >> worker) 26 | workers.push_back(worker); 27 | // If workers is sorted either increasing or decreasing order. 28 | if (adjacent_find(workers.begin(), workers.end(), 29 | greater()) == workers.end() 30 | || adjacent_find(workers.begin(), workers.end(), 31 | less()) == workers.end() 32 | ) 33 | { 34 | cout << "Ordered" << endl; 35 | } 36 | else 37 | cout << "Unordered" << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa 11988 - Broken Keyboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string line; 10 | while (getline(cin, line)) 11 | { 12 | list beiju; 13 | list::iterator iter(beiju.begin()); 14 | for (size_t i = 0; i < line.size(); ++i) 15 | { 16 | // "Home" key. 17 | if (line[i] == '[') 18 | iter = beiju.begin(); 19 | // "End" key. 20 | else if (line[i] == ']') 21 | iter = beiju.end(); 22 | 23 | /** Using a vector works but will get TLE. 24 | 25 | iter = beiju.insert(iter, line[i]); 26 | ++iter; 27 | */ 28 | else 29 | beiju.insert(iter, line[i]); 30 | } 31 | copy(beiju.begin(), beiju.end(), ostream_iterator(cout, "")); 32 | cout << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa 12015 - Google is Feeling Lucky.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool comp(const pair &p1, const pair &p2) 8 | { 9 | return p1.second < p2.second; 10 | } 11 | 12 | int main() 13 | { 14 | size_t Case = 1; 15 | size_t T; 16 | cin >> T; 17 | while ( T-- ) 18 | { 19 | vector > pages(10); 20 | for (size_t i = 0; i < 10; ++i) 21 | cin >> pages[i].first >> pages[i].second; 22 | 23 | sort(pages.begin(), pages.end(), comp); 24 | // Locate the first page of the highest relevance. 25 | vector >::iterator iter = 26 | lower_bound(pages.begin(), pages.end(), pages.back(), comp); 27 | cout << "Case #" 28 | << Case++ 29 | << ":" 30 | << endl; 31 | for (; iter != pages.end(); ++iter) 32 | cout << iter->first << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa 12250 - Language Detection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | map memo; 9 | memo["HELLO"] = "ENGLISH"; 10 | memo["HOLA"] = "SPANISH"; 11 | memo["HALLO"] = "GERMAN"; 12 | memo["BONJOUR"] = "FRENCH"; 13 | memo["CIAO"] = "ITALIAN"; 14 | memo["ZDRAVSTVUJTE"] = "RUSSIAN"; 15 | 16 | size_t T = 1; 17 | string s; 18 | while (cin >> s, s != "#") 19 | { 20 | cout << "Case " 21 | << T++ 22 | << ": " 23 | << (memo.find(s) != memo.end()? memo[s] : "UNKNOWN") 24 | << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /UVa 12289 - One-Two-Three.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string words[3] = {"one", "two", "three"}; 7 | 8 | size_t wordsDiff(string s1, string s2) 9 | { 10 | size_t diff = max(s1.size(), s2.size()) 11 | - min(s1.size(), s2.size()); 12 | 13 | for (size_t i = 0; i < min(s1.size(), s2.size()); ++i) 14 | if (s1[i] != s2[i]) 15 | ++diff; 16 | 17 | return diff; 18 | } 19 | 20 | int main() 21 | { 22 | size_t T; 23 | cin >> T; 24 | while ( T-- ) 25 | { 26 | string word; 27 | cin >> word; 28 | for (size_t i = 0; i < 3; ++i) 29 | { 30 | if (wordsDiff(word, words[i]) <= 1) 31 | { 32 | cout << i + 1 << endl; 33 | break; 34 | } 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 12372 - Packing for Holiday.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | size_t T; 7 | size_t Case = 1; 8 | cin >> T; 9 | while ( T-- ) 10 | { 11 | int L, W, H; 12 | cin >> L >> W >> H; 13 | cout << "Case " 14 | << Case++ 15 | << ": " 16 | << (max(L, max(W, H)) <= 20? "good" : "bad") 17 | << endl; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /UVa 1238 - Free Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | const int SHIFT = 100; 10 | 11 | vector N; 12 | vector S; 13 | set V; 14 | bool dp[30 + 5][30 + 5][6000 + 5]; 15 | 16 | void search(int idx, int nLeftParen, int value) 17 | { 18 | if (idx == N.size()) 19 | { 20 | V.insert(value); 21 | return; 22 | } 23 | else if (dp[idx][nLeftParen][value + idx * SHIFT]) 24 | return; 25 | 26 | // Put a left parenthesis. 27 | // It's necessary to use S[] for the case -0. 28 | if (S[idx] == '-') 29 | search(idx + 1, 30 | nLeftParen + 1, 31 | value + (nLeftParen % 2 == 0? N[idx] : -N[idx])); 32 | 33 | // Put 0, 1 .. up to nParen right parentheses. 34 | for (int i = 0; i <= nLeftParen; ++i) 35 | search(idx + 1, 36 | nLeftParen - i, 37 | value + (nLeftParen % 2 == 0? N[idx] : -N[idx])); 38 | 39 | dp[idx][nLeftParen][value + idx * SHIFT] = true; 40 | } 41 | 42 | int solve() 43 | { 44 | memset(dp, false, sizeof(dp)); 45 | V.clear(); 46 | search(0, 0, 0); 47 | return (int)V.size(); 48 | } 49 | 50 | int main() 51 | { 52 | string s; 53 | while (getline(cin, s)) 54 | { 55 | stringstream ss(s); 56 | char sign; 57 | int num; 58 | 59 | ss >> num; 60 | N.clear(); 61 | S.clear(); 62 | N.push_back(num); 63 | S.push_back('+'); 64 | while (ss >> sign >> num) 65 | { 66 | N.push_back(sign == '+'? num : -num); 67 | S.push_back(sign); 68 | } 69 | cout << solve() << endl; 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /UVa 124 - Following Orders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | return 0; 12 | } -------------------------------------------------------------------------------- /UVa 12405 - Scarecrow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int T, Case = 1; 8 | cin >> T; 9 | while ( T-- ) 10 | { 11 | int N; 12 | cin >> N; 13 | vector field(N + 2, '#'); 14 | for (int i = 0; i < N; ++i) 15 | cin >> field[i]; 16 | 17 | int scarecrows = 0; 18 | for (int i = 0; i < N; ++i) 19 | { 20 | // Greedily make a scarecrow cover 3 cells. 21 | if (field[i] == '.') 22 | { 23 | field[i] = field[i + 1] = field[i + 2] = '#'; 24 | ++scarecrows; 25 | } 26 | } 27 | cout << "Case " 28 | << Case++ 29 | << ": " 30 | << scarecrows 31 | << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /UVa 125 - Numbering Paths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int MAX_N = 35; 7 | 8 | int main() 9 | { 10 | int Case = 0, n; 11 | while (cin >> n) 12 | { 13 | vector > streets(MAX_N, vector(MAX_N, 0)); 14 | int N = 0; 15 | 16 | for (int i = 1; i <= n; ++i) 17 | { 18 | int u, v; 19 | cin >> u >> v; 20 | streets[u][v] = 1; 21 | N = max(max(u, v), N); 22 | } 23 | 24 | for (int k = 0; k <= N; ++k) 25 | for (int i = 0; i <= N; ++i) 26 | for (int j = 0; j <= N; ++j) 27 | if (streets[i][k] && streets[k][j]) 28 | streets[i][j] += streets[i][k] * streets[k][j]; 29 | 30 | for (int k = 0; k <= N; ++k) 31 | // If k -> k is a cycle, then i -> k -> j must have infinite paths. 32 | if (streets[k][k]) 33 | for (int i = 0; i <= N; ++i) 34 | for (int j = 0; j <= N; ++j) 35 | if (streets[i][k] && streets[k][j]) 36 | streets[i][j] = -1; 37 | 38 | cout << "matrix for city " << Case++ << endl; 39 | for (int i = 0; i <= N; ++i) 40 | { 41 | for (int j = 0; j <= N; ++j) 42 | { 43 | cout << (j > 0? " " : "") 44 | << streets[i][j]; 45 | } 46 | cout << endl; 47 | } 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa 12503 - Robot Instructions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t T; 9 | cin >> T; 10 | while ( T-- ) 11 | { 12 | size_t n; 13 | cin >> n; 14 | vector instructions; 15 | int position = 0; 16 | while ( n-- ) 17 | { 18 | string instruction; 19 | cin >> instruction; 20 | if (instruction == "LEFT") 21 | { 22 | --position; 23 | instructions.push_back(-1); 24 | } 25 | else if (instruction == "RIGHT") 26 | { 27 | ++position; 28 | instructions.push_back(1); 29 | } 30 | // "SAME AS .." 31 | else 32 | { 33 | int i; 34 | cin >> instruction >> i; 35 | position += instructions[i - 1]; 36 | // Store the action directly. 37 | instructions.push_back(instructions[i - 1]); 38 | } 39 | } 40 | cout << position << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa 1252 - Twenty Questions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int M, N; 8 | int F[128]; 9 | int dp[1 << 11][1 << 11]; 10 | 11 | // Q: bitmask of questions. 12 | // A: bitmask of answers. 13 | int search(int Q, int A) 14 | { 15 | if (dp[Q][A] != -1) 16 | return dp[Q][A]; 17 | 18 | // If the number of remained objects are less than 2, then there is no 19 | // need to ask any other questions. 20 | int nObjects = 0; 21 | for (int i = 0; i < N; ++i) 22 | if ((F[i] & Q) == A) 23 | ++nObjects; 24 | 25 | if (nObjects <= 1) 26 | return dp[Q][A] = 0; 27 | 28 | // Try to ask each question and check its maximum number of required 29 | // questions to identify the remained objects. Pick the minimum of them. 30 | int nQuestions = M + 1; 31 | for (int i = 0; i < M; ++i) 32 | if ((Q & (1 << i)) == 0) 33 | nQuestions = min(nQuestions, 1 + max(search(Q | (1 << i), A), 34 | search(Q | (1 << i), A | (1 << i)))); 35 | 36 | return dp[Q][A] = nQuestions; 37 | } 38 | 39 | int solve() 40 | { 41 | memset(dp, -1, sizeof(dp)); 42 | return search(0, 0); 43 | } 44 | 45 | int main() 46 | { 47 | while (cin >> M >> N, !(M == 0 && N == 0)) 48 | { 49 | for (int i = 0; i < N; ++i) 50 | { 51 | string s; 52 | cin >> s; 53 | 54 | int feature = 0; 55 | for (int j = 0; j < M; ++j) 56 | feature |= (s[j] - '0') << j; 57 | 58 | F[i] = feature; 59 | } 60 | cout << solve() << endl; 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /UVa 128 - Software CRC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int g = 34943; 7 | 8 | int main() 9 | { 10 | string m; 11 | while (getline(cin, m), m != "#") 12 | { 13 | // Horner's method. 14 | int n = 0; 15 | for (size_t i = 0; i < m.length(); ++i) 16 | n = (m[i] + (n << 8)) % g; 17 | 18 | // (n << 16) might be larger than 2^31 - 1. 19 | n = ((long long)n << 16) % g; 20 | int CRC = (g - n) % g; 21 | cout << hex << uppercase << setw(2) << setfill('0') 22 | << (CRC >> 8) 23 | << " " 24 | << hex << uppercase << setw(2) << setfill('0') 25 | << (CRC & 255) 26 | << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /UVa 156 - Ananagrams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | string s; 12 | multimap memo; 13 | while (cin >> s, s != "#") 14 | { 15 | string t(s); 16 | transform(s.begin(), s.end(), t.begin(), ::tolower); 17 | sort(t.begin(), t.end()); 18 | memo.insert(pair(t, s)); 19 | } 20 | // An ananagram has only 1 mapping. 21 | multimap::iterator iter(memo.begin()); 22 | vector ananagrams; 23 | for (; iter != memo.end(); ++iter) 24 | if (memo.count(iter->first) == 1) 25 | ananagrams.push_back(iter->second); 26 | 27 | sort(ananagrams.begin(), ananagrams.end()); 28 | copy(ananagrams.begin(), ananagrams.end(), ostream_iterator(cout, "\n")); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /UVa 195 - Anagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // 'A' < 'a' < 'b' 7 | bool comp(char c1, char c2) 8 | { 9 | return ::tolower(c1) == ::tolower(c2)? 10 | c1 < c2 : tolower(c1) < tolower(c2); 11 | } 12 | 13 | int main() 14 | { 15 | size_t T; 16 | cin >> T; 17 | while ( T-- ) 18 | { 19 | string s; 20 | cin >> s; 21 | sort(s.begin(), s.end(), comp); 22 | do 23 | { 24 | cout << s << endl; 25 | } while (next_permutation(s.begin(), s.end(), comp)); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /UVa 200 - Rare Order.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | enum flag_t { UNVISITED, VISITED }; 8 | 9 | void dfs(map > &adjList, 10 | map &flags, 11 | vector &order, 12 | char c) 13 | { 14 | flags[c] = VISITED; 15 | for (int i = 0; i < adjList[c].size(); ++i) 16 | { 17 | char v = adjList[c][i]; 18 | if (flags[v] == UNVISITED) 19 | dfs(adjList, flags, order, v); 20 | } 21 | order.push_back(c); 22 | } 23 | 24 | int main() 25 | { 26 | string ps, s; 27 | map > adjList; 28 | map flags; 29 | cin >> ps; 30 | while (cin >> s, s != "#") 31 | { 32 | // pad ps and s with spaces. 33 | ps.append(20 - ps.size(), ' '); 34 | s.append(20 - s.size(), ' '); 35 | for (int i = 0; i < 20; ++i) 36 | if (s[i] != ps[i]) 37 | { 38 | adjList[ps[i]].push_back(s[i]); 39 | flags[ps[i]] = UNVISITED; 40 | flags[s[i]] = UNVISITED; 41 | break; 42 | } 43 | ps = s; 44 | } 45 | 46 | // Topological sort. 47 | vector order; 48 | map >::iterator iter(adjList.begin()); 49 | for (; iter != adjList.end(); ++iter) 50 | if (flags[iter->first] == UNVISITED) 51 | dfs(adjList, flags, order, iter->first); 52 | 53 | for (int i = (int)order.size() - 1; i >= 0; --i) 54 | if (order[i] != ' ') 55 | cout << order[i]; 56 | cout << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /UVa 256 - Quirksome Squares.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | vector quirkNumbers; 10 | // The largest possible quirk number is 99999999. 11 | for (int n = 0; n <= 9999; ++n) 12 | quirkNumbers.push_back(n * n); 13 | 14 | int d; 15 | while (cin >> d) 16 | { 17 | for (int i = 0; i < quirkNumbers.size(); ++i) 18 | { 19 | // Consider all numbers up to d digits. 20 | if (quirkNumbers[i] >= (int)pow(10.0, d)) 21 | break; 22 | 23 | int U = quirkNumbers[i] / (int)pow(10.0, d / 2); 24 | int L = quirkNumbers[i] % (int)pow(10.0, d / 2); 25 | if ((U + L) * (U + L) == quirkNumbers[i]) 26 | cout << setw(d) 27 | << setfill('0') // Use setfill() to fill the leading zeros. 28 | << quirkNumbers[i] 29 | << endl; 30 | } 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa 257 - Palinwords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isPalinword(const string &s) 7 | { 8 | // dp[i][j] = true if s[i..j] is a palindrome; 0, otherwise. 9 | vector > dp(s.size(), vector(s.size(), true)); 10 | 11 | for (size_t i = 0; i < s.size() - 1; ++i) 12 | dp[i][i + 1] = (s[i]== s[i + 1]? true : false); 13 | 14 | // Store the found palindrome s[l..r]. 15 | string candidate; 16 | size_t l = 0, r = s.size() - 1; 17 | for (size_t len = 2; len < s.size(); ++len) 18 | for (size_t i = 0; i + len < s.size(); ++i) 19 | { 20 | size_t j = i + len; 21 | // If s[i..j] is a palindrome. 22 | if (s[i] == s[j] && dp[i + 1][j - 1]) 23 | { 24 | dp[i][j] = true; 25 | if (candidate == "") 26 | { 27 | candidate = s.substr(i, len + 1); 28 | r = i; 29 | l = j; 30 | } 31 | // "Neither of these 2 palindromes may be embedded in the other palindrome" 32 | // Since it loops from smaller lengths, this string must be no shorter than candidate. 33 | else if (i > r || j < l) 34 | if (s.substr(i, len + 1).find(candidate) == string::npos) 35 | return true; 36 | } 37 | else 38 | dp[i][j] = false; 39 | } 40 | return false; 41 | } 42 | 43 | int main() 44 | { 45 | string s; 46 | while (cin >> s) 47 | if (isPalinword(s)) 48 | cout << s << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /UVa 311 - Packets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int b1, b2, b3, b4, b5, b6; 8 | while (cin >> b1 >> b2 >> b3 >> b4 >> b5 >> b6, 9 | b1 + b2 + b3 + b4 + b5 + b6 > 0) 10 | { 11 | int parcels = 0; 12 | // b6 boxes. 13 | parcels += b6; 14 | // b5 boxes each with 11 1x1. 15 | parcels += b5; 16 | b1 -= b5 * 11; 17 | // b4 boxes each with 5 2x2. 18 | parcels += b4; 19 | b2 -= b4 * 5; 20 | // (b3 + 3) / 4 boxes, then check the last parcel. 21 | parcels += (b3 + 3) / 4; 22 | if (b3 % 4 == 1) 23 | { 24 | b2 -= 5; 25 | b1 -= 7; 26 | } 27 | else if (b3 % 4 == 2) 28 | { 29 | b2 -= 3; 30 | b1 -= 6; 31 | } 32 | else if (b3 % 4 == 3) 33 | { 34 | b2 -= 1; 35 | b1 -= 5; 36 | } 37 | // Now handle b2. 38 | if (b2 > 0) 39 | { 40 | parcels += (b2 + 8) / 9; 41 | b2 -= 9 * ((b2 + 8) / 9); 42 | } 43 | // Lastly, handle b1. 44 | if (b2 < 0) 45 | b1 += b2 * 4; 46 | if (b1 > 0) 47 | parcels += (b1 + 35) / 36; 48 | 49 | cout << parcels << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /UVa 324 - Factorial Frequencies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int MAX_DIGITS = 781; 8 | int digits[2][MAX_DIGITS]; 9 | 10 | int main() 11 | { 12 | int n; 13 | while (cin >> n, n != 0) 14 | { 15 | int resultIndex = 0; 16 | int resultDigits = 1; 17 | int lastResultIndex = 1; 18 | 19 | digits[resultIndex][0] = 1; 20 | 21 | for (int m = 2; m <= n; ++m) 22 | { 23 | swap(resultIndex, lastResultIndex); 24 | memset(digits[resultIndex], 0, sizeof digits[resultIndex]); 25 | 26 | /** Prepare the multiplier[]. 27 | For example, m = 86 28 | multiplier[0] = 6 29 | multiplier[1] = 8 30 | multiplierIndex = 2 31 | */ 32 | int multiplier[4]; 33 | int tmp = m; 34 | int multiplierIndex = 0; 35 | while (tmp) 36 | { 37 | multiplier[multiplierIndex++] = tmp % 10; 38 | tmp /= 10; 39 | } 40 | 41 | // Multiply digits[lastResultIndex][0..resultDigits] by the multiplier. 42 | int i = 0, j = 0; 43 | for ( ; i < multiplierIndex; ++i) 44 | { 45 | j = 0; 46 | int carry = 0; 47 | for ( ; j < resultDigits || carry > 0; ++j) 48 | { 49 | int product = digits[resultIndex][i + j] + multiplier[i] * digits[lastResultIndex][j] + carry; 50 | carry = product / 10; 51 | digits[resultIndex][i + j] = product % 10; 52 | } 53 | } 54 | resultDigits = i + j - 1; 55 | } 56 | 57 | // Count the digits. 58 | int countDigits[10] = {0}; 59 | for (int i = 0; i < resultDigits; ++i) 60 | { 61 | ++countDigits[digits[resultIndex][i]]; 62 | } 63 | 64 | cout << n << "! --" << endl; 65 | for (int i = 0; i <= 9; ++i) 66 | { 67 | cout << setw(5) << "(" << i << ")" 68 | << setw(5) << countDigits[i]; 69 | if (i == 4 || i == 9) 70 | cout << endl; 71 | } 72 | } 73 | return 0; 74 | } -------------------------------------------------------------------------------- /UVa 336 - A Node Too Far.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int Case = 1, NC; 10 | while (cin >> NC, NC != 0) 11 | { 12 | map > adjList; 13 | 14 | for (int i = 0; i < NC; ++i) 15 | { 16 | int u, v; 17 | cin >> u >> v; 18 | adjList[u].push_back(v); 19 | adjList[v].push_back(u); 20 | } 21 | int s, ttl; 22 | while (cin >> s >> ttl, !(s == 0 && ttl == 0)) 23 | { 24 | // Use BFS to solve the SSSP problem. 25 | queue q; 26 | map dist; 27 | // The starting node is always reachable. 28 | int reachableNodes = 1; 29 | 30 | dist[s] = 0; 31 | q.push(s); 32 | while (! q.empty()) 33 | { 34 | int u = q.front(); 35 | q.pop(); 36 | for (size_t i = 0; i < adjList[u].size(); ++i) 37 | { 38 | int v = adjList[u][i]; 39 | if (! dist.count(v)) 40 | { 41 | dist[v] = dist[u] + 1; 42 | if (dist[v] <= ttl) 43 | { 44 | q.push(v); 45 | ++reachableNodes; 46 | } 47 | } 48 | } 49 | } 50 | cout << "Case " 51 | << Case++ 52 | << ": " 53 | << adjList.size() - reachableNodes 54 | << " nodes not reachable from node " 55 | << s 56 | << " with TTL = " 57 | << ttl 58 | << "." 59 | << endl; 60 | } 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /UVa 340 - Master-Mind Hints.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int Game = 1; 9 | int N; 10 | while (cin >> N, N > 0) 11 | { 12 | vector code(N); 13 | for (int d = 0; d < N; ++d) 14 | cin >> code[d]; 15 | 16 | cout << "Game " << Game++ << ":" << endl; 17 | while (N == N) 18 | { 19 | vector guess(N); 20 | for (int i = 0; i < N; ++i) 21 | cin >> guess[i]; 22 | 23 | if (count(guess.begin(), guess.end(), 0) == N) 24 | break; 25 | 26 | int i = 0, j = 0; 27 | vector marks(N, false); 28 | // Check strong matches. 29 | for (int d = 0; d < N; ++d) 30 | if (code[d] == guess[d]) 31 | { 32 | ++i; 33 | marks[d] = true; 34 | guess[d] = -1; 35 | } 36 | // Check weak matches. 37 | for (int d = 0; d < N; ++d) 38 | if (! marks[d]) 39 | { 40 | vector::iterator iter = 41 | find(guess.begin(), guess.end(), code[d]); 42 | if (iter != guess.end()) 43 | { 44 | ++j; 45 | marks[d] = true; 46 | *iter = -1; 47 | } 48 | } 49 | cout << " (" 50 | << i 51 | << "," 52 | << j 53 | << ")" 54 | << endl; 55 | } 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /UVa 352 - The Seasonal War.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef vector vi_t; 7 | typedef vector vvi_t; 8 | 9 | int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; 10 | int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; 11 | 12 | void dfs(vvi_t &image, int x, int y) 13 | { 14 | image[x][y] = 0; 15 | // "Cells with adjacent sides on common vertices, which contain binary ones, 16 | // comprise one war eagle." 17 | for (int i = 0; i < 8; ++i) 18 | { 19 | int tx = x + dx[i]; 20 | int ty = y + dy[i]; 21 | if (tx < 0 || tx >= image.size() || ty < 0 || ty >= image.size()) 22 | continue; 23 | if (image[tx][ty] == 1) 24 | dfs(image, tx, ty); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | int Case = 1, n; 31 | while (cin >> n) 32 | { 33 | cin.ignore(); 34 | 35 | string s; 36 | vvi_t image(n, vi_t(n)); 37 | for (int x = 0; x < n; ++x) 38 | { 39 | getline(cin, s); 40 | for (int y = 0; y < n; ++y) 41 | image[x][y] = s[y] - '0'; 42 | } 43 | // Use DFS to find connected components. 44 | int nCC = 0; 45 | for (int x = 0; x < n; ++x) 46 | for (int y = 0; y < n; ++y) 47 | if (image[x][y] == 1) 48 | { 49 | ++nCC; 50 | dfs(image, x, y); 51 | } 52 | 53 | cout << "Image number " 54 | << Case++ 55 | << " contains " 56 | << nCC 57 | << " war eagles." 58 | << endl; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /UVa 353 - Pesky Palindromes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool isPalindrome(const string &s) 8 | { 9 | string r_s(s); 10 | reverse(r_s.begin(), r_s.end()); 11 | return s == r_s; 12 | } 13 | 14 | int main() 15 | { 16 | string s; 17 | while (cin >> s) 18 | { 19 | map memo; 20 | int c = 0; 21 | for (size_t i = 0; i < s.size(); ++i) 22 | for (size_t j = i; j < s.size(); ++j) 23 | { 24 | string subString = s.substr(i, j - i + 1); 25 | if (!memo.count(subString) 26 | && isPalindrome(subString)) 27 | { 28 | ++c; 29 | ++memo[subString]; 30 | } 31 | } 32 | cout << "The string '" 33 | << s 34 | << "' contains " 35 | << c 36 | << " palindromes." 37 | << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa 400 - Unix ls.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool comp(const string &s1, const string &s2) 9 | { 10 | return s1.length() < s2.length(); 11 | } 12 | 13 | int main() 14 | { 15 | size_t N; 16 | while (cin >> N) 17 | { 18 | vector filenames(N); 19 | for (size_t i = 0; i < N; ++i) 20 | cin >> filenames[i]; 21 | 22 | sort(filenames.begin(), filenames.end()); 23 | size_t L = max_element(filenames.begin(), filenames.end(), comp)->length(); 24 | // According to the problem description: (C - 1) * (L + 2) + L <= 60 25 | size_t C = (60 - L) / (L + 2) + 1; 26 | // Round N / C up. 27 | size_t R = (N + C - 1) / C; 28 | 29 | cout << "------------------------------------------------------------" << endl; 30 | for (size_t i = 0; i < R; ++i) 31 | { 32 | // "all other columns will be the width of the longest filename plus 2" and 33 | // filenames are "formatted into left-justified columns." 34 | for (size_t j = i; j < N; j += R) 35 | cout << setw(L + 2) << setiosflags(ios::left) << filenames[j]; 36 | 37 | cout << endl; 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa 401 - Palindromes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static map mirror; 8 | 9 | bool isPalindrome(const string &s) 10 | { 11 | string r_s(s); 12 | reverse(r_s.begin(), r_s.end()); 13 | return s == r_s; 14 | } 15 | 16 | bool isMirrored(const string &s) 17 | { 18 | string::const_iterator iter(s.begin()); 19 | string::const_reverse_iterator r_iter(s.rbegin()); 20 | // Need to check the middle element for odd sizes of strings. 21 | for ( ; iter < s.begin() + (s.size() + 1) / 2; ++iter, ++r_iter) 22 | if (!mirror.count(*iter) || mirror[*iter] != *r_iter) 23 | return false; 24 | 25 | return true; 26 | } 27 | 28 | int main() 29 | { 30 | char from[] = {'A', 'E', 'H', 'I', 'J', 'L', 31 | 'M', 'O', 'S', 'T', 'U', 'V', 32 | 'W', 'X', 'Y', 'Z', '1', '2', 33 | '3', '5', '8'}; 34 | char to[] = {'A', '3', 'H', 'I', 'L', 'J', 35 | 'M', 'O', '2', 'T', 'U', 'V', 36 | 'W', 'X', 'Y', '5', '1', 'S', 37 | 'E', 'Z', '8'}; 38 | 39 | for (size_t i = 0; i < sizeof(from) / sizeof(char); ++i) 40 | mirror[from[i]] = to[i]; 41 | 42 | string s; 43 | while (cin >> s) 44 | { 45 | bool isPalin = isPalindrome(s); 46 | bool isMir = isMirrored(s); 47 | 48 | cout << s << " -- "; 49 | if (!isPalin && !isMir) 50 | cout << "is not a palindrome." << endl; 51 | else if (isPalin && !isMir) 52 | cout << "is a regular palindrome." << endl; 53 | else if (!isPalin && isMir) 54 | cout << "is a mirrored string." << endl; 55 | else 56 | cout << "is a mirrored palindrome." << endl; 57 | 58 | // "after each output line, you must print an empty line." 59 | cout << endl; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /UVa 414 - Machined Surfaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | string line; 11 | while (getline(cin, line), line != "0") 12 | { 13 | size_t N; 14 | // Convert line to N. 15 | istringstream(line) >> N; 16 | vector image; 17 | for (size_t i = 0; i < N; ++i) 18 | { 19 | getline(cin, line); 20 | image.push_back(line); 21 | } 22 | size_t totalB = 0; 23 | size_t minB = 25; 24 | for (size_t i = 0; i < N; ++i) 25 | { 26 | size_t countB = count(image[i].begin(), image[i].end(), ' '); 27 | totalB += countB; 28 | minB = min(minB, countB); 29 | } 30 | cout << totalB - N * minB << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa 417 - Word Index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | map memo; 7 | int code; 8 | 9 | // Enumerate the strings of s + one character in a strict 10 | // lexicographic order. 11 | void enumerateStrings(string s, size_t len) 12 | { 13 | if (s.size() == len) 14 | { 15 | memo[s] = code++; 16 | return; 17 | } 18 | char lastLetter; 19 | if (s.empty()) 20 | lastLetter = 'a'; 21 | else 22 | lastLetter = s[s.size() - 1] + 1; 23 | 24 | for (char c = lastLetter; c <= 'z'; ++c) 25 | enumerateStrings(s + c, len); 26 | } 27 | 28 | int main() 29 | { 30 | code = 1; 31 | for (size_t len = 1; len <= 5; ++len) 32 | enumerateStrings("", len); 33 | 34 | string word; 35 | while (cin >> word) 36 | cout << memo[word] << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /UVa 429 - Word Transformation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int T; 12 | cin >> T; 13 | while ( T-- ) 14 | { 15 | vector words; 16 | 17 | string word; 18 | while (cin >> word, word != "*") 19 | words.push_back(word); 20 | 21 | cin.ignore(); 22 | 23 | string line; 24 | while (getline(cin, line) && line != "") 25 | { 26 | stringstream ss(line); 27 | string s, t; 28 | ss >> s >> t; 29 | // BFS 30 | queue q; 31 | map trans; 32 | 33 | trans[s] = 0; 34 | q.push(s); 35 | while (!q.empty() && trans.count(t) == 0) 36 | { 37 | string u = q.front(); 38 | q.pop(); 39 | // Loop over every word in the dictionary. 40 | for (size_t i = 0; i < words.size(); ++i) 41 | { 42 | const string &v = words[i]; 43 | if (! trans.count(v) && u.length() == v.length()) 44 | { 45 | int diff = 0; 46 | for (size_t j = 0; j < u.length(); ++j) 47 | if (u[j] != v[j]) 48 | ++diff; 49 | if (diff == 1) 50 | { 51 | trans[v] = trans[u] + 1; 52 | q.push(v); 53 | } 54 | } 55 | } 56 | } 57 | cout << s << " " << t << " " << trans[t] << endl; 58 | } 59 | if (T) 60 | cout << endl; 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /UVa 454 - Anagrams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | string baseString(const string &s) 8 | { 9 | string bs(s); 10 | // Remove all empty spaces and sort the string. 11 | bs.erase(remove(bs.begin(), bs.end(), ' '), bs.end()); 12 | sort(bs.begin(), bs.end()); 13 | return bs; 14 | } 15 | 16 | int main() 17 | { 18 | size_t T; 19 | cin >> T; 20 | cin.ignore(); 21 | cin.ignore(); 22 | while ( T-- ) 23 | { 24 | string s; 25 | vector words; 26 | while (getline(cin, s) && s != "") 27 | words.push_back(s); 28 | 29 | sort(words.begin(), words.end()); 30 | // Check, in alphabetic order, every pair of strings. 31 | for (size_t i = 0; i < words.size(); ++i) 32 | for (size_t j = i + 1; j < words.size(); ++j) 33 | if (baseString(words[i]) == baseString(words[j])) 34 | cout << words[i] << " = " << words[j] << endl; 35 | 36 | if (T) 37 | cout << endl; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa 459 - Graph Connectivity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef vector vi_t; 7 | typedef vector vvi_t; 8 | 9 | const int UNVISITED = 0; 10 | const int VISITED = 1; 11 | 12 | void dfs(const vvi_t &G, 13 | vi_t &dfsNum, 14 | int u) 15 | { 16 | dfsNum[u] = VISITED; 17 | for (int j = 0; j < (int)G[u].size(); ++j) 18 | { 19 | int v = G[u][j]; 20 | if (dfsNum[v] == UNVISITED) 21 | dfs(G, dfsNum, v); 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int T; 28 | cin >> T; 29 | while ( T-- ) 30 | { 31 | char c; 32 | int nNodes; 33 | cin >> c; 34 | nNodes = c - 'A' + 1; 35 | cin.ignore(); 36 | 37 | vvi_t G(nNodes); 38 | vi_t dfsNum(nNodes, UNVISITED); 39 | string s; 40 | while (getline(cin, s) && s != "") 41 | { 42 | // G is an undirected graph. 43 | G[s[0] - 'A'].push_back(s[1] - 'A'); 44 | G[s[1] - 'A'].push_back(s[0] - 'A'); 45 | } 46 | // Use DFS to find connected components. 47 | int nCC = 0; 48 | for (int i = 0; i < nNodes; ++i) 49 | { 50 | if (dfsNum[i] == UNVISITED) 51 | { 52 | ++nCC; 53 | dfs(G, dfsNum, i); 54 | } 55 | } 56 | cout << nCC << endl; 57 | if (T) 58 | cout << endl; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /UVa 481 - What Goes Up.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void print(const vector &seq, 7 | const vector &path, 8 | int pos) 9 | { 10 | if (pos == -1) 11 | return; 12 | print(seq, path, path[pos]); 13 | cout << seq[pos] << endl; 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | vector seq; 20 | while (cin >> n) 21 | seq.push_back(n); 22 | 23 | // L[i] stores the smallest ending value of seq's length-i LIS. 24 | vector L(seq.size()); 25 | vector L_id(seq.size()); 26 | vector path(seq.size()); 27 | int lisEnd = 0, lisCount = 0; 28 | // O(nlogk) greedy + D&C algorithm (where k is the length of the LIS). 29 | for (int i = 0; i < seq.size(); ++i) 30 | { 31 | // Find the insert position of L. 32 | size_t pos = lower_bound(L.begin(), L.begin() + lisCount, seq[i]) 33 | - L.begin(); 34 | L[pos] = seq[i]; 35 | L_id[pos] = i; 36 | // Store seq[i]'s previous number. 37 | path[i] = pos? L_id[pos - 1] : -1; 38 | // If seq[i] was inserted to L's "end". 39 | if (pos == lisCount) 40 | { 41 | ++lisCount; 42 | lisEnd = i; 43 | } 44 | } 45 | 46 | cout << lisCount << "\n-\n"; 47 | print(seq, path, lisEnd); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa 484 - The Department of Redundancy Department.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | map memo; 9 | vector sequence; 10 | int integer; 11 | 12 | while (cin >> integer) 13 | { 14 | if (memo[integer] == 0) 15 | sequence.push_back(integer); 16 | 17 | ++memo[integer]; 18 | } 19 | 20 | for (size_t i = 0; i < sequence.size(); ++i) 21 | cout << sequence[i] << " " << memo[sequence[i]] << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa 489 - Hangman Judge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t round; 9 | while (cin >> round, round != -1) 10 | { 11 | string solution, guess; 12 | cin >> solution >> guess; 13 | 14 | int stroke = 0; 15 | for (size_t i = 0; i < guess.size() 16 | && stroke <= 6 17 | && solution != ""; ++i) 18 | { 19 | if (solution.find(guess[i]) != string::npos) 20 | { 21 | // Remove all guess[i] in solution. 22 | solution.erase(remove(solution.begin(), solution.end(), 23 | guess[i]), 24 | solution.end()); 25 | } 26 | else 27 | ++stroke; 28 | } 29 | cout << "Round " << round << endl; 30 | if (stroke == 7) 31 | cout << "You lose." << endl; 32 | else if (solution == "") 33 | cout << "You win." << endl; 34 | else 35 | cout << "You chickened out." << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa 497 - Strategic Defense Initiative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | size_t T; 11 | cin >> T; 12 | cin.ignore(); 13 | cin.ignore(); 14 | while ( T-- ) 15 | { 16 | vector M; 17 | string s; 18 | while (getline(cin, s) && s != "") 19 | { 20 | stringstream ss(s); 21 | int missle; 22 | 23 | ss >> missle; 24 | M.push_back(missle); 25 | } 26 | 27 | // O(n^2) DP. 28 | vector LIS(M.size(), 1); 29 | vector path(M.size(), -1); 30 | LIS[0] = 1; 31 | for (size_t i = 1; i < M.size(); ++i) 32 | for (size_t j = 0; j < i; ++j) 33 | { 34 | int l = M[j] < M[i]? LIS[j] + 1 : 0; 35 | if (l > LIS[i]) 36 | { 37 | LIS[i] = l; 38 | path[i] = static_cast(j); 39 | } 40 | } 41 | // Print the path. 42 | int i = static_cast(distance(LIS.begin(), 43 | max_element(LIS.begin(), LIS.end()) 44 | ) 45 | ); 46 | vector print(1, M[i]); 47 | while (path[i] != -1) 48 | { 49 | i = path[i]; 50 | print.insert(print.begin(), M[i]); 51 | } 52 | cout << "Max hits: " << print.size() << endl; 53 | copy(print.begin(), print.end(), ostream_iterator(cout, "\n")); 54 | if (T) 55 | cout << endl; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /UVa 514 - Rails.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | size_t N; 9 | while (cin >> N, N > 0) 10 | { 11 | vector coaches(N); 12 | for (int i = 0; i < N; ++i) 13 | coaches[i] = i + 1; 14 | 15 | vector target(N); 16 | while (cin >> target[0], target[0] > 0) 17 | { 18 | for (size_t i = 1; i < N; ++i) 19 | cin >> target[i]; 20 | 21 | stack station; 22 | size_t coachesIndex = 0; 23 | size_t targetIndex = 0; 24 | while (targetIndex == targetIndex) // Avoid compiler's warning. 25 | { 26 | // Does the top coach in the station match the target coach? 27 | if (!station.empty() 28 | && station.top() == target[targetIndex]) 29 | { 30 | station.pop(); 31 | ++targetIndex; 32 | continue; 33 | } 34 | if (coachesIndex >= N) 35 | break; 36 | station.push(coaches[coachesIndex++]); 37 | } 38 | if (station.empty()) 39 | cout << "Yes" << endl; 40 | else 41 | cout << "No" << endl; 42 | } 43 | cout << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa 541 - Error Correction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | while (cin >> n, n > 0) 9 | { 10 | vector > matrix(n, vector(n)); 11 | for (int i = 0; i < n; ++i) 12 | for (int j = 0; j < n; ++j) 13 | { 14 | int bit; 15 | cin >> bit; 16 | matrix[i][j] = bit; 17 | } 18 | 19 | vector rowSum(n), columnSum(n); 20 | int oddRowCount = 0, oddColumnCount = 0; 21 | int oddRow, oddColumn; 22 | // Scan every row. 23 | for (int i = 0; i < n; ++i) 24 | { 25 | int sum = 0; 26 | for (int j = 0; j < n; ++j) 27 | { 28 | sum += matrix[i][j]; 29 | } 30 | if (sum % 2 == 1) 31 | { 32 | ++oddRowCount; 33 | oddRow = i; 34 | } 35 | rowSum[i] = sum; 36 | } 37 | // Scan every column. 38 | for (int j = 0; j < n; ++j) 39 | { 40 | int sum = 0; 41 | for (int i = 0; i < n; ++i) 42 | { 43 | sum += matrix[i][j]; 44 | } 45 | if (sum % 2 == 1) 46 | { 47 | ++oddColumnCount; 48 | oddColumn = j; 49 | } 50 | columnSum[j] = sum; 51 | } 52 | if (oddRowCount == 0 && oddColumnCount == 0) 53 | cout << "OK" << endl; 54 | else if (oddRowCount == 1 && oddColumnCount == 1) 55 | cout << "Change bit (" 56 | << oddRow + 1 57 | << "," 58 | << oddColumn + 1 59 | << ")" 60 | << endl; 61 | else 62 | cout << "Corrupt" << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /UVa 567 - Risk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | static const int N = 20; 8 | static const int INF = N * N; 9 | 10 | int main() 11 | { 12 | int TEST = 1, n; 13 | while (cin >> n) 14 | { 15 | // sp: shortest path. 16 | vector > sp(N + 1, vector(N + 1, INF)); 17 | for (int i = 1; i <= N; ++i) 18 | sp[i][i] = 0; 19 | 20 | int u = 1; 21 | do 22 | { 23 | for (int i = 1; i <= n; ++i) 24 | { 25 | int v; 26 | cin >> v; 27 | sp[u][v] = 1; 28 | sp[v][u] = 1; 29 | } 30 | if (++u > 19) 31 | break; 32 | } while (cin >> n); 33 | 34 | // Floyd-Warshall algorithm. 35 | for (int k = 1; k <= N; ++k) 36 | for (int i = 1; i <= N; ++i) 37 | for (int j = 1; j <= N; ++j) 38 | sp[i][j] = min(sp[i][j], sp[i][k] + sp[k][j]); 39 | 40 | cout << "Test Set #" << TEST++ <> n; 42 | while ( n-- ) 43 | { 44 | int s, t; 45 | cin >> s >> t; 46 | cout << right << setw(2) 47 | << s 48 | << " to " 49 | << right << setw(2) 50 | << t 51 | << ": " 52 | << left 53 | << sp[s][t] 54 | << endl; 55 | } 56 | cout << endl; 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /UVa 579 - ClockHands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int h, m; 9 | char c; 10 | while (cin >> h >> c >> m, h != 0 || m != 0) 11 | { 12 | double hAngle = (360.0 / 12) * h + ((360.0 / 12) / 60) * m; 13 | double mAngle = (360.0 / 60) * m; 14 | double angle = max(hAngle - mAngle, mAngle - hAngle); 15 | cout << setprecision(3) 16 | << fixed 17 | << min(angle, 360.0 - angle) 18 | << endl; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /UVa 591 - Box of Bricks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int set = 1; 10 | int n; 11 | while (cin >> n, n > 0) 12 | { 13 | vector stacks; 14 | for (int i = 0; i < n; ++i) 15 | { 16 | int stack; 17 | cin >> stack; 18 | stacks.push_back(stack); 19 | } 20 | // Compute the average height. 21 | int average = accumulate(stacks.begin(), stacks.end(), 0) / n; 22 | int k = 0; 23 | for (int i = 0; i < n; ++i) 24 | { 25 | if (stacks[i] > average) 26 | k += stacks[i] - average; 27 | } 28 | cout << "Set #" << set++ << endl; 29 | cout << "The minimum number of moves is " 30 | << k 31 | << "." 32 | << endl 33 | << endl; // Output a blank line after each set 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa 621 - Secret Research.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | size_t n; 8 | cin >> n; 9 | while ( n-- ) 10 | { 11 | string s; 12 | cin >> s; 13 | 14 | // Negative result. 15 | if (s.size() >= 2 && s.substr(s.size() - 2, 2) == "35") 16 | cout << "-" << endl; 17 | // Experiment failed. 18 | else if (s[0] == '9' && s[s.size() - 1] == '4') 19 | cout << "*" << endl; 20 | // Experiment not completed. 21 | else if (s.size() >= 3 && s.substr(0, 3) == "190") 22 | cout << "?" << endl; 23 | // Positive result. 24 | else 25 | cout << "+" << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /UVa 674 - Coin Change - Bottom-Up DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int coins[] = {50, 25, 10, 5, 1}; 5 | int ways[8192]; 6 | 7 | int main() 8 | { 9 | // The problem description says "we count that there is one way 10 | // of making change for zero cent". 11 | ways[0] = 1; 12 | 13 | // Every j cent (j = 0 to 8191) can be reached by taking off one 14 | // of the coins, so just sum up their ways. 15 | for (int i = 0; i < 5; ++i) 16 | for (int j = coins[i]; j < 8192; ++j) 17 | ways[j] += ways[j - coins[i]]; 18 | 19 | int money; 20 | while (cin >> money) 21 | { 22 | cout << ways[money] << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa 674 - Coin Change - Top-Down DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /** This solution uses Top-Down DP, but the problem can be solved more 6 | efficiently by Bottom-Up DP. 7 | */ 8 | 9 | int coins[] = {50, 25, 10, 5, 1}; 10 | 11 | /** 12 | memo[m][i] stores the number of different ways of making changes 13 | for m cents using the coins of index >= i. 14 | */ 15 | int memo[8192][5]; 16 | 17 | int waysOfChanges(int money, int coinsIndex) 18 | { 19 | if (money == 0) 20 | return 1; 21 | if (memo[money][coinsIndex] != -1) 22 | return memo[money][coinsIndex]; 23 | 24 | int ways = 0; 25 | 26 | /** Consider the coins of index >= coinsIndex. This is to avoid 27 | repetitions in the search. For example, for money = 11, 5 5 1 28 | and 1 5 5 should be counted as the same way. 29 | */ 30 | for (int i = coinsIndex; i < 5; ++i) 31 | { 32 | if (money >= coins[i]) 33 | ways += waysOfChanges(money - coins[i], i); 34 | } 35 | 36 | return memo[money][coinsIndex] = ways; 37 | } 38 | 39 | int main() 40 | { 41 | int money; 42 | 43 | // No need to reset memo for every input case. 44 | memset(memo, -1, sizeof memo); 45 | while (cin >> money) 46 | { 47 | cout << waysOfChanges(money, 0) << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa 714 - Copying Books.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int N; 10 | cin >> N; 11 | while ( N-- ) 12 | { 13 | int m, k; 14 | cin >> m >> k; // m books, k scribes. 15 | 16 | vector p(m); 17 | for (int i = 0; i < m; ++i) 18 | cin >> p[i]; 19 | 20 | vector finalDivision; 21 | long long U = accumulate(p.begin(), p.end(), 0LL); 22 | long long L = *max_element(p.begin(), p.end()); 23 | while (U > L) 24 | { 25 | long long M = (L + U) / 2; 26 | long long sum = 0, maxSum = 0; 27 | vector division; 28 | for (int i = m - 1; i >= 0; --i) 29 | { 30 | // Switch to the next scribe? 31 | if (sum + p[i] > M // This scribe is overloaded. 32 | || i + 1 < k - division.size()) // Leave at least one book for each scribe. 33 | { 34 | division.push_back(i); 35 | sum = p[i]; 36 | } 37 | else 38 | sum += p[i]; 39 | 40 | maxSum = max(maxSum, sum); 41 | } 42 | 43 | if (division.size() + 1 == k) 44 | finalDivision = division; 45 | 46 | // If no scribe's load is too heavy or there are too many devisions, increase L. 47 | if (maxSum > M || division.size() + 1 > k) 48 | L = M; 49 | else 50 | U = M; 51 | 52 | if ((L + U) / 2 == M) 53 | break; 54 | } 55 | 56 | for (int i = 0; i < m; ++i) 57 | { 58 | cout << (i > 0? " " : "") << p[i]; 59 | if (!finalDivision.empty() 60 | && finalDivision.back() == i) 61 | { 62 | cout << " /"; 63 | finalDivision.pop_back(); 64 | } 65 | } 66 | cout << endl; 67 | } 68 | } -------------------------------------------------------------------------------- /UVa 763 - Fibinary Numberscpp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int Case = 1; 8 | string n1, n2; 9 | while (cin >> n1 >> n2) 10 | { 11 | if (n2.length() > n1.length()) 12 | swap(n1, n2); 13 | 14 | for (int i = (int)n1.size() - 1, j = (int)n2.size() - 1 15 | ; i >= 0 && j >= 0 16 | ; --i, --j) 17 | { 18 | n1[i] += n2[j] - '0'; 19 | } 20 | 21 | n1.insert(0, "000"); 22 | bool hasModification = false; 23 | // Loop until there is no changing n1. 24 | do 25 | { 26 | hasModification = false; 27 | for (size_t i = 1; i < n1.size(); ++i) 28 | { 29 | // 0200 30 | // -> 1001 31 | while (n1[i] > '1') 32 | { 33 | hasModification = true; 34 | if (i == n1.size() - 2) 35 | { 36 | ++n1[i + 1]; 37 | ++n1[i - 1]; 38 | } 39 | else if (i == n1.size() - 1) 40 | ++n1[i - 1]; 41 | else 42 | { 43 | ++n1[i - 1]; 44 | ++n1[i + 2]; 45 | } 46 | n1[i] -= 2; 47 | } 48 | // 100 49 | // -> 011 50 | while (i < n1.size() - 1 && n1[i] > '0' && n1[i + 1] > '0') 51 | { 52 | hasModification = true; 53 | ++n1[i - 1]; 54 | --n1[i]; 55 | --n1[i + 1]; 56 | } 57 | } 58 | } while (hasModification); 59 | 60 | // Erase leading zeros. 61 | while (n1[0] == '0' && n1.size() > 1) 62 | n1.erase(0, 1); 63 | 64 | if (Case++ > 1) 65 | cout << endl; 66 | cout << n1 << endl; 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /UVa 821 - Page Hopping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const static int N = 100; 8 | const static int INF = 1000; 9 | 10 | int main() 11 | { 12 | int Case = 1; 13 | int a, b; 14 | while (cin >> a >> b, !(a == 0 && b == 0)) 15 | { 16 | vector > adj(N + 1, vector(N + 1, INF)); 17 | 18 | do { 19 | adj[a][b] = 1; 20 | } while (cin >> a >> b, !(a == 0 && b == 0)); 21 | 22 | // Floyd-Warshall algorithm. 23 | for (int k = 1; k <= N; ++k) 24 | for (int i = 1; i <= N; ++i) 25 | for (int j = 1; j <= N; ++j) 26 | adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); 27 | 28 | int sum = 0, count = 0; 29 | for (int i = 1; i <= N; ++i) 30 | for (int j = 1; j <= N; ++j) 31 | if (i != j && adj[i][j] != INF) 32 | { 33 | sum += adj[i][j]; 34 | ++count; 35 | } 36 | 37 | cout << "Case " 38 | << Case++ 39 | << ": average length between pages = " 40 | << setprecision(3) 41 | << fixed 42 | << (double)sum / count 43 | << " clicks" 44 | << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa 836 - Largest Submatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int MINUS_INF = -(25 * 25); 7 | 8 | int main() 9 | { 10 | size_t N; 11 | string s; 12 | 13 | cin >> N; 14 | cin.ignore(); 15 | getline(cin, s); 16 | 17 | while ( N-- ) 18 | { 19 | vector > M; // Cumulative sum matrix. 20 | size_t r = 0; 21 | while (getline(cin, s) && s != "") 22 | { 23 | M.push_back(vector()); 24 | for (size_t c = 0; c < s.size(); ++c) 25 | { 26 | // To include any '0' element will make a submatrix 27 | // infeasible. 28 | M[r].push_back(s[c] == '0'? MINUS_INF : 1); 29 | if (r > 0) 30 | M[r][c] += M[r - 1][c]; 31 | if (c > 0) 32 | M[r][c] += M[r][c - 1]; 33 | if (r > 0 && c > 0) 34 | M[r][c] -= M[r - 1][c - 1]; 35 | } 36 | ++r; 37 | } 38 | 39 | int maxSubRect = 0; 40 | // (sr,sc) 41 | // x x x 42 | // x x x 43 | // x x x 44 | // (er, ec) 45 | for (size_t sr = 0; sr < M.size(); ++sr) 46 | for (size_t sc = 0; sc < M.size(); ++sc) 47 | for (size_t er = sr; er < M.size(); ++er) 48 | for (size_t ec = sc; ec < M.size(); ++ec) 49 | { 50 | int subRect = M[er][ec]; 51 | if (sr > 0) 52 | subRect -= M[sr - 1][ec]; 53 | if (sc > 0) 54 | subRect -= M[er][sc - 1]; 55 | if (sr > 0 && sc > 0) 56 | subRect += M[sr - 1][sc - 1]; 57 | maxSubRect = max(maxSubRect, subRect); 58 | } 59 | 60 | cout << maxSubRect << endl; 61 | if (N) 62 | cout << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /UVa 869 - Airline Comparison.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static const int SIZE = 128; 7 | 8 | int main() 9 | { 10 | int T; 11 | cin >> T; 12 | while ( T-- ) 13 | { 14 | int N; 15 | cin >> N; 16 | 17 | vector > C1(SIZE, vector(SIZE, 0)); 18 | 19 | for (int i = 0; i < N; ++i) 20 | { 21 | char u, v; 22 | cin >> u >> v; 23 | C1[u][v] = 1; 24 | } 25 | 26 | int M; 27 | cin >> M; 28 | 29 | for (int k = 0; k < SIZE; ++k) 30 | for (int i = 0; i < SIZE; ++i) 31 | for (int j = 0; j < SIZE; ++j) 32 | if (C1[i][k] && C1[k][j]) 33 | C1[i][j] = 1; 34 | 35 | vector > C2(SIZE, vector(SIZE, 0)); 36 | 37 | for (int i = 0; i < M; ++i) 38 | { 39 | char u, v; 40 | cin >> u >> v; 41 | C2[u][v] = 1; 42 | } 43 | 44 | for (int k = 0; k < SIZE; ++k) 45 | for (int i = 0; i < SIZE; ++i) 46 | for (int j = 0; j < SIZE; ++j) 47 | if (C2[i][k] && C2[k][j]) 48 | C2[i][j] = 1; 49 | 50 | if (C1 == C2) 51 | cout << "YES" << endl; 52 | else 53 | cout << "NO" << endl; 54 | 55 | if (T) 56 | cout << endl; 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /UVa 908 - Re-connecting Computer Sites.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Edge 7 | { 8 | int u, v, cost; 9 | }; 10 | 11 | bool comp(const Edge &e1, const Edge &e2) 12 | { 13 | return e1.cost < e2.cost; 14 | } 15 | 16 | int getGroup(const vector &groups, int u) 17 | { 18 | return groups[u] == u? 19 | u : getGroup(groups, groups[u]); 20 | } 21 | 22 | int main() 23 | { 24 | int T = 0, N; 25 | while (cin >> N) 26 | { 27 | int cost1 = 0, cost2 = 0; 28 | for (int i = 1; i <= N - 1; ++i) 29 | { 30 | int u, v, c; 31 | cin >> u >> v >> c; 32 | cost1 += c; 33 | } 34 | 35 | vector lines; 36 | vector groups; 37 | int K, M; 38 | 39 | cin >> K; 40 | for (int i = 1; i <= K; ++i) 41 | { 42 | Edge e; 43 | cin >> e.u >> e.v >> e.cost; 44 | lines.push_back(e); 45 | } 46 | cin >> M; 47 | for (int i = 1; i <= M; ++i) 48 | { 49 | Edge e; 50 | cin >> e.u >> e.v >> e.cost; 51 | lines.push_back(e); 52 | } 53 | 54 | sort(lines.begin(), lines.end(), comp); 55 | 56 | for (int i = 0; i <= N; ++i) 57 | groups.push_back(i); 58 | 59 | // Kruskal's algorithm. 60 | for (size_t i = 0; i < lines.size(); ++i) 61 | { 62 | Edge &e = lines[i]; 63 | int uGroup = getGroup(groups, e.u); 64 | int vGroup = getGroup(groups, e.v); 65 | 66 | // Skip the edge if it forms a cycle. 67 | if (uGroup == vGroup) 68 | continue; 69 | 70 | cost2 += e.cost; 71 | // Set v's group to u's group. 72 | groups[vGroup] = uGroup; 73 | } 74 | if (T++) 75 | cout << endl; 76 | cout << cost1 << endl 77 | << cost2 << endl; 78 | } 79 | return 0; 80 | } -------------------------------------------------------------------------------- /UVa 920 - Sunny Mountains.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Point { int x, y; }; 9 | 10 | // Return the Euclidean distance between a and b. 11 | double dist(Point a, Point b) 12 | { 13 | return sqrt((double)(a.x - b.x) * (a.x - b.x) 14 | + (a.y - b.y) * (a.y - b.y)); 15 | } 16 | 17 | bool comp(const Point &a, const Point &b) 18 | { 19 | return b.x < a.x; 20 | } 21 | 22 | int main() 23 | { 24 | int C; 25 | cin >> C; 26 | while ( C-- ) 27 | { 28 | int N; 29 | cin >> N; 30 | vector points; 31 | for (int i = 1; i <= N; ++i) 32 | { 33 | Point point; 34 | cin >> point.x >> point.y; 35 | points.push_back(point); 36 | } 37 | 38 | // Sort the points in descending order of x-coordinate. 39 | sort(points.begin(), points.end(), comp); 40 | 41 | /** 42 | points[i].y 43 | \* 44 | \* --- currentMaxY 45 | \ 46 | \ 47 | points[i - 1].y 48 | 49 | The line segment marked by * can be computed by 50 | 51 | distance(points[i], points[i - 1]) * 52 | (points[i].y - currentMaxY) / (points[i].y - points[i - 1].y) 53 | */ 54 | int currentMaxY = 0; 55 | double length = 0; 56 | for (int i = 1; i < points.size(); ++i) 57 | { 58 | if (points[i].y > currentMaxY) 59 | { 60 | length += dist(points[i], points[i - 1]) 61 | * (points[i].y - currentMaxY) / (points[i].y - points[i - 1].y); 62 | currentMaxY = points[i].y; 63 | } 64 | } 65 | 66 | // Print with two decimal digits. 67 | cout << setprecision(2) 68 | << setiosflags(ios::fixed) 69 | << length << endl; 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /UVa 929 - Number Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int dx[] = {-1, 0, 1, 0}; 7 | int dy[] = {0, 1, 0, -1}; 8 | 9 | struct Edge 10 | { 11 | int r, c; 12 | int cost; 13 | // For std::priority_queue. 14 | bool operator>(const Edge &e) const 15 | { 16 | return this->cost > e.cost; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | int T; 23 | cin >> T; 24 | while ( T-- ) 25 | { 26 | int N, M; 27 | cin >> N >> M; 28 | 29 | vector > maze(N, vector(M)); 30 | 31 | for (int r = 0; r < N; ++r) 32 | for (int c = 0; c < M; ++c) 33 | cin >> maze[r][c]; 34 | 35 | priority_queue, greater > pq; 36 | vector > cost(N, vector(M, -1)); 37 | 38 | // Dijkstra's algorithm using min-heap. 39 | Edge e; 40 | e.r = 0; 41 | e.c = 0; 42 | e.cost = maze[0][0]; 43 | pq.push(e); 44 | cost[0][0] = e.cost; 45 | while (!pq.empty()) 46 | { 47 | Edge u = pq.top(); 48 | pq.pop(); 49 | for (int i = 0; i < 4; ++i) 50 | { 51 | int r = u.r + dx[i]; 52 | int c = u.c + dy[i]; 53 | if (r < 0 || r >= N || c < 0 || c >= M) 54 | continue; 55 | if (cost[r][c] == -1 56 | || cost[u.r][u.c] + maze[r][c] < cost[r][c]) 57 | { 58 | cost[r][c] = cost[u.r][u.c] + maze[r][c]; 59 | e.r = r; 60 | e.c = c; 61 | // Store cost[r][c] rather than maze[r][c]. 62 | e.cost = cost[r][c]; 63 | pq.push(e); 64 | } 65 | } 66 | } 67 | cout << cost[N - 1][M - 1] << endl; 68 | } 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Uva 10176 - Ocean Deep ! - Make it shallow !!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | static const int P = 131071; 6 | 7 | int main() 8 | { 9 | string n; 10 | int v = 0; 11 | while (getline(cin, n)) 12 | { 13 | // Horner's method. 14 | for (size_t i = 0; i < n.length(); ++i) 15 | { 16 | if (n[i] == '#') 17 | { 18 | cout << (v == 0? "YES" : "NO") << endl; 19 | v = 0; 20 | } 21 | else if (n[i] >= '0' && n[i] <= '9') 22 | v = ((n[i] - '0') + (v << 1)) % P; 23 | } 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Uva 10986 - Sending email.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Cable 9 | { 10 | int u, v; 11 | int latency; 12 | 13 | // For std::priority_queue. 14 | bool operator>(const Cable &c) const 15 | { 16 | return this->latency > c.latency; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | int Case = 1, N; 23 | cin >> N; 24 | while ( N-- ) 25 | { 26 | int n, m, S, T; 27 | map > cables; 28 | map dist; 29 | 30 | cin >> n >> m >> S >> T; 31 | for (int i = 0; i < m; ++i) 32 | { 33 | Cable cable; 34 | cin >> cable.u >> cable.v >> cable.latency; 35 | cables[cable.u].push_back(cable); 36 | swap(cable.u, cable.v); 37 | cables[cable.u].push_back(cable); 38 | } 39 | 40 | // Dijkstra's algorithm using min-heap. 41 | priority_queue, greater > pq; 42 | 43 | Cable s; 44 | s.u = S; 45 | s.v = S; 46 | s.latency = 0; 47 | pq.push(s); 48 | dist[S] = 0; 49 | while (!pq.empty()) 50 | { 51 | Cable e1 = pq.top(); 52 | pq.pop(); 53 | // Lazy deletion: if dist[e1.v] is smaller, that means e1.v 54 | // has been visited already. 55 | if (dist[e1.u] + e1.latency > dist[e1.v]) 56 | continue; 57 | for (size_t i = 0; i < cables[e1.v].size(); ++i) 58 | { 59 | Cable e2 = cables[e1.v][i]; 60 | // Push the edge. 61 | if (!dist.count(e2.v) 62 | || dist[e2.u] + e2.latency < dist[e2.v]) 63 | { 64 | dist[e2.v] = dist[e2.u] + e2.latency; 65 | pq.push(e2); 66 | } 67 | } 68 | } 69 | cout << "Case #" 70 | << Case++ 71 | << ": "; 72 | if (dist.count(T)) 73 | cout << dist[T] << endl; 74 | else 75 | cout << "unreachable" << endl; 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Uva 11088 - End up with More Teams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int N; 7 | vector C; 8 | int memo[1 << 20]; 9 | 10 | int search(int bitMask) 11 | { 12 | if (bitMask == (1 << N) - 1) 13 | return 0; 14 | 15 | if (memo[bitMask] != -1) 16 | return memo[bitMask]; 17 | 18 | int pTeams = 0; 19 | for (int i = 0; i < N; ++i) 20 | if ((bitMask & (1 << i)) == 0) 21 | for (int j = i + 1; j < N; ++j) 22 | if ((bitMask & (1 << j)) == 0) 23 | for (int k = j + 1; k < N; ++k) 24 | if ((bitMask & (1 << k)) == 0) 25 | pTeams = max(pTeams, (C[i] + C[j] + C[k] >= 20? 1 : 0) 26 | + search(bitMask | (1 << i) | (1 << j) | (1 << k))); 27 | 28 | return memo[bitMask] = pTeams; 29 | } 30 | 31 | int solve() 32 | { 33 | memset(memo, -1, sizeof (memo)); 34 | return search(0); 35 | } 36 | 37 | int main() 38 | { 39 | int Case = 1; 40 | while (cin >> N, N > 0) 41 | { 42 | C.resize(N); 43 | for (int i = 0; i < N; ++i) 44 | cin >> C[i]; 45 | 46 | cout << "Case " << Case++ << ": " << solve() << endl; 47 | } 48 | return 0; 49 | } 50 | --------------------------------------------------------------------------------