Ever Dream ZOJ - 3700

题目

"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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值