Zliczenie częstości wystąpienia liter w podanym tekście

0

Hej, mam mały problem z zadaniem ze SPOJ http://pl.spoj.com/problems/AL_14_01/
Program ma za zadanie, zliczyć częstość wystąpienia liter w tekście, napisałem taki oto programik poniżej, progam wyświetla '' w zależnosci w ilu % wystąpiła litera. Miałem to napisane również tak, że osobno wypisywało literke i wypisywało '' od ilości %, ale również nie przechodziło, choć % są poprawne. Podsyłam wersje, gdzie wrzuciłem wszystko do vectora, z myślą, że tak przejdzie.
Niestety program, nie przechodzi w spoju, mógłby ktoś rzucić okiem i pokierować gdzie mogłem popełnić błąd?
Prosiłbym o wyrozumiałość, dla początkującego :)

 
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

int percent(int length, int howMuch) {
    if(length <= 0) return 0;
    return ((howMuch * 100) / length);    
}

int main() {
    string tekst;
    vector< string > results;
    results.resize(26);
    int n;
    getline(cin, tekst); 
    transform(tekst.begin(), tekst.end(), tekst.begin(), ::toupper);
    n = tekst.length();
    
    if(n >= 1 && n <= 1000000) {

        char letters[26];
        for(int i = 0; i < 26; ++i) {
            letters[i] = (char)(i + 65); // wypelnienie literkami
        }

        int table[26]; 
        for(int i = 0; i < 26; ++i) table[i] = 0; // clearing table
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < 26; j++) {
                if( letters[j] == tekst[i]) {
                    table[j] += 1;    
                }   
            }    
        }

        for(int i = 0; i < tekst.length(); ++i) if(tekst[i] == (char)32) n -= 1; // deleting whitespaces

        for(int i = 0; i < 26; ++i) {
            table[i] = percent(n, table[i]);    
        }

        for(int i = 0; i < 26; ++i) {
            results[i] += letters[i];
            for(int j = 0; j < table[i]; ++j) {   
                results[i] += "*";
            }
        }

        for(int i = 0; i < 26; ++i) {
            cout << results[i] << endl;
        }
    }
    
    return 0;    
}
3

o_O
potrafisz użyć std::transform a robisz takie coś:

            for(int j = 0; j < 26; j++) {
                if( letters[j] == tekst[i]) {
                    table[j] += 1;    
                }   
            }  

To jest w ogóle możliwe? o_O A nie dało się

table[tekst[i]-'A']++

? ;]
Ten sam kod w wersji trochę bardziej ludzkiej:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
 
using namespace std;
 
void percent(int length, int howMuch, int letter) {
	int percent = length > 0 ? (howMuch * 100) / length : 0;
	cout<<char('A'+letter);
	for(int i=0;i<percent;i++){
		cout<<"*";
	}
	cout<<endl;
}
 
int main() {
    string tekst;
    int n;
    getline(cin, tekst); 
    tekst.erase(remove(tekst.begin(), tekst.end(), ' '), tekst.end());
    n = tekst.length();

    int table[26] = {};
    for(int i = 0; i < n; i++) {
    	table[tekst[i]-'A']++;
    }

    for(int i = 0; i < 26; i++) {
    	percent(n, table[i], i);
    }
    return 0;    
}
2

Czyli próbowałeś zrobić coś takiego:

#include <cstdio>
#include <cctype>
using namespace std;

int main()
  {
   unsigned Tb[26]={0},cnt=0;
   for(int ch;(ch=getchar())!=EOF;) if(isalpha(ch)) ++Tb[ch-'A'];
   for(int i=0;i<26;++i) cnt+=Tb[i];
   unsigned half=cnt>>1;
   for(int i=0;i<26;++i)
     {
      printf("%c",'A'+i);
      for(unsigned aster=(100*Tb[i]+half)/cnt;aster--;) putchar('*');
      printf("\n");
     }
   return 0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1