当前位置:网站首页>[nk] Niuke monthly race 51g calculation problem
[nk] Niuke monthly race 51g calculation problem
2022-06-10 21:46:00 【*DDL_ GzmBlog】
Preface
t a g : tag : tag: character string Palindrome string hash Two points difficult
Portal :
The question :
You have a length of n n n String , You can choose to delete a suffix ( Can be null ), Then modify one character in the remaining string , Make it a palindrome string . How many possible palindromes can be generated in the end .
It is necessary to ensure that the character set in the whole process only contains lowercase letters . The modification operation must be modified , However, the character can be modified to the original character .
Ideas :
We can divide the remaining strings into three categories
Itself is palindrome , Two palindromes are missing , A lot of palindromes are missing
For those who are palindromes
If it is an odd length, then the contribution + 26 +26 +26, Otherwise, contribute + 1 +1 +1
For those who are one palindrome away
Because there must be no non palindrome in the middle of odd numbers , So odd numbers == Even numbers . Write down a few knowable contributions + 2 +2 +2
The following is to judge whether to reply
Obviously we can calculate Hash array
When positive and negative Hash When they are equal, they are palindromes
So how to judge the difference between a palindrome
We can go through Two points
Take the middle point as the starting point , And then half the length
When m i d − l e n , m i d + l e n mid-len,mid+len mid−len,mid+len When the hash values of are equal , Obviously not , Keep bisection
Therefore, it has monotonicity
Super hard to write
code :
// Problem: Calculation questions
// 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 ;
}
边栏推荐
- Use DAP link to download the executable file separately to the mm32f5 microcontroller
- C language -- 7 operators
- Redis cluster configuration
- CentOS7环境下MySQL8常用命令小结
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated
- Cas de test app
- Signal and system review 1
- Leetcode advanced path - delete duplicates in the sorting array
- 视频监控系统存储控件,带宽计算方法
- Will your company choose to develop data center?
猜你喜欢

C language -- 1 c language cognition

72. editing distance ●●

SoC development environment and hardware development preparation

What do software test engineers do?

Standard dual airbags, starting from 48900 for butcher Chang'an Lumin

C language ---9 first knowledge of macros and pointers

Understanding deep learning attention

Redis cache avalanche

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)

In 2021, the average salary will be released, and the IT industry will not be surprised
随机推荐
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
H265 Nalu类型判断及 sps 数据解析
[untitled] broken item
自制Table錶格
旋转菜单3.0
Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
LeetCode 进阶之路 - 加一
在Oracle表中进行关键词搜索的过程
旋转导航栏
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
Calculus review 1
app測試用例
[qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
实用 | 如何利用 Burp Suite 进行密码爆破!
Vissim仿真快速入门
用少儿编程思维塑造青少年领悟能力
Brute force method / task assignment
Whether there is a simple path from brute force method /u to V
Read the source code of micropyton - add the C extension class module (3)
Nanny tutorial: how to become a contributor to Apache linkis documents