ZOJ3700-Ever Dream

来源:互联网 发布:单片机和嵌入式 编辑:程序博客网 时间:2024/05/21 19:23

Ever Dream

Time Limit: 2 Seconds      Memory Limit: 65536 KB



"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 lines contain the lyric of the song. An empty line is also counted as a single line. Any ASCII code can occur in the lyric. There will be at most 100 characters in a single line.

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

129Ever 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

Author: HUANG, Qiao
Contest: The 13th Zhejiang University Programming Contest


题意:给出一首歌,由n行字符串字符串,每行含有一些单词,求出这些单词里面的关键词。
关键词由以下规则生成:1.提取出所有不同的单词和每个单词出现的次数,单词只有英文字母组成,并且不区分大小写。
2.把这些单词分成若干组,出现相同次数的单词分成一个组
3.对于没一个组,输出长度最长的单词;如果有多个单词长度相同,则先按字典序排序,然后输出倒数第二个。


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;char ch[1000009],s[1000009];bool cmp(string a,string b){    if(a.size()==b.size())return a<b;    else return a.size()<b.size();}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        getchar();        map<string,int>mp;        while(n--)        {            gets(ch);            int len=strlen(ch);            ch[len++]=' ',ch[len]='\0';            int k=0;            for(int i=0;i<len;i++)            {                if(ch[i]>='a'&&ch[i]<='z') s[k++]=ch[i];                else if(ch[i]>='A'&&ch[i]<='Z') s[k++]='a'+ch[i]-'A';                else                {                    s[k]='\0';                    if(k) mp[s]++;                    k=0;                }            }        }        map<string,int>::iterator it=mp.begin();        vector<string> v[120];        for(; it!=mp.end(); it++)        {            string s=it->first;            if(it->second==1)continue;            v[it->second].push_back(s);        }        bool flag=0;        for(int i=100; i>0; i--)        {            int sz=v[i].size();            if(!sz)continue;            sort(v[i].begin(),v[i].end(),cmp);            if(sz==1){if(flag)cout<<" ";cout<<*(v[i].end()-1);}            else            {                if(flag)cout<<" ";                if(v[i][sz-1].size()==v[i][sz-2].size())cout<<v[i][sz-2];                else cout<<v[i][sz-1];            }            flag=1;        }        cout<<endl;    }    return 0;}

0 0
原创粉丝点击