【STL】 map、set;

map

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 113243    Accepted Submission(s): 44255

Problem Description
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.
Input
Input 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.
 
Output
For 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<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

map<string,int >cnt;
int main()
{
    int n;
    cnt.clear();   //清空map;
    while(cin>>n)
    {
        if(n==0)  break;
        string s;
        for(int i=0;i<n;i++)
        {
            cin>>s;
            if(!cnt.count(s))  //查询map中是否有字符串s;
                cnt[s]=1;
            else
                cnt[s]++;
        }

        int maxx=0;
        map<string ,int>::iterator it,tmp;  //map的迭代;
        for(it=cnt.begin();it!=cnt.end();it++)
        {
            if(it->second>maxx)
            {
                maxx=it->second;   //调用map;
                tmp=it;
            }
        }
        cout<<tmp->first<<endl;
        cnt.clear();
    }
    return 0;
}


map与pair函数;

B. Parallelogram is Back
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal.

Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points.

Input

The input consists of three lines, each containing a pair of integer coordinates xi and yi ( - 1000 ≤ xi, yi ≤ 1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide.

Output

First print integer k — the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.

Then print k lines, each containing a pair of integer — possible coordinates of the fourth point.

Example
input
0 0
1 0
0 1
output
3
1 -1
-1 1
1 1
Note

If you need clarification of what parallelogram is, please check Wikipedia page:

https://en.wikipedia.org/wiki/Parallelogram


代码:

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;


set<pair<int,int > >s;  //定义set,用到了pair函数;
/*pair的应用
pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,
如stl中的map就是将key和value放在一起来保存。
另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。
 pair的实现是一个结构体,主要的两个成员变量是first second 
 因为是使用struct不是class,所以可以直接使用pair的成员变量。*/
int main()
{
    int x1,y1,x2,y2,x3,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    s.insert(make_pair(x1+x2-x3,y1+y2-y3));
    s.insert(make_pair(x1+x3-x2,y1+y3-y2));
    s.insert(make_pair(x2+x3-x1,y2+y3-y1));
    cout<<s.size()<<endl;
    set<pair<int,int> >::iterator it;
    for(it=s.begin();it!=s.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    return 0;
}
于本题而言,k恒为3;


A. Lesha and array splitting
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.

Lesha is tired now so he asked you to split the array. Help Lesha!

Input

The first line contains single integer n (1 ≤ n ≤ 100) — the number of elements in the array A.

The next line contains n integers a1, a2, ..., an ( - 103 ≤ ai ≤ 103) — the elements of the array A.

Output

If it is not possible to split the array A and satisfy all the constraints, print single line containing "NO" (without quotes).

Otherwise in the first line print "YES" (without quotes). In the next line print single integer k — the number of new arrays. In each of the next k lines print two integers li and ri which denote the subarray A[li... ri] of the initial array A being the i-th new array. Integers lirishould satisfy the following conditions:

  • l1 = 1
  • rk = n
  • ri + 1 = li + 1 for each 1 ≤ i < k.

If there are multiple answers, print any of them.

Examples
input
3
1 2 -3
output
YES
2
1 2
3 3
input
8
9 -12 3 4 -4 -10 7 3
output
YES
2
1 2
3 8
input
1
0
output
NO
input
4
1 2 3 -5
output
YES
4
1 1
2 2
3 3
4 4

大意:给你一个数组,问是否能找到元素和不为 0 的子数组。

思路:只要原数组不全为 0 就肯定 YES 啊,然后不为 0 的元素自己就是一个(只存在一个元素)数组,碰到 0 的元素,就跟下一个不为的 0 的元素合一块作为一个数组就行了


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int> pii;
int n;
int a[110];
pii ans[110];
int main()
{
	while(~scanf("%d",&n))
	{
		int cnt=0,k=0,p=1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",a+i);
			if(a[i]!=0)
			{
				ans[k].first=p;
				ans[k++].second=i;
				p=i+1;
			}
		}
		if(p!=n+1) // 说明末尾有存在一定数量的 0 
			ans[k-1].second=n;
		if(k==0)
		{
			puts("NO");
		}
		else
		{
			puts("YES");
			printf("%d\n",k);
			for(int i=0;i<k;i++)
				printf("%d %d\n",ans[i].first,ans[i].second);
		}	
	}
	return 0;
}
/*

5 
1 -1 1 -1 1 

*/ 




可用map习题

B. Santa Claus and Keyboard Check
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be.

In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.

You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.

Input

The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.

Output

If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print «-1» (without quotes).

Otherwise, the first line of output should contain the only integer k (k ≥ 0) — the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct.

If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair.

Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.

Examples
input
helloworld
ehoolwlroz
output
3
h e
l o
d z
input
hastalavistababy
hastalavistababy
output
0
input
merrychristmas
christmasmerry
output
-1

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;

map<char ,char >cnt1;
map<char ,char >cnt2;
char s1[1003],s2[1003];
int main()
{
    int same1,same2,flag;
    while(~scanf("%s%s",s1,s2))
    {
        cnt1.clear(),cnt2.clear();
        same1=0,same2=0,flag=0;
        int len=strlen(s1);
        for(int i=0; i<len; i++)
        {
            char ss1,ss2,t;
            ss1=s1[i],ss2=s2[i];
            if(ss1>ss2)
            {
                t=ss1;
                ss1=ss2;
                ss2=t;
            }
            if((cnt1[ss1]!=NULL)&&cnt1[ss1]!=ss2)
                flag=1;
            else
                cnt1[ss1]=ss2;
            if((cnt2[ss2]!=NULL)&&cnt2[ss2]!=ss1)
                flag=1;
            else
                cnt2[ss2]=ss1;
        }
        map<char ,char>::iterator it1,it2,its;

//        for(it=cnt.begin(); it!=cnt.end(); it++)
//            cout<<it->first<<" "<<it->second<<"  nani"<<endl;
//        printf("store ok!\n");

        //再开一个map;

        for(it1=cnt1.begin(); it1!=cnt1.end(); it1++)
        {
            if(it1->first==it1->second)
                same1++;
        }
        for(it2=cnt2.begin();it2!=cnt2.end();it2++)
        {
            if(it2->first==it2->second)
                same2++;
        }

        its=cnt1.begin();  its++;
        for(it1=cnt1.begin(); its!=cnt1.end(); its++,it1++)
        {
            if(its->first==it1->second||its->second==it1->second)
                flag=1;
        }
        its=cnt2.begin();  its++;
        for(it2=cnt2.begin(); its!=cnt2.end(); its++,it2++)
        {
            if(its->first==it2->second||its->second==it2->second)
                flag=1;
        }
        if(cnt1.size()!=cnt2.size())
            flag=1;

        if(flag)
        {
            printf("-1\n");
        }
        else
        {
            int m=cnt1.size()-same1;
            cout<<m<<endl;
            for(it1=cnt1.begin(); it1!=cnt1.end(); it1++)
            {
                if(it1->first==it1->second)
                    continue;
                else
                    cout<<it1->first<<" "<<it1->second<<endl;
            }
        }

    }
    return 0;
}


set

{A} + {B}

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19006    Accepted Submission(s): 7983


Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
 
Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
 
Sample Input
  
  
1 2 1 2 3 1 2 1 1 2

Sample Output
  
  
1 2 3 1 2

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MOD=7;
const int N=5;

int main()
{
    set<int >s;     //定义集合set;
    int n,m;
    while(cin>>n>>m)
    {
        int x;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            s.insert(x);    //插入元素s;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d",&x);
            s.insert(x);
        }
        set<int>::iterator Ite=s.begin();  //定义set迭代器;
        cout<<*Ite;          //输出set中元素;
        Ite++;
        for(;Ite!=s.end();Ite++)
            cout<<" "<<*Ite;
        cout<<endl;
        s.clear();           //清空set;
    }
    return 0;
}


set

单词数

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 48101    Accepted Submission(s): 11728


Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
 
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束  

Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
 
Sample Input
  
  
you are my friend #  

Sample Output
  
  
4

代码:

#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000000;

char w[N];
int main()
{
    int len;
    set<string >words;
    while(gets(w)!=NULL)
    {
        words.clear();
        string s;
        int len=strlen(w);
        if(w[0]=='#')
            break;
        for(int i=0;i<len;i++)
        {
            if(w[i]==' '&&!s.empty())
            {
                words.insert(s);
                s.clear();
            }
            else if(w[i]!=' ')
                s+=w[i];
        }
        if(!s.empty())  words.insert(s);
        cout<<words.size()<<endl;
//        set<string >::iterator it;
//        for(it=words.begin();it!=words.end();it++)
//            cout<<"|"<<*it<<"|"<<" ";
//        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值