回文字D

回文字D

https://ac.nowcoder.com/acm/contest/11169/D

题目:https://ac.nowcoder.com/acm/contest/11169

  • 从左到右,从右到左分别求一次字符串hash值
  • 从字符串起点每次枚举长度为D的字符串,如果是回文串,继续往后枚举,如果不是回文串ans++
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; 
const int INF=0x3f3f3f3f;
const int N=1e7+5, P=131;
ULL h[N], rh[N], p[N];
char s[N];

bool check(int l, int r){
    return h[r]-h[l-1]*p[r-l+1]==rh[l]-rh[r+1]*p[r-l+1];
} 

int main(){
    int n,d;
    cin>>n>>d;
    cin>>s+1;
    p[0]=1;
    for(int i=1; i<=n; i++){
        h[i]=h[i-1]*P+s[i];
        p[i]=p[i-1]*P;
    }

    for(int i=n; i>=1; i--){
        rh[i]=rh[i+1]*P+s[i];
    }

    int ans=0;

    for(int i=1, j; i<=n; i=j+1){
        j=i+d-2;
        while(j+1<=n&&check(j+1-d+1, j+1)) j++;  //j+1<=n,处理了边界问题
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}
全部评论
我想问一下大佬, 38行 j+1-d+1 这里的j 不就是37行的j=i+d-2,将此时的i+d-2 带入 ,之后不就是i吗,也就是 while(j+1<=n&&check(i, j+1)), 但是这么弄的话,就会wa,为什么呢
点赞 回复 分享
发布于 2021-03-28 14:45

相关推荐

饼子吃到撑:当我看到外企的时候,我就知道这大概率可能是真的
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务