简单容器应用

Andy's First Dictionary 

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful. You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like “Apple”, “apple” or “APPLE” must be considered the same.
Input
The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.
Output
Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.
Sample Input
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left."
So they went home.
Sample Output
a

adventures

blondes

came

disneyland

fork

going

home

in

left

read

road

sign

so

the

they

to

two

went

were

when

WA了好久,看了书才解决

#include<iostream>
#include<set>
#include<cstring>
#include<sstream>
using namespace std;
set<string>s;
int main()
{
    string a,b;
    while(cin>>a)
    {
        for(int i=0;i<a.length();i++)
            if(isalpha(a[i]))
                a[i]=tolower(a[i]);
            else a[i]=' ';
            stringstream ss(a);
            while(ss >> b) s.insert(b);
    }
        for(set<string>::iterator it = s.begin();it!=s.end();++it)
        {
            cout<<*it<<endl;
        }
    return 0;
}

Let the Balloon Rise

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
InputInput contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
OutputFor each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
int main()
{
    map<string,int>m;
    int n;
    while(cin>>n)
    {
        if(n==0) break;
        string s,ss;
        int i,maxn=0;
        for(i=0;i<n;i++)
        {
            cin>>s;
            ++m[s];
            if(m[s]>maxn)
            {
                ss=s;
                maxn=m[s];
            }
        }
        cout<<ss<<endl;
        m.clear();
    }
    return 0;
}

What Are You Talking About

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
InputThe problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab(' '), enter(' ') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
OutputIn this problem, you have to output the translation of the history book.
Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END

Sample Output

hello, i'm from mars.
i like earth!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
using namespace std;
string a,b;
map<string,string>m;
int main()
{
    int i;
    string a,b;
    cin>>a;
    while(cin>>a && a!="END")
    {
        cin>>b;
        m[b]=a;
    }
    cin>>a;
    getchar();
    char ss[5000];
    while(1)
    {
        gets(ss);
        if(strcmp(ss,"END")==0) break;
        b="";
        for(i=0;i<strlen(ss);i++)
        {
            if(!(ss[i]>='a' && ss[i]<='z'))
            {
                if(m[b]!="")
                    cout<<m[b];
                else cout<<b;
                b="";
                cout<<ss[i];
            }
            else b+=ss[i];
        }
        cout<<endl;
    }
    return 0;
}

Bombing

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.
Input

Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 10 9, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.
Output

For each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.

Sample Input

3 2
1 2
1 3
2 3
0 1
1 3
0 0

Sample Output

2
1
#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<cstdio>
using namespace std;
typedef map<int,multiset<int> >t;
map<int,multiset<int> >x;
map<int,multiset<int> >y;
int cmp(t &x,t &y,int d)
{
    int l=x[d].size();
    multiset<int>::iterator it;
    for(it=x[d].begin();it!=x[d].end();it++)
        y[*it].erase(d);
    x[d].clear();
    return l;
}
int main()
{
    long long n,m,i;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        if(n==0 && m==0) break;
        x.clear();
        y.clear();
        for(i=0;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            x[a].insert(b);
            y[b].insert(a);
        }
        while(m--)
        {
            int c,a,b;
            scanf("%d%d",&a,&b);
            if(a==0) c=cmp(x,y,b);
            else c=cmp(y,x,b);
            printf("%d
",c);
        }
        printf("
");
    }
    return 0;
}




原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/6537643.html