"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.
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.
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.
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."
for ever with dream
Author: HUANG, Qiao
Contest: The 13th Zhejiang University Programming Contest
题意:
对于一篇文章 有如下处理 把所有单词统计出来 每个单词出现频率 注意单词的概念 只有有字符组成的才是单词 另外不分大小写
像“All All是单词 you. you也是单词 去掉边上的符号就是单词 也算是单词 对于I‘m 不是单词 中间有符号’
将出现频率相同的单词作为一组
对于每组 输出长度最长的单词,如果有多个单词都是最长的,那么输出字典序倒数第二个
输出的时候按频率从大到小
#include<stdio.h> #include<map> #include<set> #include<string> #include<string.h> #include<iostream> using namespace std; map<string,int>mp; map<string,int>::iterator it; set<string>st; set<string>::iterator ii; int mx,flag=0;//mx记录最大频率 char s[1000],ss[1000]; void solve() { int i,mmax=0,k; flag=0; for(i=mx;i>=2;i--)//暴力所有频率 { st.clear(); mmax=0; for(it=mp.begin();it!=mp.end();it++)//mp存了所有单词 其中也有一些不是单词 比如i'm { if(it->second==i)//如果频率等于i { for(k=0;k<it->first.size();k++)//判断是不是一个正确的单词 排除掉i'm you're等 if(it->first[k]>='a'&&it->first[k]<='z') continue; else break; if(k!=it->first.size()) continue; //确定是单词后 if(it->first.size()>mmax)//如果这个单词长度比目前发现的最大长度还要大 就要把st集合之前保存的全丢掉 { mmax=it->first.size(); st.clear(); st.insert(it->first); } else { if(it->first.size()==mmax)//如果是最大的就放进集合st中 st.insert(it->first); } } } if(st.size()==1) //如果就一个直接输出 { if(flag==0) { cout<<(*st.begin()) ; flag=1; } else { cout<<" "<<(*st.begin()) ; } } else if(st.size()>=2)//如果有多个输出字典序倒数第二个 { ii=st.end(); ii--; ii--; if(flag==0) { cout<<(*ii); flag=1; } else { cout<<" "<<(*ii); } } } } int is(char ch)//判断是否为字符 { if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) return 1; else return 0; } int main() { int cas,i,j; scanf("%d",&cas); while(cas--) { mp.clear(); mx=0; int n; scanf("%d",&n); getchar(); while(n--) { gets(s); int d=strlen(s); for(i=0;i<d;i++)//有符号(除了符号‘ )我们就将其变为空格 这样就不影响我们去取单词了 if(!is(s[i])) { if(s[i]!='\'') s[i]=' '; } d++;//多出一个格子放空格 对于下面找单词好找 方便 s[d-1]=' '; j=0; for(i=0;i<d;i++) { if(s[i]==' ') { ss[j]='\0'; if(j>=1)//这里是大于等于1 表示是至少存有一个字符 { mp[ss]++; if(mp[ss]>mx) mx=mp[ss]; } j=0; } else { if(s[i]<='Z'&&s[i]>='A')//如果是大写则换成小写 s[i]=s[i]+32; ss[j++]=s[i]; } } } solve(); printf("\n"); } return 0; }