Unique Snowflakes(JSU-ZJJ)

题目描述

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 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 10^9, 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.

输出

For each test case output a line containing single integer, the maximum number of unique snowflakes that can be in a package.

样例输入 Copy

1
5
1
2
3
2
1

样例输出 Copy

3

分析:

这个题一开始题目都为读懂。这个题的题意大致是从一组数中找到最长的不重复数列。看题目条件的要求,我一开始只开了一个10000的数组。导致一直运行错误。这是失误。在本题中,我一开始的考虑是用C语言写,不借助容器set。但是用那种方法内存超限。因为我要开一个10的9次方的数组大小来保存i是否在之前出现过。我后来打算看一下是否能水过。就并没有把数组开的那么大。很幸运水过。
本题的话用C++里面的容器更加好写。
下面先将c的代码复制如下:
思路

#include"stdio.h"
#include"string.h"
int a[1000001];
typedef struct
{
    long long data[1000000];
    long long  rear;
    long long  front;
} SqQueue;
SqQueue Q;
//这里是寻找最长非重复子序列的程序
long long Serch()
{

    long long MAX_lenght=0;
    long long start=0;
    long long i=Q.front,j,k;
//标记一下,如果值为-1,则表示之前为出现过
    for(i=0;i<(long long)1000001;i++)
        a[i]=-1;
    k=1;
//为了避免最后一个数字为被考虑到。
//所以将最后一个数的后一位赋值为最后一位。
    Q.data[Q.rear]=Q.data[Q.rear-1];
z
//遍历数组。
    for(j=0; j<=Q.rear; j++)
    {
//这里表示Q.data[j]在start到j中出现过。
        if(a[Q.data[j]]>=start)
        {
        //如果当前的最大子序列长度小于start到j-1的。那么更新
           if(MAX_lenght<j-start)
              MAX_lenght=j-start;
           //将start更新为上次出现的地方的后一位
           start=a[Q.data[j]]+1;
           //同时更新Q.data[j]的新出现位置
           a[Q.data[j]]=j;

        }
        else
        {
        //如果未出现过,则保存下来 
             a[Q.data[j]]=j;

        }
    }


    return MAX_lenght;
}
int main()
{
    long long  T;
    long long  N;
    long long  i,count;
    while(~scanf("%lld",&T))
    {
        while(T--)
        {


            scanf("%lld",&N);
            //输入数据
            for(i=0; i<N; i++)
                scanf("%lld",&Q.data[i]);

            Q.front=0;
            Q.rear=N;
            count=Serch();

            printf("%lld\n",count);
        }
    }
}

C++
利用set容器来写。可以先去了解set

#include"algorithm"
#include"set"
#include"cstdio"
using namespace std;
long long a[1000000];
int main()
{
    long long N,i,j,k,T,left,right,Max;
    while(~scanf("%lld",&T))
    {
        while(T--)
        {
            scanf("%lld",&N);
            for(i=0;i<N;i++)
                scanf("%lld",&a[i]);
            set<long long> A;
            left=0;right=0;Max=0;
            while(right<N)
            {
                while(right<N&&A.count(a[right])==0)
                {
                    A.insert(a[right]);
                    right++;
                }
                if(Max<right-left)
                    Max=right-left;
                A.erase(a[left]);
                left++;
            }

         printf("%lld\n",Max);

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43506138/article/details/85775957