zoukankan      html  css  js  c++  java
  • 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.
    ·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.

    样例输入

    【样例17
    beat
    vet
    beet
    bed
    vet
    bet
    beet
    【样例28
    buffalo
    buffalo
    buffalo
    buffalo
    buffalo
    buffalo
    buffalo
    buffalo
    【样例37
    bass
    bass
    kick
    kick
    bass
    kick
    kick
    【样例44
    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.

    题意比较简单,就是输出出现次数较多的字符串,如果有多个,按照字典序来解决
    下面附上本蒟蒻的收获
    在这里插入图片描述
    当时清晰地记得这个题在原网站上做过,并且能够顺利通过,当把代码重构再次写出来时,在原网站能够正确通过,但是在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
    ",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
    ",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("
    ");
            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);
    

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

  • 相关阅读:
    《JFlow: Practical Mostly-Static Information Flow Control》
    《嵌入式Linux C编程》第一章笔记
    Ansible --- 通过Ansible管理地区机房中的内网机器
    等保审核 --- MySQL密码复杂度
    等保审核 --- MySQL连接控制插件
    等保审核 --- MySQL操作审计记录
    CSS中居中的完全指南(中英对照翻译)
    svn提交报database is locked
    PHP session_cache_expire 会话函数
    MySQL CONCAT_WS 函数
  • 原文地址:https://www.cnblogs.com/PushyTao/p/13144199.html
Copyright © 2011-2022 走看看