// UVa 10336 - Rank the Languages #include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <stdio.h> using namespace std; struct pa { char c; int s; }; bool operator <(const pa & a, const pa & b) { return a.s > b.s || (a.s == b.s && a.c < b.c); } int n, m; vector<string> world; void dfs(int i, int j, char c) { if (world[i][j] == c) { world[i][j] = '0'; if (i > 0) dfs(i - 1, j, c); if (i < n - 1) dfs(i + 1, j, c); if (j > 0) dfs(i, j - 1, c); if (j < m - 1) dfs(i, j + 1, c); } } int main() { int tt; cin >> tt; for (int t = 1; t <= tt; t++) { cin >> n >> m; world.clear(); for (int i = 0; i < n; i++) { string line; cin >> line; world.push_back(line); } map<char, int> states; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (world[i][j] != '0') { states[world[i][j]]++; dfs(i, j, world[i][j]); } vector<pa> sol; for (char c = 'a'; c <= 'z'; c++) if (states[c]) { pa p = { c, states[c] }; sol.push_back(p); } sort(sol.begin(), sol.end()); printf("World #%d\n", t); for (int i = 0; i < sol.size(); i++) printf("%c: %d\n", sol[i].c, sol[i].s); } return 0; }
Monday, July 13, 2015
UVa 10336 - Rank the Languages
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment