王小波说:“别怕美好的一切消失,咱们先来让它存在。”
好久不见,甚是想念呐!小李马不停蹄开始整理stl笔记了。今天要介绍的朋友是set!(掌声欢迎牛年第一个知识点)
一、定义:
~1.概念:一个不包含重复元素的 collection。 (更确切地讲,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素。正如其名称所暗示的,此接口模仿了数学上的 set 抽象。)
2.调用格式:set(句柄,属性名1,属性值1,属性名2,属性值2,…)~
二、作用:
~boolean add(E e)
如果 set 中尚未存在指定的元素,则添加此元素(可选操作)。
boolean addAll(Collectionc)
如果 set 中没有指定 collection 中的所有元素,则将其添加到此 set 中(可选操作)。
void clear()
移除此 set 中的所有元素(可选操作)。
boolean contains(Object o)
如果 set 包含指定的元素,则返回 true。
boolean containsAll(Collectionc)
如果此 set 包含指定 collection 的所有元素,则返回 true。
boolean equals(Object o)
比较指定对象与此 set 的相等性。
int hashCode()
返回 set 的哈希码值。
boolean isEmpty()
如果 set 不包含元素,则返回 true。
Iterator iterator()
返回在此 set 中的元素上进行迭代的迭代器。
boolean remove(Object o)
如果 set 中存在指定的元素,则将其移除(可选操作)。
boolean removeAll(Collectionc)
移除 set 中包含在指定 collection 中的元素(可选操作)。
boolean retainAll(Collectionc)
仅保留 set 中包含在指定 collection 中的元素。
int size()
返回 set 中的元素数(其容量)。
Object[] toArray()
返回一个包含 set 中所有元素的数组。
T[]
toArray(T[] a)
返回一个包含此 set 中所有元素的数组;返回数组的运行时类型是指定数组的类型。~
三、例题:
~1. Unique Snowflakes
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a
machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow,
one by one, into a package. Once the package is full, it is closed and shipped to be sold.
The marketing motto for the company is “bags of uniqueness.” To live up to the motto, every
snowflake in a package must be different from the others. Unfortunately, this is easier said than done,
because in reality, many of the snowflakes flowing through the machine are identical. Emily would like
to know the size of the largest possible package of unique snowflakes that can be created. The machine
can start filling the package at any time, but once it starts, all snowflakes flowing from the machine
must go into the package until the package is completed and sealed. The package can be completed
and sealed before all of the snowflakes have flowed out of the machine.Input
The first line of input contains one integer specifying the number of
test cases to follow. Each test case begins with a line containing an
integer n, the number of snowflakes processed by the machine. The
following n lines each contain an integer (in the range 0 to 109 ,
inclusive) uniquely identifying a snowflake. Two snowflakes are
identified by the same integer if and only if they are identical. The
input will contain no more than one million total snowflakes.Output
For each test case output a line containing single integer, the
maximum number of unique snowflakes that can be in a package.
Sample Input 1 5 1 2 3 2 1
Sample Output 3
解题思路:一般我们如果看到数组去重就要条件反射的想到set。这道题需要我们用map来记住每个数的位置。map一边存,一边判断,一旦出现重复数,就把这个重复数前面的全清空。
AC代码:
#include <iostream>
#include<cstring>
#include<map>
using namespace std;
#define N 100000010
int a[N];
int t,i,j,n,count,ans;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
map<int,int>ma;
count=ans=0;
for(i=1;i<=n;i++)
scanf("%d",a+i);
for(j=1;j<=n;j++)
{
if(ma[a[j]]==0)
{
ma[a[j]]=j;//记住位置
count++;//长度
}
else
{
if(count>ans) ans=count;
j=ma[a[j]];//若出现重复数a[j],就从j+1位置循环
ma.clear();//清空重复数前面的数字
count=0; //长度清零
}
}
if(count>ans) ans=count;
printf("%d\n",ans);
}
return 0;
}