Ever Dream ZOJ - 3700【STL 模拟】

“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个句子,统计所有句子中不同单词出现的次数(出现次数为1的单词舍去),然后将出现次数相同的单词分为一组,输出该组最长的单词,如果有多个单词,按照字典序输出该组中最长单词中倒数第二个单词.

思路:用map统计出现的单词以及出现的次数,开一个vector,v[i][j]表示出现次数为i的单词有j个,分别是v[i][0]…v[i][j-1],然后对v[i]排序,找倒数第二个输出即可。

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
char s[500],temp[500];
vector<string> v[500];

bool cmp(string a,string b)
{
	if(a.size()==b.size())
		return a<b;
	else
		return a.size()<b.size();
}		
int main()
{
	int t,n,flag;
	cin>>t;
	while(t--)
	{
		cin>>n;
		gets(s);
		map<string,int> mp;
		string str;
		for(int i=1;i<=n;++i)
		{
			gets(s);
			int len=strlen(s);
			for(int j=0;j<len;++j)
				if(s[j]>='A'&&s[j]<='Z')
					s[j]+=32;
				else if(!(s[j]>='a'&&s[j]<='z'))
					s[j]='\0';
			for(int j=0;j<len;)
			{
				while(s[j]=='\0' && j<len)
					j++;
				if(j>=len)
					break;
				sscanf(s+j,"%s",temp);
				str=temp;
				if(str=="")
					j++;
				else 
				{
					mp[str]++;
					j+=str.length();
				}
			}
		}
		map<string, int>::iterator tt;
		for(tt=mp.begin();tt!=mp.end();tt++)
		{
			if(tt->second==1) continue;
			if(!tt->first.size()) continue;
			string q=tt->first;
			v[tt->second].push_back(q);
		}
		flag=0;
		for(int i=100;i>0;i--)
		{
			int l=v[i].size();
			if(!l) continue;
			sort(v[i].begin(),v[i].end(),cmp);
			if(l==1)
			{
				if(flag) cout<<" ";
				cout<<v[i][l-1];
			}
			else
			{
				if(flag) cout<<" ";
				if(v[i][l-1].size()==v[i][l-2].size())
					cout<<v[i][l-2];
				else
					cout<<v[i][l-1];
			}
			flag=1;
		}
		for(int i=0;i<=100;i++)
			v[i].clear();
		cout<<endl; 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值