题目描述:
“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:
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
思路:
输入用getline( )
map 记住string存放的位置
后将string按出现次数存放在vector中
细节见代码
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
typedef long long ll;
using namespace std;//此题的 I'd 可以看成一个单词 I'd,也可以看成含有俩单词 I 和 d ,这不是坑点
map<string,int>mp;
string s;
char ss[110];//j
int a[10000];//
string str[10000];//cnt
string sst[10000];//cn
vector<string>vec[100];
int zm(char c) //标点符号当作空格处理,为方便'也当作空格处理
{
if(c>='a'&&c<='z')
return 1;
if(c>='A'&&c<='Z')
return 2;
return 0;
}
int main()
{
int T,n;
scanf("%d",&T);
string st;
while(T--)
{
scanf("%d",&n);
getchar();
int cnt=1;
for(int ii=0; ii<n; ii++)
{
getline(cin,s);// 一行一行的输入
int len=s.size();
int i=0;
while(i<len)
{
if(zm(s[i]))//识别出这是一个单词的开始
{
int j=0;
while(zm(s[i]))
{
if(zm(s[i])==2)
s[i]+='a'-'A';
ss[j]=s[i];//这个单词暂时存放在ss中
j++;
i++;
}
ss[j]=0;
st=ss;//将ss字符数组转化成string类放在st中
if(mp[st]==0)//st是一个全新的字符串,st存在a【cnt】中,用map记录下st存放的位置
{
mp[st]=cnt;
str[cnt]=st;
a[cnt]=1;
cnt++;
}
else//st已经存在,出现次数++
{
a[mp[st]]++;
}
}
else
i++;
}
}
if(cnt==1)//如果样例中一个单词都没有,输出空行,处理下一个样例
{
cout<<endl;
continue;
}
int mx=0;//出现次数的最大值
int si=0;
for(int h=1; h<cnt; h++)//按出现的次数将st归类存放在vec中
{
if(a[h]>mx)
mx=a[h];
vec[a[h]].push_back(str[h]);
}
if(mx==1)//如果出现的最大值是 ,按照题意,输出空行,处理下一个样例,注清空vec和mp,否则会WA
{
cout<<endl;
for(int h=1; h<=mx; h++)
vec[h].clear();
mp.clear();
continue;
}
int minx=mx;//minx是大于1,最小的出现次数,方便规范输出格式
for(int h=1; h<cnt; h++)
{
if(a[h]>1&&a[h]<minx)
minx=a[h];
}
for(int h=mx; h>=minx; h--)
{
if(!vec[h].empty())
{
int l=0,li=0;//l是vec【h】中st最长的长度,li是长度为l的st的个数
string outs;
for(int q=0; q<vec[h].size(); q++) //求vec【h】中最长的长度
{
if(vec[h][q].size()>l)
{
l=vec[h][q].size();
li=1;
outs=vec[h][q];
}
else if(vec[h][q].size()==l)
li++;
}
if(li==1)//长度最长的只有一个,直接输出
{
if(h>minx)//输出的最后一个单词后无空格
cout<<outs<<' ';
else
cout<<outs<<endl;
}
else//长度最长的不唯一,按题意sort
{
int cn=0;
for(int q=0; q<vec[h].size(); q++)
{
if(vec[h][q].size()==l)
sst[cn++]=vec[h][q];
}
sort(sst,sst+cn);
if(h>minx)//输出的最后一个单词后无空格
cout<<sst[cn-2]<<' ';
else
cout<<sst[cn-2]<<endl;
}
}
}
for(int h=1; h<=mx; h++)//注意清空vec和mp,否则会WA
vec[h].clear();
mp.clear();
}
}
本文通过一首名为EverDream的歌曲,探讨了其歌词背后的情感故事,并设计了一个算法来提炼歌曲的主题关键词。算法分为三个步骤:提取并计数不同单词、按频率分组以及输出最长单词。该文不仅推荐了这首动人的歌曲,还分享了一种用算法表达歌曲意义的方法。
1811

被折叠的 条评论
为什么被折叠?



