Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- const int LENGTH_ALPHABET = 'z' - 'a' + 1;
- char lletra_mes_frequent(const string& s) {
- vector <int> lmf(LENGTH_ALPHABET, 0);
- int tams = s.size();
- for (int i = 0; i < tams; ++i) {
- ++lmf[s[i]-'a'];
- }
- int pos, freq = 0;
- for (int i = 0; i < LENGTH_ALPHABET; ++i) {
- if (lmf[i] > freq) {
- pos = i;
- freq = lmf[i];
- }
- }
- char c = 'a' + pos;
- return c;
- }
- //Pre: Llegeix un seguit d'n paraules
- //Post: Escriu la longitut mitjana i de totes les paraules que tenen una longitut superior a aquesta, la lletra mĂŠs repetida
- int main() {
- cout.setf(ios::fixed);
- cout.precision(2);
- int n;
- double longitut = 0;
- string s;
- cin >> n;
- int aux = n;
- vector<string> v(n); //Guardem la paraula
- vector<int> u(n); //Guardem el tamany de la paraula
- for (int i = 0; i < n; ++i) {
- cin >> s;
- int tams = s.size();
- v[i] = s;
- u[i] = tams;
- longitut += tams;
- }
- longitut /= aux;
- cout << longitut << endl;
- for (int i = 0; i < aux; ++i) {
- if (u[i] >= longitut) {
- cout << v[i] << ": " << lletra_mes_frequent(v[i]) << endl;
- }
- }
- }
- // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment