CF19C Deletion of Repeats 题解
应该是最简单的做法。
模拟题目描述,从左往右枚举左端点,然后找重复。
容易发现重复的两半开头必须相同,直接枚举后面的和左端点相同的位置作为重复后面一半的开头,使用字符串哈希 check 一下两半是否完全相同。
如果相同就直接更新当前被删的前缀最大值,然后结束循环,后面遇到被删除的左端点就跳过即可。
#include <bits/stdc++.h>
#define lowbit(o) o & -o
#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 100005, P = 133331;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 2e18;
int a[N], tmp[N], n;
ULL p[N], h[N];
vector<int> pos[N];
ULL gethash(int l,int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
void work()
{
cin >> n;
for (int i=1;i<=n;i++)
cin >> a[i], tmp[i] = a[i];
sort(tmp + 1, tmp + n + 1);
for (int i=1;i<=n;i++)
a[i] = lower_bound(tmp + 1, tmp + n + 1, a[i]) - tmp, pos[a[i]].pb(i);
p[0] = 1;
for (int i=1;i<=n;i++)
{
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + a[i];
}
int mx = 0;
for (int i=1;i<=n;i++)
{
if (mx >= i)
continue;
for (int j : pos[a[i]])
{
if (j <= i)
continue;
int len = j - i;
if (j + len - 1 > n)
break;
if (gethash(i, j - 1) == gethash(j, j + len - 1))
{
mx = max(mx, j - 1);
break;
}
}
}
cout << n - mx << '\n';
for (int i=mx+1;i<=n;i++)
cout << tmp[a[i]] << ' ';
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int _T = 1;
//cin >> _T;
while (_T--)
work();
return 0;
}