ZOJ 3700 Ever Dream(Vector)

做了这道题,算是会vector了。

/*
Problem :
Status  :
By wf,
*/

#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int t;
int n;

map<string,int>mp;
vector<string>v[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    cin>>t;
    while(t--)
    {
        mp.clear();


        for(int i=0;i<maxn;++i)v[i].clear();

        cin>>n;
        getchar();
        string str;
        for(int i=0; i<n; ++i)
        {
            getline(cin,str);
            //cout<<str<<endl;

            int len = str.length();
            for(int i=0; i<len;)
            {
                if( isalpha(str[i]) )
                {
                    str[i]=tolower(str[i]);
                    int j = i + 1;
                    while(j<len && isalpha(str[j]))
                    {
                        str[j]=tolower(str[j]);
                        j++;
                    }
                    string word = str.substr(i,j-i);
                    mp[word]++;
                    //cout<<word<<" "<<endl;
                    i=j;
                }
                else i++;
            }
        }
        map<string,int>::iterator it;
        for(it=mp.begin(); it!=mp.end(); ++it)
        {
            //cout<< it->first <<endl;

            if(it->second==1)continue;

            if( v[ it->second ].empty() )
            {
                 v[ it->second ].push_back( it->first );
            }
            else
            {
                int len1 = it->first.length();
                int len2 = v[ it->second ].front().length();
                if( len1 > len2 )
                {
                    v[ it->second ].clear();
                    v[ it->second ].push_back( it->first );
                }
                else if( len1==len2 )
                {
                    v[ it->second ].push_back( it->first );
                }
            }

        }
        bool first=1;
        for(int i=maxn-1;i>=2;--i)
        {
            if( v[i].empty() )continue;
            int num = v[i].size();
            if( num==1 )
            {
                if(first)first=0;
                else cout<<" ";

                cout<<v[i].front();
                //cout<<" "<<mp[  v[i].front()   ]<<endl;
            }
            else
            {
                sort(v[i].begin(),v[i].end());

                if(first)first=0;
                else cout<<" ";

                v[i].pop_back();
                cout<<v[i].back();

                //cout<<" "<<mp[ v[i].back()  ]<<endl;
            }

            //debug
            /*
            if( i==2 )
            {
                vector<string>::iterator it2;
                it2 = v[i].begin();
                while( it2!=v[i].end() )
                {
                    cout<<"2:"<< *it2 <<endl;
                    ++it2;
                }
            }
            */
        }
        cout<<endl;

    }

    return 0;
}

  

转载于:https://www.cnblogs.com/bruce27/p/5487206.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值