CF 19C 思維題STL應用

life4711發表於2015-01-07

http://codeforces.com/problemset/problem/19/C

Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coincides character by character with its second half. Bob started deleting all the repeats from the string. He does it as follows: while it's possible, Bob takes the shortest repeat, if it is not unique, he takes the leftmost one, and deletes its left half and everything that is to the left of this repeat.

You're given the string seen by Bob. Find out, what it will look like after Bob deletes all the repeats in the way described above.

Input

The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times.

Output

In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way.

Sample test(s)
input
6
1 2 3 1 2 3
output
3
1 2 3 
input
7
4 5 6 5 6 7 7
output
1
7 

/**
CF19C STL
題目大意:給定一個陣列有重複,讓求出刪除重複後的陣列。所謂重複即為;123123,而123124不算重複。必須是2n長度1-n和n+1~n*2完全對應相等
          現在尋找最右邊的一個重複的,將其1~n部分和其前面所有的數全部刪掉。
解題思路:利用list儲存所有相同數字依次出現的位置,從前往後匹配,並一邊刪除,比vector時間複雜度低
*/
#include <stdio.h>
#include <iostream>
#include <map>
#include <list>
using namespace std;
map<int,list<int> > t;
list<int>::iterator it;
int a[200000];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for (int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            t[a[i]].push_back(i);
        }
        a[n]=-1;
        int ans;
        for (int i=0; i<n; i++)
        {
            list<int> e=t[a[i]];
            t[a[i]].pop_front();
            for (it=e.begin(); it!=e.end(); it++)
            {
                int x=i+1;
                int y=(*it)+1;
                while (x<(*it)&&a[x]==a[y])
                {
                    x++;
                    y++;
                }
                if (x==(*it))
                {
                    i=(*it)-1;
                    ans=(*it);
                }
            }
        }
        printf("%d\n",n-ans);
        for (int i=ans; i<n; i++)
            printf(i==n-1?"%d\n":"%d ",a[i]);
    }
    return 0;
}


相關文章