CodeForces - 19C Deletion of Repeats【离散化】【字符串哈希】

7 篇文章 0 订阅
6 篇文章 0 订阅

题目链接:https://codeforces.com/contest/19/problem/C

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
static const int MAXN=1e5+10;
static const int P=13331;
int n;
map<int,int> _hash;
int a[MAXN],b[MAXN],idx;
vector<int> seq[MAXN];
ull h[MAXN],p[MAXN];
vector<PII> same;
bool check(int l1,int l2)
{
    int r1=l2-1,r2=l2*2-l1-1;
    if(r2>n) return false;
    return h[r1]-h[l1-1]*p[r1-l1+1]==h[r2]-h[l2-1]*p[r2-l2+1];
}
int main()
{
    scanf("%d",&n);
    p[0]=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
        if(!_hash.count(b[i])) _hash[b[i]]=++idx;
        a[i]=_hash[b[i]];
        seq[a[i]].push_back(i);
        p[i]=p[i-1]*P;
        h[i]=h[i-1]*P+a[i];
    }
    for(int i=1;i<=idx;i++)
        for(int j=0;j<seq[i].size();j++)
            for(int k=j+1;k<seq[i].size();k++)
                if(check(seq[i][j],seq[i][k])) same.push_back({seq[i][k]-seq[i][j],seq[i][j]});
    sort(same.begin(),same.end());
    int pos=1;
    for(int i=0;i<same.size();i++)
        if(same[i].second>=pos) pos=same[i].second+same[i].first;
    printf("%d\n",n-pos+1);
    for(int i=pos;i<=n;i++)
        printf("%d ",b[i]);
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值