Solution of 11577 - Letter Frequency

Problem Description
source:http://uva.onlinejudge.org/external/115/11577.html



In this problem we are interested in the frequency of letters in a given line of text. Specifi- cally, we want to know the most frequently occurring letter(s) in the text, ignoring case (to be clear, “letters” refers precisely to the 26 letters of the alphabet).

Input 


Input begins with the number of test cases on its own line. Each test case consists of a single line of text. The line may contain non-letter characters, but is guaranteed to contain at least one letter and less than 200 characters in total. 

Output 

For each test case, output a line containing the most frequently occurring letter(s) from the text in lowercase (if there are ties, output all such letters in alphabetical order). 

Sample Input 


Computers account for only 5% of the country’s commercial electricity consumption. 

Sample Output 

co


Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>

using namespace std;

int main()
{
    long int i, j, k, n, t, a[30];
    char lf[205];
    while(scanf("%ld",&t)==1)
    {
        getchar();
        for(i=1;i<=t;i++)
        {
            memset(lf,'\0',sizeof(lf));
            gets(lf);
            memset(a,0,sizeof(a));
            for(j=0;lf[j]!='\0';j++)
            {
                switch(lf[j])
                {
                    case 'a':
                    case 'A':
                    {
                        a[1]++;
                        break;
                    }
                    case 'b':
                    case 'B':
                    {
                        a[2]++;
                        break;
                    }
                    case 'c':
                    case 'C':
                    {
                        a[3]++;
                        break;
                    }
                    case 'd':
                    case 'D':
                    {
                        a[4]++;
                        break;
                    }
                    case 'e':
                    case 'E':
                    {
                        a[5]++;
                        break;
                    }
                    case 'f':
                    case 'F':
                    {
                        a[6]++;
                        break;
                    }
                    case 'g':
                    case 'G':
                    {
                        a[7]++;
                        break;
                    }
                    case 'h':
                    case 'H':
                    {
                        a[8]++;
                        break;
                    }
                    case 'i':
                    case 'I':
                    {
                        a[9]++;
                        break;
                    }
                    case 'j':
                    case 'J':
                    {
                        a[10]++;
                        break;
                    }
                    case 'k':
                    case 'K':
                    {
                        a[11]++;
                        break;
                    }
                    case 'l':
                    case 'L':
                    {
                        a[12]++;
                        break;
                    }
                    case 'm':
                    case 'M':
                    {
                        a[13]++;
                        break;
                    }
                    case 'n':
                    case 'N':
                    {
                        a[14]++;
                        break;
                    }
                    case 'o':
                    case 'O':
                    {
                        a[15]++;
                        break;
                    }
                    case 'p':
                    case 'P':
                    {
                        a[16]++;
                        break;
                    }
                    case 'q':
                    case 'Q':
                    {
                        a[17]++;
                        break;
                    }
                    case 'r':
                    case 'R':
                    {
                        a[18]++;
                        break;
                    }
                    case 's':
                    case 'S':
                    {
                        a[19]++;
                        break;
                    }
                    case 't':
                    case 'T':
                    {
                        a[20]++;
                        break;
                    }
                    case 'u':
                    case 'U':
                    {
                        a[21]++;
                        break;
                    }
                    case 'v':
                    case 'V':
                    {
                        a[22]++;
                        break;
                    }
                    case 'w':
                    case 'W':
                    {
                        a[23]++;
                        break;
                    }
                    case 'x':
                    case 'X':
                    {
                        a[24]++;
                        break;
                    }
                    case 'y':
                    case 'Y':
                    {
                        a[25]++;
                        break;
                    }
                    case 'z':
                    case 'Z':
                    {
                        a[26]++;
                        break;
                    }
                    default:
                        continue;
                }
            }
           long int len=*max_element(a,a+28);
           for(j=1;j<=26;j++)
           {
               if(a[j]==len)
                printf("%c",j+96);
           }
            printf("\n");
        }
    }
    return 0;
}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience