1. 程式人生 > >Unique Snowflakes(JSU-ZJJ)

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

        }
    }
}