题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3700 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
题意:给出一首歌,由n行字符串字符串,每行含有一些单词,求出这些单词里面的关键词。
关键词由以下规则生成:1.提取出所有不同的单词和每个单词出现的次数,单词只有英文字母组成,并且不区分大小写。
2.把这些单词分成若干组,出现相同次数的单词分成一个组
3.对于没一个组,输出长度最长的单词;如果有多个单词长度相同,则先按字典序排序,然后输出倒数第二个。
注意:像 I'd 这样的简写认为是一个单词,和题意说的有点不一样,一直WA在这里。
知道了这些,就可以AC了。
#include <iostream>
#include <set>
#include <map>
#include <string>
using namespace std;inline bool Is_alpha(char ch) { // 判断字符是不是字母if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))return true;return false;
}map <string, int> mp;
map <string, int>::iterator it;
set <string> ans;int main() {int T, n;string s;cin >> T;while(T--) {cin >> n;cin.ignore();mp.clear();int max_cnt = -1;for(int i = 0; i < n; i++) {getline(cin, s);if(s == "") continue;string str = "";for(int j = 0; j < (int)s.size(); j++) {if(!Is_alpha(s[j])) {if(s[j] != '\'') {  // 如果不是单引号,I'd 看做一个单词if(str.length() > 0) {if(mp[str] == 0) mp[str] = 1;else mp[str]++;max_cnt = max(max_cnt, mp[str]);str = "";}}else str += s[j];}else {if(s[j] >= 'A' && s[j] <= 'Z')s[j] = (s[j] + 32);str += s[j];}}if(str.length() > 0) {if(mp[str] == 0) mp[str] = 1;else mp[str]++;max_cnt = max(max_cnt, mp[str]);str = "";}}int flag = 0;for(int i = max_cnt; i >= 2; i--) {ans.clear();int max_len = 0;for(it = mp.begin(); it != mp.end(); it++) {if(it->second == i) { // 找出长度为i的最长字符串if((it->first.length()) > max_len) {max_len = it->first.length();ans.clear();ans.insert(it->first);}else if(it->first.length() == max_len)ans.insert(it->first);}}if(ans.size() == 1) {if(flag) cout << " ";cout << *ans.begin();flag = 1;}else {if(ans.size() >= 2) {set <string> ::iterator sit = ans.end();sit--;sit--;if(flag) cout << " ";cout << *sit;flag = 1;}}}cout << endl;}return 0;
}
												

ZOJ 3700 Ever Dream(模拟)相关推荐

  1. ZOJ 3879(大模拟)

    传送门 题面:Capture the Flag Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In comp ...

  2. 【ZOJ - 3211】Dream City (01背包类问题,贪心背包)

    题干: JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the y ...

  3. ZOJ 3826 Hierarchical Notation 模拟

    模拟: 语法的分析 hash一切Key建设规划,对于记录在几个地点的每个节点原始的字符串开始输出. . .. 对每一个询问沿图走就能够了. .. . Hierarchical Notation Tim ...

  4. HDU 1984 ZOJ 2987 Misspelling(模拟)

    题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1984 ZOJ:http://acm.zju.edu.cn/onlinejudge/showP ...

  5. zoj 3327 Friend Number 模拟题

    一年的省赛题目. 先容我骂一下,这题目有病吧,我调试了一个下午+晚上,好不容易才顺利A了,真是坑. 比赛中能做出来的那绝对是厉害啊!!. 我的思路,方法可能有笨: 1.首先判断下有几个0,有一个0的话 ...

  6. zoj 1851 Code Formatter 模拟

    题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=2851 题意:给出一段代码,统计'\t'和后缀空格的个数,有一个地方要 ...

  7. 模拟手机通讯录管理 2021.2.11

    模拟手机通讯录管理 现在手机都有通讯录程序,下面这个代码模拟手机通讯录设置添加,查询,删除联系人等功能. 今天是除夕,祝各位程序员朋友们新春快乐,阖家欢乐! import os import sysp ...

  8. ZOJ 3703,3700,3699

    经典背包问题,处理好限制条件的优先级 排序是为了让相同val,相同问题数量以及选择的时候,从ti更小的问题坐起,总的penalty会最少 #include <iostream> #incl ...

  9. 模拟 ZOJ 3878 Convert QWERTY to Dvorak

    题目传送门 1 /* 2 模拟:手敲map一一映射,累! 3 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) 4 */ 5 #include <cstdio> 6 ...

最新文章

  1. js:深入prototype(下:原型重写)
  2. Windbg Extension NetExt 使用指南 【2】 ---- NetExt 的基本命令介绍
  3. 实战|记一次绕过宝塔防火墙的BC站渗透
  4. 女生学高铁和计算机哪个更好,2020铁路最好的5个专业 女生上铁路学什么专业好...
  5. 摸鱼也要讲究方法:工作学习中玩手机并不能缓解无聊和疲劳
  6. php点赞,php如何实现点赞
  7. 电梯设计需求调研报告
  8. IDC:第三季度企业WLAN市场增长强劲
  9. aspcms首页content内容html代码过滤,ASPCMS用百度编辑器JS/html代码(script标签)被过滤的解决办法...
  10. WebSocket 是什么原理?为什么可以实现持久连接?
  11. [附源码]java毕业设计网吧购物系统
  12. 怎么做新闻软文推广?故事性新闻稿写作技巧_云媒易
  13. TIA博途中如何为PLC分配IP地址?
  14. 百度短网址 php,调用百度短网址API生成短网址
  15. android11.0 Launcher3 高端定制之抽屉列表固定APP显示位置
  16. 题解 修改字符串 DDP基础题
  17. 2021年中式烹调师(中级)考试题库及中式烹调师(中级)操作证考试
  18. T48566 【zzy】yyy点餐
  19. Python - 深度学习训练过程使用matplotlib.pyplot实时动态显示loss和acc曲线
  20. C++ primer笔记

热门文章

  1. .net Int16 、(int Int32)、 Int64 的区别
  2. 用vue.js写的一个瀑布流的组件
  3. Gartner:移动设备在工作场所中的使用尚未成熟
  4. 如何书写高质量的jQuery代码
  5. JSTokenField
  6. java虚拟机及加载class文件的原理机制
  7. 介绍 Java 平台的 Jazzy:一种新的拼写检查器 API
  8. building a software for what?
  9. bike with a famous logo
  10. ubuntu-10.04硬盘安装挫折略记