Thursday, July 20, 2017

Uva problem solution 11577 - Letter Frequency

problem link:


Discuss: in this problem if you simply use map then you'll get result. first search for second value for large number of time. then run another loop to match the value. if the value matched then just print the first element of the map.


try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{
        int n,t,i,j,cnt,flag,d;
        cin >>t;
        for(j=0;j<t;j++)
        {
            scanf("\n");
            char str[202],temp;
            map<char,int>mp;
            flag=0;
            gets(str);
            n=strlen(str);
            for(i=0;i<n;i++)
            {
                 str[i]=tolower(str[i]);
               if(str[i]!=' '&&str[i]>='a'&&str[i]<='z') {

                     temp=str[i];
              mp[temp]++;
                }

            }
            for(map<char,int>::const_iterator it=mp.begin();it!=mp.end();++it)
            {
                d=it->second;
                if(d>flag)
                    flag=d;

            }
            for(map<char,int>::const_iterator it=mp.begin();it!=mp.end();++it)
            {
                      d=it->second;

                if(d==flag)
                {
                        cout << it->first;

                }
            }
            cout <<endl;

        }
        return 0;
}

No comments:

Post a Comment