前言
t
a
g
:
tag :
tag:字符串
回文串
hash
二分
难
传送门 :
题意 :
你有一个长度为
n
n
n的字符串,你可以选择删除一段后缀(可以为空),然后修改剩余串中的一个字符,使其变成回文串。请问最后能生成多少种可能的回文串。
需要保证整个过程中的字符集仅包含小写字母。其中修改操作必须修改,但可将字符修改为原字符。
思路 :
我们可以将剩余字符串分为三类
本身就是回文,差两个回文,差很多回文
对于本身就是回文的
如果是奇数长度那么贡献
+
26
+26
+26,否则贡献
+
1
+1
+1
对于差一个回文的
因为奇数中间必然不可能非回文的,所以奇数==偶数情况。写几下可知贡献
+
2
+2
+2
下面就是判断是否回文
显然我们可以先计算 哈希数组
当正反 哈希相等的时候就是回文
那么如何判断差一个回文呢
我们这里可以通过 二分
取中间点为起点,然后二分长度
当 m i d − l e n , m i d + l e n mid-len,mid+len mid−len,mid+len的哈希值相等是时候,显然不是,继续二分
因此具有单调性
超级难写
code :
// Problem: 计算题
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11228/G
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define IOS ios::sync_with_stdio(false);
#define CIT cin.tie(0);
#define COT cout.tie(0);
#define ll long long
#define ull long long
#define x first
#define y second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(),x.end()
#define Fup(i,a,b) for(int i=a;i<=b;i++)
#define Fde(i,a,b) for(int i=a;i>=b;i--)
#define cer(a) cerr<<#a<<'='<<(a)<<" @ line "<<__LINE__<<" "<<endl
typedef priority_queue<int,vector<int>,greater<int>> Pri_m;
typedef pair<int,int> pii;
typedef vector<int> VI;
map<int,int> mp;
const int N = 2e5+10,INF = 0x3f3f3f3f , P = 13;
const double eps = 1e-5;
ull h[N],p[N],rh[N];
char str[N];
int n;
ull get(int l,int r){
return h[r] - h[l-1]*p[r-l+1];
}
ull rget(int l,int r){
return rh[r] - rh[l-1]*p[l-1];
}
void calc(){
p[0] = 1;
for(int i=1;i<=n;i++){
h[i] = h[i-1]*P + str[i];
rh[n-i+1] = rh[n-i+2]*P+str[n-i+1];
p[i] = p[i-1]*P;
}
}
int search(int st1,int st2,int len){
int l = 1 ,r = len;
int ans = 0 ;
while(l<=r){
int mid = (l+r)>>1;
// if(get(st1+1,st1+mid) == rget(st2+1,st2 - mid)) l = mid+1;
// else ans = mid, r = mid-1;
if(h[st1+mid] - h[st1]*p[mid] == rh[st2-mid] - rh[st2]*p[mid]) l = mid+1;
else ans = mid , r = mid-1;
}
return st1+ans;
}
void solve(){
cin>>n;
cin>>(str+1);
calc();
ll ans = 0 ;
int x = 0, y = 0 ;
for(int i=1;i<=n;i++){
// if(get(1,i) == rget(i+2,1)){
if(h[i] - h[0]*p[i] == rh[1] - rh[i+1]*p[i]){
if(i&1) ans +=26;
else ans++;
continue;
}
x = search(0,i+1,i);
y = search(x,i+1-x,i-x);
if(y == search(y,i+1-y,i-y))ans+=2;
}
cout<<ans<<endl;
}
int main(){
//int t;cin>>t;while(t--)
solve();
return 0 ;
}