Ever Dream ZOJ - 3700

本文通过一首Nightwish的EverDream,展示了如何使用算法解析歌词,提取关键词,揭示歌曲主题。通过计数不同单词的出现频率,将其分组,并找出最长且出现次数最多的单词,最终输出这些关键词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

"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

题意

T组输入,每组给定一个n,表示有n行字符串,接下来n行,每行一行字符串。将每个单词按照出现的次数,相同的分在一组,先按长度从大到小排序,长度相同时,按字典序排序。输出时,在出现次数大于1的组中,输出最长的单词,如果有多个最长的单词,那么输出最长的单词中按字典序排序,排在倒数第二的单词。

分析

用map记录每个单词出现的次数,用vector保存每种出现次数的最长的单词。最后输出时,vector的size等于1的,直接输出该单词,大于等于1的,按字典序排序,输出倒数第二个单词。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
const int N=1e4+5;
map<string,int> mp;
vector<string> v[N];
bool check(char c)//检查是否为字母 
{
	if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
		return true;
	return false;
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		mp.clear();
		for(int i=0;i<N;i++)
			v[i].clear();
		cin>>n;
		getchar();
		string str;
		for(int i=0;i<n;i++)
		{
			getline(cin,str);//不能写成cin>>str 
			int len=str.length();
			for(int k=0;k<len;)
			{
				if(check(str[k]))
				{
					str[k]=tolower(str[k]);
					int j=k+1;
					while(j<len&&check(str[j]))
					{
						str[j]=tolower(str[j]);
						j++;
					}
					string word=str.substr(k,j-k);
					mp[word]++;
					k=j;
				}
				else
					k++;
			}
		}
		map<string,int>::iterator it;
		for(it=mp.begin();it!=mp.end();it++)
		{
			if(it->second==1)//某个单词只出现了一次,忽略 
				continue;
			if(v[it->second].empty())//为空,直接将单词放入vector 
				v[it->second].push_back(it->first);
			else//不为空 
			{
				int len1=it->first.length();
				int len2=v[it->second].front().length();
				if(len1>len2)//新单词比已有的最长单词长 
				{
					v[it->second].clear();
					v[it->second].push_back(it->first);
				}
				else if(len1==len2)//相等 
				{
					v[it->second].push_back(it->first);
				}
			}
		}
		int flag=0;
		for(int i=N-1;i>=2;i--)
		{
			if(v[i].empty())
				continue;
			int num=v[i].size();
			if(num==1)//只有一个单词,直接输出 
			{
				if(!flag)
				{
					flag=1;
					cout<<v[i].front();
				}
				else
				{
					cout<<" "<<v[i].front();
				}
			}
			else
			{
				sort(v[i].begin(),v[i].end());//按字典序排序 
				v[i].pop_back();//弹出倒数第一个 
				if(!flag)
				{
					flag=1;
					cout<<v[i].back();
				}
				else
				{
					cout<<" "<<v[i].back();//输出倒数第二个 
				}
			}
		}
		cout<<endl;
	}
	return 0;
}

 

分数阶傅里叶变换(Fractional Fourier Transform, FRFT)是对传统傅里叶变换的拓展,它通过非整数阶的变换方式,能够更有效地处理非线性信号以及涉及时频局部化的问题。在信号处理领域,FRFT尤其适用于分析非平稳信号,例如在雷达、声纳和通信系统中,对线性调频(Linear Frequency Modulation, LFM)信号的分析具有显著优势。LFM信号是一种频率随时间线性变化的信号,因其具有宽频带和良好的时频分辨率,被广泛应用于雷达和通信系统。FRFT能够更精准地捕捉LFM信号的时间和频率信息,相比普通傅里叶变换,其性能更为出色。 MATLAB是一种强大的数值计算和科学计算工具,拥有丰富的函数库和用户友好的界面。在MATLAB中实现FRFT,通常需要编写自定义函数或利用信号处理工具箱中的相关函数。例如,一个名为“frft”的文件可能是用于执行分数阶傅里叶变换的MATLAB脚本或函数,并展示其在信号处理中的应用。FRFT的正确性验证通常通过对比变换前后信号的特性来完成,比如评估信号的重构质量、信噪比等。具体而言,可以通过计算原始信号与经过FRFT处理后的信号之间的相似度,或者对比LFM信号的关键参数(如初始频率、扫频率和持续时间)是否在变换后得到准确恢复。 在MATLAB代码实现中,通常包含以下步骤:首先,生成LFM信号模型,设定其初始频率、扫频率、持续时间和采样率等参数;其次,利用自定义的frft函数对LFM信号进行分数阶傅里叶变换;接着,使用MATLAB的可视化工具(如plot或imagesc)展示原始信号的时域和频域表示,以及FRFT后的结果,以便直观对比;最后,通过计算均方误差、峰值信噪比等指标来评估FRFT的性能。深入理解FRFT的数学原理并结合MATLAB编程技巧,可以实现对LFM信号的有效分析和处理。这个代码示例不仅展示了理论知识在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值