ZOJ 3700 Ever Dream(模拟)

题目链接: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;
}


				
时间: 2024-10-09 22:37:57

ZOJ 3700 Ever Dream(模拟)的相关文章

ZOJ 3700 Ever Dream(Vector)

做了这道题,算是会vector了. /* Problem : Status : By wf, */ #include "algorithm" #include "iostream" #include "cstring" #include "cstdio" #include "string" #include "stack" #include "cmath" #incl

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo

ZOJ How Many Nines 模拟 | 打表

How Many Nines Time Limit: 1 Second      Memory Limit: 65536 KB If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s will appear in all the dates between Y1-M1-D1 and Y2-M2-D2 (both inclusive)? Note that

zoj 3826 Hierarchical Notation(模拟)

题目链接:zoj 3826 Hierarchical Notation 题目大意:给定一些结构体.结构体有value值和key值,Q次询问,输出每一个key值相应的value值. 解题思路:思路非常easy.写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了. 这题是最后10分钟出的.由于没有考虑value为{}的情况,导致RE了.可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE.只是另一个地方会

ZOJ 1760 &amp;&amp;POJ1552 Doubles (模拟)

链接:click here 题意:叫你求一个数是另一个数的二倍的这样的组合有多少个. 思路:纯模拟,一重循环:读入当前数据组a,并累积数据元素个数n,循环的结束标志是读入数据0,两重循环结构枚举组内所有数据对a[i] a[j] 判断是否成两倍关系 代码: #include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <algorithm>

ZOJ 3180 Number Game(模拟,倒推)

题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h> #include<string.h> #include <algorithm> using namespace std; int t,a[5],b[5]; int main() { scanf("%d",&t); while(t--) { scanf(

ZOJ 3778 Talented Chef 模拟 [ 祝愿明天省赛一帆风顺, ZJSU_Bloom WILL WIN : )

这题的意思是给你 n 道菜,第 i 道菜需要 Ai 步才能完成 每次你能对 m 道菜分别完成一步,请问最少需要几次? 这题暴力写肯定是不行的,去年省赛的时候就是没写出来这题,今天再把思路理一理吧. 首先我们需要明确的是 1. n <= m 的时候, 那么答案显而易见,就是 Max (A[i]) 2. n > m ①此时我们不难发现这个现象,如果 sum (A[i]) 能被 m 整除,那么我们可能恰恰只需要 (sum / m) 次即可完成 一个例子: n = 3, m = 2 a[1] = 1,

ZOJ - 3829 Known Notation(模拟+贪心)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字,还可以交换任意两个字符,问需要多少步能得到一个合法后缀表达式. 如果数字<星号+1 那么必须要添加数字,那么肯定是添加在字符串前面是最优的,然后从头到尾扫描整个串,如果遇到星号并且前面出现的数字>=2 数字减1,否则就要从后往前找第一个非*的数字然后与星号交换. 注意全为数字的情况,和处理完后字