2020 CCPC河南省赛:子串翻转回文串(字符串Hash)

 PS:自然溢出hash会被卡,cin,endl也会被卡!!!!!!!!!!!!!!!!!!!!!!!

#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int N=1e6+5,mod=1e9+7,P=131;
int n;
int h1[N],h2[N],p[N];
char str[N],s[N];
int init(){
    int len=strlen(str+1);
    for(int i=1,j=len;i<=len&&i<j;i++,j--){
        if(str[i]==str[j]){
            str[i]=str[j]='#';
        }
        else break;
    }
      
    int k=0;
    for(int i=1;i<=len;i++){
        if(str[i]=='#')continue;
        s[++k]=str[i];
    }
    return k;
} 
  
void make_hash(){
    p[0]=1,h1[0]=0;
    for(int i=1;i<=n;i++){
        h1[i]=(h1[i-1]*P%mod+s[i])%mod;
        p[i]=p[i-1]*P%mod; 
    }
      
    h2[n+1]=0;
    for(int i=n;i>=1;i--){
        h2[i]=(h2[i+1]*P%mod+s[i])%mod;
    }
}
  
int get1(int l,int r){
    return (h1[r]%mod-h1[l-1]%mod*p[r-l+1]%mod+mod)%mod;
}
  
int get2(int l,int r){
    return (h2[l]%mod-h2[r+1]%mod*p[r-l+1]%mod+mod)%mod;
}
  
bool judge(int l,int r){
    int hash_l=(get1(1,n)-get1(l,r)*p[n-r]%mod+get2(l,r)*p[n-r]%mod+mod)%mod;
    int hash_r=(get2(1,n)-get2(l,r)*p[l-1]%mod+get1(l,r)*p[l-1]%mod+mod)%mod;
    //cout<<hash_l<<"~~~~~~"<<hash_r<<endl;
    if(hash_l==hash_r)return true;
    return false;
}
  
bool check(){
    //cout<<"%%%%%"<<n<<endl;
     for(int i=1;i<=n;i++){
        if(judge(1,i))return true;
     }
     for(int i=n;i>=1;i--){
        if(judge(i,n))return true;
     }
     return false;
}
  
 
main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",str+1);
        //n=strlen(str+1);
        n=init();
        make_hash();
          
        //debug();
          
        if(check()||n==0)puts("Yes");
        else puts("No");
           
    }
}
#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
using namespace std;
const int N=1e6+5,mod=1e9+7,P=131;
int n;
int h1[N],h2[N],p[N];
char s[N];
  
void make_hash(){
    p[0]=1,h1[0]=0;
    for(int i=1;i<=n;i++){
        h1[i]=(h1[i-1]*P%mod+s[i])%mod;
        p[i]=p[i-1]*P%mod; 
    }
      
    h2[n+1]=0;
    for(int i=n;i>=1;i--){
        h2[i]=(h2[i+1]*P%mod+s[i])%mod;
    }
}
  
int get1(int l,int r){
    return (h1[r]%mod-h1[l-1]%mod*p[r-l+1]%mod+mod)%mod;
}
  
int get2(int l,int r){
    return (h2[l]%mod-h2[r+1]%mod*p[r-l+1]%mod+mod)%mod;
}
  
bool judge(int l,int r){
    int hash_l=(get1(1,n)-get1(l,r)*p[n-r]%mod+get2(l,r)*p[n-r]%mod+mod)%mod;
    int hash_r=(get2(1,n)-get2(l,r)*p[l-1]%mod+get1(l,r)*p[l-1]%mod+mod)%mod;
     
    if(hash_l==hash_r)return true;
    return false;
}
  
bool check(){
      
     int pos=-1;
     for(int i=1,j=n;i<=n;i++,j--){
        if(s[i]==s[j])continue;
        pos=i;
        break;
     }
     
     for(int i=pos+1;i<=n-pos+1;i++)
        if(judge(pos,i)||judge(i,n-pos+1))return true;
     
     return false;
}
  
 
main(){
    IOS;
    int T;
    cin>>T;
    while(T--){
        cin>>s+1;
         
        n=strlen(s+1); 
        make_hash();
            
        if(check())puts("Yes");
        else puts("No");
           
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值