#include<bits/stdc++.h> usingnamespace std; #define ll long long
intmain() { ll x; while (cin >> x && x) { ll res = 0; cout << "The parity of "; bool y = false; for (int i = 32; i >= 0; i--) if ((x >> i) & 1) { res++; cout << 1; y = true; } elseif (y) cout << 0; cout << " is " << res << " (mod 2).\n"; } }
UVA-10789 Prime Frequency
Since every case can only put 2001 size of string, I just pre-calculate every case.(from exist 1 ~ 2001 times). Then, use map to calculate individual alphabat exist n times, and it will auto sort too.
intmain() { bool nums[2002]; nums[0] = nums[1] = false; for (int i = 2; i < 2002; i++) { nums[i] = true; for (int j = sqrt(i); j > 1; j--) if (i % j == 0) nums[i] = false; } int t, n; cin >> t; for (int i = 0; i < t; i++) { cout << "Case " << i + 1 << ": "; string s; cin >> s; map<char, int> mp; for (char c : s) mp[c]++; bool isEmpty = true; for (auto it : mp) if (nums[it.second]) { isEmpty = false; if (it.first < 10) cout << int(it.first); else cout << it.first; } if (isEmpty) cout << "empty"; cout << "\n"; } }
UVA-10903 Rock-Paper-Scissors Tournament
Firstly, I define rock paper scissors to 0,1,2, respectfully. (call it t1, t2, for person 1, 2, respectfully.) We observed that this game has 3^2 = 9 ways to perform, to get result. To get result, I use a 3*3 matrix to store result, 1 for win, -1 for loss, 0 for even. result = pos[t1][t2], so to speak. Then, I put result into arrays ,and therefore get answers we wanted.
constint dir[5] = {0, -1, 0, 1, 0}; int m, n; vector<vector<char>> v;
voiddfs(int x, int y) { if (v[x][y] == 0) return; char val = v[x][y]; v[x][y] = 0; for (int i = 0; i < 4; i++) { int nx = x + dir[i]; int ny = y + dir[i + 1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && v[nx][ny] == val) dfs(nx, ny); } }
intmain() { int t; cin >> t; for (int cnt = 1; cnt <= t; cnt++) { cin >> m >> n; v.resize(m, vector<char>(n)); map<char, int> mp; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> v[i][j]; int res = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (v[i][j] != 0) mp[v[i][j]]++, dfs(i, j); cout << "World #" << cnt << "\n"; vector<pair<int, char>> ans; for (auto &it : mp) ans.push_back(make_pair(it.second, it.first)); sort( ans.begin(), ans.end(), [](const pair<int, char> &it1, const pair<int, char> &it2) -> bool { if(it1.first != it2.first) return it1.first > it2.first; return it1.second < it2.second; }); for (auto &it : ans) cout << it.second << ": " << it.first << "\n"; } }
We use Dynamic Programming to address this problem. That is, dp[i] means from s[0] to s[i-1], the minimum partition. Therefore, dp[s.size()] or dp.back() is the result.
intmain() { int t; cin >> t; while (t--) { string s; cin >> s; vector<int> dp(s.size() + 1); for (int i = 0; i <= s.size(); i++) dp[i] = i; // that is, dp 1 ~ s.size() is s: 0 ~ s.size()-1 for (int i = 1; i < s.size(); i++) { // even numbers of characters kind of palindrome int x = i - 1, y = i; while (x >= 0 && y < s.size() && s[x] == s[y]) { dp[y + 1] = min(dp[y + 1], dp[x] + 1); x--, y++; } // odd x = y = i; while (x >= 0 && y < s.size() && s[x] == s[y]) { dp[y + 1] = min(dp[y + 1], dp[x] + 1); x--, y++; } } cout << dp.back() << endl; } }
UVA-10635 Prince and Princess
A Longest Common Subsequence (LCS) Problem.
Fairly hard problem tho, mainly because it will TLE on the 2d DP solution.
Also, there’s a same problem on leetcode, up and running for those leetcode grinder. By the way, the 2d DP approach will pass on leetcode, and it’s fairly fast on those test case. No wonder it’s mark medium on leetcode. haha.
intdfs(vector<int> &t1, vector<int> &t2, int x, int y) { if (dp[x][y] != -1) return dp[x][y]; if (x >= t1.size() || y >= t2.size()) return0; int y1 = y, x1 = x; for (; y < t2.size(); y++) if (t1[x] == t2[y]) break; if (y >= t2.size()) return0; int res = 0; for (int i = x + 1; i < t1.size(); i++) res = max(res, dfs(t1, t2, i, y + 1) + 1); return dp[x1][y1] = res; }
But, it just not this case, right? Since elements in our list is unique, so our thing will not alike some normal LCS graph. Rather, it’s more easy.
According to this rule, we can confidently say that same row or column will have no more than 1 value. Therefore, it reduce the complexity that we don’t have to use pairs… sorting … stuff. All we have to do is:
map t1 value to t1 index.
through t2 iteration, add (if t1 get the same value) it’s t1 index to a vector.
Since vector is already in t2’s iteration, do LIS on vector’s value.