传送门:Friends
思路:枚举每一个位置的字母,求出删去该出字母后的字符串是否由两段相同字符串拼接而成的,同时要注意一种特殊的情况,题目里面没有说,但形如ABBBCABBC这种有三个位置都可以得到ABBCABBC这一个,他们是算一个。
其余就奇数直接out。
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef unsigned long long ull;
const int pp=13331,N=2e6+10;
char a[N];
ull p[N];
ull last;
ull s[N];
int n;
ull get(ull a[],int l,int r)
{
return a[r]-a[l-1]*p[r-l+1];
}
int main()
{
cin>>n;
scanf("%s",a+1);
if(n%2==0)
{
printf("NOT POSSIBLE\n");
return 0;
}
p[0]=1;
for(int i=1;i<=n;i++)
{
s[i]=s[i-1]*pp+a[i];
p[i]=p[i-1]*pp;
}
int sum=0;
int ans;
for(int i=1;i<=n;i++)
{
if(i==n/2+1)
{
if(get(s,1,n/2)!=last&&get(s,1,n/2)==get(s,n/2+2,n))
{
last=get(s,1,n/2);
sum++;
ans=i;
}
}else if(i<=n/2)
{
if(get(s,n/2+2,n)!=last&&get(s,n/2+2,n)==get(s,1,i-1)*p[n/2+1-i]+get(s,i+1,n/2+1))
{
last=get(s,n/2+2,n);
sum++;
ans=i;
}
}else
{
if(get(s,1,n/2)!=last&&get(s,1,n/2)==get(s,n/2+1,i-1)*p[n-i]+get(s,i+1,n))
{
last=get(s,1,n/2);
sum++;
ans=i;
}
}
if(sum>1)
break;
}
if(sum>1)
puts("NOT UNIQUE");
else if(sum==0)
puts("NOT POSSIBLE");
else{
if(ans>=n/2+1)
{
for(int i=1;i<=n/2;i++)
printf("%c",a[i]);
}else
for(int i=n/2+2;i<=n;i++)
printf("%c",a[i]);
}
return 0;
}
思路:使用map<string,int>会爆内存,这样只能用map<ull,int>,存储每一个删除了两个字符之后的字符串的哈希值,这里除了求两段区间的哈希值还要求他们拼接起来后的才是真正的哈希值,这里字符串拼接不是简单的
// Problem: D. Remove Two Letters
// Contest: Codeforces - Codeforces Round #855 (Div. 3)
// URL: https://codeforces.com/contest/1800/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int N = 2e5 + 10, pp = 13331;
int d[N];
char a[N];
int n, t, m;
ull p[N];
ull s[N];
ull get(ull a[], int l, int r) {
// if (l > r) return 0;
return a[r] - a[l - 1] * p[r - l + 1];
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
map<ull, int> mp;
scanf("%d", &n);
scanf("%s", a + 1);
p[0] = 1;
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] * pp + a[i];
p[i] = p[i - 1] * pp;
}
int ans = 0;
ull tmp = pp * pp;
for (int i = 1; i < n; i++) {
ull x = get(s, i + 2, n) + get(s, 1, i - 1) * p[n - i - 1];
// cout << x << endl;
if (mp[x] == 0) {
ans++;
mp[x]++;
}
}
cout << ans << endl;
}
return 0;
}