Poll——map+字符串数组

简介: 题目描述We have N voting papers. The i-th vote (1≤i≤N) has the string Si written on it.Print all strings that are written on the most number of votes, in lexicographical order.Constraints·1≤N≤2×105·Si (1≤i≤N) are strings consisting of lowercase English letters.

题目描述


We have N voting papers. The i-th vote (1≤i≤N) has the string Si written on it.

Print all strings that are written on the most number of votes, in lexicographical order.


Constraints

·1≤N≤2×105

·Si (1≤i≤N) are strings consisting of lowercase English letters.

·The length of Si (1≤i≤N) is between 1 and 10 (inclusive).


输入


Input is given from Standard Input in the following format:
N
S1
:
SN


输出


Print all strings in question in lexicographical order.


样例输入


【样例1】
7
beat
vet
beet
bed
vet
bet
beet
【样例2】
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
【样例3】
7
bass
bass
kick
kick
bass
kick
kick
【样例4】
4
ushi
tapu
nichia
kun


样例输出


【样例1】
beet
vet
【样例2】
buffalo
【样例3】
kick
【样例4】
kun
nichia
tapu
ushi


提示


样例1解释

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.


题意比较简单,就是输出出现次数较多的字符串,如果有多个,按照字典序来解决

下面附上本蒟蒻的收获


微信图片_20220528174646.png


当时清晰地记得这个题在原网站上做过,并且能够顺利通过,当把代码重构再次写出来时,在原网站能够正确通过,但是在UPCoj上却无法通过,后来知道这个题卡了cin cout,根据同学介绍,不得不换为字符串数组来解决,并能顺利卡过


#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
int num[maxn];
map<string,ll>mp;
map<string,ll>::iterator it;
string ss[maxn];
int main()
{
        /**
        int n=read;
    string ss;
    map<string,int>mp;
    for(int i=0;i<n;i++)
    {
        ///scanf("%s",ss);
        cin>>ss;
        mp[ss]++;
    }
    int cnt=0;
    for(auto& v:mp) cnt=max(cnt,v.second);
    for(auto& v:mp){
        if(v.second==cnt)
            ///cout<<v.first<<endl;
            printf("%s\n",v.first);
    }
    ll n;
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ///scanf("%s",ss);
        ///getchar();
        string ss;
        cin>>ss;
        mp[ss]++;
    }
    int maxx=-1;
    for(const auto& x : mp){
        int temp=x.second;
        if(temp>maxx) maxx=temp;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        if(it->second==maxx)
            ///cout<<it->first<<endl;
            printf("%s\n",it->first);
    }
    int n;
    cin>>n;
    int maxx=0;
    for(int i=1;i<=n;i++)
    {
        string s;
        ///scanf("%s",&s);
        cin>>s;
        mp[s]++;
        maxx=max(maxx,mp[s]);
    }
    for(auto x:mp)
        if(x.second==maxx)
            cout<<x.first<<endl;**/
    ll n=read;
    ll maxx=0;
    for(ll i=1;i<=n;i++){
        cin>>ss[i];
        mp[ss[i]]++;
    }
    for(it=mp.begin();it!=mp.end();it++)
        maxx=max(maxx,it->second);
    sort(ss+1,ss+1+n);
    for(ll i=1;i<=n;i++){
        if(mp[ss[i]]!=maxx) continue;
        ll len=ss[i].length();
        for(ll j=0;j<len;j++)
            putchar(ss[i][j]);
        printf("\n");
        mp[ss[i]]=0;
    }
    return 0;
}
/**ll kruskal(){
    sort(num,num+m,cmp);
    for(ll i=1;i<=n;i++) num2[i]=i;
    ll res=0,cnt=0;
    for(ll i=0;i<m;i++){
        ll aa=num[i].a,b=num[i].b,w=num[i].w;
        aa=searchnum(aa),b=searchnum(b);
        if(aa!=b){
            num2[aa]=b;
            res+=w;
            cnt++;
        }
    }
    if(cnt<n-1) return inf;
    else return res;
}**/
/**************************************************************
    Language: C++
    Result: 正确
    Time:659 ms
    Memory:52768 kb
****************************************************************/


在前面已经附上了若干版本

但是卡不过去

当时还发生了玄学问题:

加上这句之后,不能AC


    ios_base::sync_with_stdio(false);


知道的同志们可以在下方评论告诉本蒟蒻

目录
相关文章
|
2月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
4月前
|
C++
c++ set、map的四种自定义排序方法
c++ set、map的四种自定义排序方法
85 0
|
4月前
使用Lamda表达式、stream流遍历Map、list
使用Lamda表达式、stream流遍历Map、list
|
8月前
Stream方法使用-peek和foreach方法讲解
Stream方法使用-peek和foreach方法讲解
86 0
|
10月前
List,Map 三种遍历方式:(总结理解)
List,Map 三种遍历方式:(总结理解)
43 0
|
11月前
|
存储 Dart 数据处理
Dart中常用的集合类型List、Set、Map、Queue
Dart中常用的集合类型List、Set、Map、Queue
Collection.stream()forEach()和Collection.forEach()有什么区别?
Collection.stream()forEach()和Collection.forEach()有什么区别?
|
存储 Java
从数组当做map的key引发的思考
从数组当做map的key引发的思考
117 0
|
数据库 容器
使用Stream流对集合排序
有一些业务需要对集合按照一些规则进行排序,本篇介绍如何用Stream()对集合进行升序或者降序操作。之前只有接触过使用数据库进行排序,有一些情况需要不同的排序结果,如果进行多次查询会多次操作数据库,所以有些地方使用流对集合操作是更好的选择。Stream 流 -> 调用 sorted 方法 -> 方法内传入对比规则,用容器对象的属性作为入参作为排序依据,默认升序,需要倒叙的话后面调用.reversed() 方法。
575 0
使用Stream流对集合排序