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;
}

  

时间: 2024-10-27 09:33:12

ZOJ 3700 Ever Dream(Vector)的相关文章

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

ZOJ 3204 Connect them (C) 最小生成树kruskal

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the sa

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

ZOJ 3207 80ers&#39; Memory (F)

80ers' Memory Time Limit: 1 Second      Memory Limit: 32768 KB I guess most of us are so called 80ers, which means that we were born in the 1980's. This group of people shared a lot of common memories. For example, the Saint Seiya, the YoYo ball, the

R语言编程艺术# 数据类型向量(vector)

R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用typeof(x)函数查询. > #插入向量元素 > x <- c(88,5,12,13) > x [1] 88 5 12 13 > x <- c(x[1:3],168,x[4]) #插入168数字在13之前 > x [1] 88 5 12 168 13 > 2.

STL--向量(vector)

STL的组成 标准模板库STL关注的重点是泛型数据结构和算法,其关键组成部分是容器(containers).算法(algorithms).迭代器(iterators).函数对象(Function Object).适配(Adaptor). 本文地址:http://www.cnblogs.com/archimedes/p/Cpp-STL-Vector.html,转载请注明源地址. 容器(containers):容器是数据在内存中组织的方法,例如,数组.堆栈.队列.链表或二叉树(不过这些都不是STL标

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

ZOJ 2477 Magic&#160;Cube(魔方)

ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 * 3 small cubes. We can unwrap the cube, it will become like this: 这是个有名的儿童游戏.游戏

STL源码剖析(4):容器(vector)

容器是很多人对STL的第一印象,vector,stack,queue,set,map等等都是容器. 这里先介绍 STL中的序列式容器. 所谓序列式容器,其中的元素可序(ordered),但未必有序(sorted).C++ 本身提供了一个序列式容器--数组(array),STL中还提供了向量(vector),链表(list),堆栈(stack),队列(queue),优先队列(priority queue)等,其中stack和queue只是将deque(双端队列)设限而得到的,技术上可以被归为一种配