Commits

Martinho Fernandes committed 47eae36

Solved 10008: What's Cryptanalysis?

Comments (0)

Files changed (3)

+/*
+ * 10008: What's Cryptanalysis?
+ * http://uva.onlinejudge.org/external/100/10008.html
+ */
+
+#ifdef ONLINE_JUDGE
+#define NDEBUG
+#endif
+
+#include <algorithm>
+#include <cassert>
+#include <cctype>
+#include <iostream>
+#include <limits>
+#include <map>
+#include <string>
+#include <vector>
+using namespace std;
+
+bool comp(pair<char,int> l, pair<char,int> r) {
+    if(l.second > r.second) return true;
+
+    if(l.second == r.second && l.first < r.first) return true;
+
+    return false;
+}
+
+int main() {
+    int n;
+    cin >> n;
+    cin.ignore(numeric_limits<streamsize>::max(), '\n');
+    vector<string> lines(n);
+    for(vector<string>::iterator it = lines.begin(); it != lines.end(); ++it) {
+        getline(cin, *it);
+    }
+
+    map<char,int> counts;
+    for(vector<string>::iterator it = lines.begin(); it != lines.end(); ++it) {
+        for(string::iterator cit = it->begin(); cit != it->end(); ++cit) {
+            char up = toupper(*cit);
+            if(up >= 'A' && up <= 'Z') {
+                counts[up]++;
+            }
+        }
+    }
+    vector<pair<char,int> > result;
+    copy(counts.begin(), counts.end(), back_inserter(result));
+    sort(result.begin(), result.end(), comp);
+
+    for(vector<pair<char,int> >::iterator it = result.begin(); it != result.end(); ++it) {
+        cout << it->first << " " << it->second << endl;
+    }
+
+    return 0;
+}

test/10008.example.in

+3
+This is a test.
+Count me 1 2 3 4 5.
+Wow!!!!  Is this question easy?

test/10008.example.out

+S 7
+T 6
+I 5
+E 4
+O 3
+A 2
+H 2
+N 2
+U 2
+W 2
+C 1
+M 1
+Q 1
+Y 1