"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 numbern (n < 50) indicating that there are n lines of words for the song. The followingn 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
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
Author: HUANG, Qiao
Contest: The 13th Zhejiang University Programming Contest
今天浙大比赛的一道题目,就是字符串处理,怕超时就用了,字典树,调代码,调了一晚上,注意当字符串出线的频率相同时,字符串的长度最大者,只有一个,就输出这一个。如果最大长度有多个符合,就将这些最大长度按字典序排序,输出时候,输出倒数第二个。
#include <stdio.h>
#include <string.h>
#include <math.h>
struct tire
{
int next[26];
int tag,pos;
}infor[100000];
struct num
{
char s1[150];
int occur;
}res[10000];
struct Num
{
char s1[150];
}trueres[10000];
int Top,pre[1000000],queue[1000000],tag;
char s1[10000];
char s2[10000];
int cmp(const void *e,const void *f)
{
struct num *p1,*p2;
int l1,l2;
p1=(struct num *)e;
p2=(struct num *)f;
if(p1->occur!=p2->occur)
{
return p2->occur-p1->occur;
}
l1=strlen(p1->s1);
l2=strlen(p2->s1);
if(l1!=l2)
{
return l2-l1;
}
if(strcmp(p1->s1,p2->s1)>0)
{
return 1;
}else
{
return -1;
}
}
int main()
{
int newnode();
void build(int p,char s1[10000]);
void bfs(int head);
int i,j,n,m,s,t,top,key,l,x,base;
int head;
scanf("%d",&t);
while(t--)
{
tag=0;
Top=0;
head=newnode();
Top++;
scanf("%d%*c",&n);
for(i=0;i<=n-1;i++)
{
gets(s1);
l=strlen(s1);
for(j=0,top=0;j<=l;j++)
{
if(((s1[j]>='A'&&s1[j]<='Z')||(s1[j]>='a'&&s1[j]<='z')))
{
if(s1[j]>='A'&&s1[j]<='Z')
{
s1[j]+=32;
}
s2[top++]=s1[j];
}else if(top>0)
{
s2[top]='\0';
build(head,s2);
top=0;
}
}
}
bfs(head);
qsort(res,tag,sizeof(res[0]),cmp);
key=0;
for(i=0;i<=tag-1;)
{
if(res[i].occur!=1)
{
base=0;
strcpy(trueres[base].s1,res[i].s1);
base++;
for(j=i+1;j<=tag-1;j++)
{
if((res[j].occur==res[i].occur)&&strlen(res[j].s1)==strlen(res[i].s1))
{
strcpy(trueres[base].s1,res[j].s1);
base++;
}else
{
break;
}
}
for(;j<=tag-1;j++)
{
if(res[i].occur!=res[j].occur)
{
break;
}
}
i=j;
if(base==1)
{
base=0;
}else
{
base=base-2;
}
if(key==0)
{
printf("%s",trueres[base].s1);
key=1;
}else
{
printf(" %s",trueres[base].s1);
}
}else
{
break;
}
}
printf("\n");
}
return 0;
}
int newnode()
{
int i,j;
for(i=0;i<=25;i++)
{
infor[Top].next[i]=-1;
}
infor[Top].pos=Top;
infor[Top].tag=0;
return Top;
}
void build(int p,char s1[10000])
{
int l=strlen(s1),k,i,x;
for(i=0;i<=l-1;i++)
{
k=s1[i]-'a';
if(infor[p].next[k]==-1)
{
infor[p].next[k]=newnode();
Top++;
}
p=infor[p].next[k];
}
infor[p].tag+=1;
}
void bfs(int p)
{
int i,j,base,top,x,u,v;
char temp[10000],temp_top;
base=top=0;
pre[0]=-1;
queue[top++]=0;
while(base<top)
{
x=queue[base++];
if(infor[x].tag!=0)
{
temp_top=0;
for(i=x;pre[i]!=-1;i=pre[i])
{
j=pre[i];
for(v=0;v<=25;v++)
{
if(infor[j].next[v]==i)
{
temp[temp_top++]='a'+v;
break;
}
}
}
temp[temp_top]='\0';
for(v=temp_top-1,u=0;v>=0;v--,u++)
{
res[tag].s1[u]=temp[v];
}
res[tag].s1[u]='\0';
res[tag++].occur=infor[x].tag;
}
for(i=0;i<=25;i++)
{
if(infor[x].next[i]!=-1)
{
pre[infor[x].next[i]]=x;
queue[top++]=infor[x].next[i];
}
}
}
}