Problema ACM de la pagina
http://icpcres.ecs.baylor.edu/onlinejudge/ el enunciado del problema en ingles es el siguiente:
What's Cryptanalysis? Cryptanalysis is the process of breaking someone else's cryptographic writing. This sometimes involves some kind of statistical analysis of a passage of (encrypted) text. Your task is to write a program which performs a simple analysis of a given text.
----Input
The first line of input contains a single positive decimal integer n. This is the number of lines which follow in the input. The next n lines will contain zero or more characters (possibly including whitespace). This is the text which must be analyzed.
----Output
Each line of output contains a single uppercase letter, followed by a single space, then followed by a positive decimal integer. The integer indicates how many times the corresponding letter appears in the input text. Upper and lower case letters in the input are to be considered the same. No other characters must be counted. The output must be sorted in descending count order; that is, the most frequent letter is on the first output line, and the last line of output indicates the least frequent letter. If two letters have the same frequency, then the letter which comes first in the alphabet must appear first in the output. If a letter does not appear in the text, then that letter must not appear in the output.
----Sample Input
3
This is a test.
Count me 1 2 3 4 5.
Wow!!!! Is this question easy?
----Sample Output
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
/*
Problema "What's Cryptanalysis?"
de la pagina UVa Online Judge
Resuelto por Esteban Arango Medina
Marzo 2007
*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <conio.h>
using namespace std;
void cryptanalysis(int numero);
void organizar();
typedef struct Tletras
{
char letraM;
char letram;
int veces;
};
struct Tletras letras [26];
void cryptanalysis(int numero){
char txt[100];
txt[0]=getchar();
for(int i=0; i<numero; i++){
cout<<"Ingrese la frase: "<<endl;
for(int n=0;(txt[n]=getchar()) != '\n';++n){
for(int i=0;i<26;i++){
if(txt[n]==letras[i].letraM || txt[n]==letras[i].letram){
letras[i].veces = letras[i].veces + 1;
i=i+26;
}
}
}
}
}
void organizar(){
int TAM=26;
struct Tletras temp;
for (int i=1; i<TAM; i++){
for (int j=0; j<TAM - 1; j++){
if (letras[j].veces < letras[j+1].veces){
temp = letras[j];
letras[j] = letras[j+1];
letras[j+1] = temp;
}
}
}
}
int main(int argc, char *argv[]){
int numero=0;
for (int i = 0; i<26;i++){
letras[i].letraM = (char)('A' + i);
letras[i].letram = (char)('a' + i);
letras[i].veces = 0;
}
cout<<"Ingrese el numero de frases: ";
cin>>numero;
cryptanalysis(numero);
organizar();
for (int i = 0; i<26;i++){
if(letras[i].veces != 0){
cout<<letras[i].letraM<<" "<<letras[i].veces<<endl;
}
}
getch();
return 0;
}
Autor: Esteban Arango Medina