problem link:
Discuss: in this problem you can use map for repeat the alphabet then copy the second element in the vector then sort it and erase all duplicate element in array. then check vector element with second element of map. if match then print the map element..
try your self before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,i,j,flag,m,d;
map<char,int>mp;
char str[1000];
vector<int>vec;
cin >>t;
while(t--)
{
scanf("\n");
gets(str);
n=strlen(str);
for(i=0;i<n;i++)
{
str[i]=toupper(str[i]);
if(str[i]>='A'&&str[i]<='Z')
{
mp[str[i]]++;
}
}
}
for (map<char, int>::const_iterator it = mp.begin(); it != mp.end(); ++it)
{
d=it->second;
vec.push_back(d);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
m=vec.size();
for(i=m-1;i>=0;i--)
{
for (map<char, int>::const_iterator it = mp.begin(); it != mp.end(); ++it)
{
d=it->second;
if(d==vec[i])
{
cout <<it->first<<' '<<d<<endl;
}
}
}
return 0;
}
Discuss: in this problem you can use map for repeat the alphabet then copy the second element in the vector then sort it and erase all duplicate element in array. then check vector element with second element of map. if match then print the map element..
try your self before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,i,j,flag,m,d;
map<char,int>mp;
char str[1000];
vector<int>vec;
cin >>t;
while(t--)
{
scanf("\n");
gets(str);
n=strlen(str);
for(i=0;i<n;i++)
{
str[i]=toupper(str[i]);
if(str[i]>='A'&&str[i]<='Z')
{
mp[str[i]]++;
}
}
}
for (map<char, int>::const_iterator it = mp.begin(); it != mp.end(); ++it)
{
d=it->second;
vec.push_back(d);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
m=vec.size();
for(i=m-1;i>=0;i--)
{
for (map<char, int>::const_iterator it = mp.begin(); it != mp.end(); ++it)
{
d=it->second;
if(d==vec[i])
{
cout <<it->first<<' '<<d<<endl;
}
}
}
return 0;
}
No comments:
Post a Comment