zoj 3700 模拟+STL vector

Ever Dream

Time Limit: 2 Seconds      Memory Limit: 65536 KB



"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行,统计单词个数,然后按出现频率大小输出频率大于一的单词,若多个单词单词频率一样,输出最长的单词,若有多个单词长度一样,输出字典序第二大的单词。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
string str;
vector<string>namelist;
struct node{
   string name;
   int cnt;
   int len;
};
node kind[1000010];
bool cmp(node a,node b){
    if(a.cnt==b.cnt){
        if(a.len==b.len) return a.name>b.name;
        else return a.len>b.len;
    }
    return a.cnt>b.cnt;
}
int main()
{

//     freopen("in.in","r",stdin);
     int T;
     scanf("%d",&T);getchar();
     while(T--)
     {
         namelist.clear();
         int n;
         scanf("%d",&n);getchar();
         while(n--)
         {
            getline(cin,s);
            str.clear();
            for(int i=0;s[i]!='\0';i++){
                if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]-'A'+'a';
                if(s[i]>='a'&&s[i]<='z'){
                   str.push_back(s[i]);
                }else{
                    if(str.size()) namelist.push_back(str);
                    str.clear();
                }
            }
            if(str.size()) namelist.push_back(str);
         }
         sort(namelist.begin(),namelist.end());
         string tp=namelist[0];
         int cnt=0;
         int st=0;
         for(int i=0;i<namelist.size();i++)
         {
             if(namelist[i]==tp) cnt++;
             else{
                 if(cnt>1){
                     kind[st].name=tp;
                     kind[st].cnt=cnt;
                     kind[st++].len=tp.size();
                 }
                 tp=namelist[i];
                 cnt=1;
             }
         }
          if(cnt>1){
             kind[st].name=tp;
             kind[st].cnt=cnt;
             kind[st++].len=tp.size();
          }
        sort(kind,kind+st,cmp);
        int ok=0;
        for(int i=0;i<st;i++){
            if(i!=st-1&&kind[i].cnt==kind[i+1].cnt&&kind[i].len==kind[i+1].len) i++;
            int j=i+1;
            while(j<st&&kind[j].cnt==kind[i].cnt) j++;
            if(ok) printf(" ");
            cout<<kind[i].name;
            ok=1;
            i=j-1;
        }
        printf("\n");
     }
     return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值