"Ever Dream" played by Nightwish is my favorite metal music. The lyric (see Sample Input) of this song is much more like a poem. Every people may have their own interpretation for this song depending on their own experience in the past. For me, it is a song about pure and unrequited love filled with innocence, willingness and happiness. I believe most people used to have or still have a long story with someone special or something special. However, perhaps fatefully, life is totally a joke for us. One day, the story ended and became a dream in the long night that would never come true. The song touches my heart because it reminds me the dream I ever had and the one I ever loved.
Today I recommend this song to my friends and hope that you can follow your heart. I also designed a simple algorithm to express the meaning of a song by several key words. There are only 3 steps in this algorithm, which are described below:
Step 1: Extract all different words from the song and counts the occurrences of each word. A word only consists of English letters and it is case-insensitive.
Step 2: Divide the words into different groups according to their frequencies (i.e. the number of times a word occurs). Words with the same frequency belong to the same group.
Step 3: For each group, output the word with the longest length. If there is a tie, sort these words (not including the words with shorter length) in alphabetical order and output the penultimate one. Here "penultimate" means the second to the last. The word with higher frequency should be output first and you don't need to output the word that just occurs once in the song.
Now given the lyric of a song, please output its key words by implementing the algorithm above.
Input
The first line of input is an integer T (T < 50) indicating the number of test cases. For each case, first there is a line containing the number n (n < 50) indicating that there are n lines of words for the song. The following n
Output
For each case, output the key words in a single line. Words should be in lower-case and separated by a space. If there is no key word, just output an empty line.
Sample Input
1
29
Ever felt away with me
Just once that all I need
Entwined in finding you one day
Ever felt away without me
My love, it lies so deep
Ever dream of me
Would you do it with me
Heal the scars and change the stars
Would you do it for me
Turn loose the heaven within
I'd take you away
Castaway on a lonely day
Bosom for a teary cheek
My song can but borrow your grace
Come out, come out wherever you are
So lost in your sea
Give in, give in for my touch
For my taste for my lust
Your beauty cascaded on me
In this white night fantasy
"All I ever craved were the two dreams I shared with you.
One I now have, will the other one ever dream remain.
For yours I truly wish to be."
Sample Output
for ever with dream
#include<iostream>
#include<cstdio>
#include<math.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int N=1e5+10;
const int mod=1e9+7;
int T,n;
char s[N];
map<string,int> M;
pair<int,string> g[N];
bool cmp(pair<int,string> a,pair<int,string> b)
{
if (a.first!=b.first) return a.first>b.first;
if (a.second.size()!=b.second.size()) return a.second.size()>b.second.size();
return a.second>b.second;
}
int main()
{
for (scanf("%d",&T);T--;)
{
scanf("%d",&n); getchar(); M.clear();
for (int i=1;i<=n;i++)
{
gets(s);
string g="";
for (int j=0;s[j];j++)
{
if (s[j]>='A'&&s[j]<='Z') s[j]+=32;
if (s[j]>='a'&&s[j]<='z') g+=s[j];
if (s[j]<'a'||s[j]>'z'||!s[j+1])
if (g!="") M[g]++,g="";
}
}
int sz=0;
for (map<string,int>::iterator it=M.begin();it!=M.end();it++)
{
if (it->second>1) g[sz++]=make_pair(it->second,it->first);
}
sort(g,g+sz,cmp);
int flag=0;
for (int i=0,j;i<sz;i=j)
{
if (flag) printf(" "); else flag=1;
for (j=i;g[i].first==g[j].first&&j<sz;j++);
if (j==i+1) cout<<g[i].second;
else if (g[i].second.size()==g[i+1].second.size())
{
cout<<g[i+1].second;
}
else cout<<g[i].second;
}
putchar(10);
}
return 0;
}