[Baltic2014]friends

时间:2024-09-18 12:34:38

嘟嘟嘟

首先想想暴力的做法,枚举加入的字符,然后判断删去这个字符后两个长度为n / 2的字符串是否相等,复杂度O(n2)。

所以可以想办法把判断复杂度降低到O(1),那自然就想到hash了。hash是能做到O(n)预处理,然后O(1)比较的。

取一段的hash值:hash[L, R] = hash[1, R] - hash[1, L - 1] * baseR - L + 1.这个也好理解,就是前面的hash[1, L - 1]为整个hash值贡献了hash[1, L - 1] * baseR - L + 1,减去即可。

思路就这么明显~~只不过合并hash值得时候得想清楚了……

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int INF = 0x3f3f3f3f;
const ull base = ;
const int eps = 1e-;
const int maxn = 2e6 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, mid;
char s[maxn];
ull has[maxn], f[maxn], las = ;
int del, tot = ; ull get(int L, int R)
{
return has[R] - has[L - ] * f[R - L + ];
}
int judge(int x)
{
ull h1, h2;
if(x < mid)
{
h1 = get(, x - ) * f[mid - x] + get(x + , mid);
h2 = get(mid + , n);
}
else if(x > mid)
{
h1 = get(, mid - );
h2 = get(mid, x - ) * f[n - x] + get(x + , n);
}
else
{
h1 = get(, x - );
h2 = get(x + , n);
}
if(h1 == h2)
{
del = x;
if(h1 == las) return ;
las = h1; return ;
}
return ;
} int main()
{
n = read(); scanf("%s", s + );
if(!(n & )) {puts("NOT POSSIBLE"); return ;}
f[] = ;
for(int i = ; i <= n; ++i) f[i] = f[i - ] * base;
for(int i = ; i <= n; ++i) has[i] = has[i - ] * base + s[i] - 'A' + ;
mid = (n >> ) + ;
for(int i = ; i <= n; ++i) tot += judge(i);
if(!tot) puts("NOT POSSIBLE");
else if(tot > ) puts("NOT UNIQUE");
else
{
for(int i = , cnt = ; cnt <= (n >> ); ++i, cnt++)
{
if(i == del) cnt--;
else putchar(s[i]);
}
enter;
}
return ;
}