AM文字列

AM文字列

問題解決のアイデア:ハッシュ。プレフィックスの長さはすでにわかっているため、プレフィックスの対称ハッシュ値は、比較のためにハッシュによって簡単に計算できます。


バイナリ文字列のハッシュを統合します

0 1 2 3 4 5 5
ビット 1 2 4 8 16 32 64
バイナリ 1 0 1 0 1 1 0
a 1 2 5 10 21 43 86

2〜4の2進数「101」を見つけます。a[4] -a [1] * bit [3]、

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e6+10;
const int M = 50005;
const ll base = 131;
const ll mod =  1000000007;
const double PI = acos(-1.0);
const double eps = 1e-4;

inline int read(){
    
    int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}return x*f;}
inline string readstring(){
    
    string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){
    
    s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){
    
    str+=s;s=getchar();}return str;}
int random(int n){
    
    return (int)(rand()*rand())%n;}
void writestring(string s){
    
    int n = s.size();for(int i = 0;i < n;i++){
    
    printf("%c",s[i]);}}
ll fast_power(ll a,ll p){
    
    
    ll ret = 1;
    while(p){
    
    
        if(p&1) ret = (ret*a)%mod;
        p >>= 1;
        a = (a*a)%mod;
    }
    return ret;
}


char str[N];
ll a[N],b[N];
ll bit[N];

int main(){
    
    
    //srand((unsigned)time(NULL));
    //freopen( "out.txt","w",stdout);
    scanf("%s",str+1);
    bit[0] = 1;
    for(int i = 1;i < N;i++){
    
    
        bit[i] = (bit[i-1]*base);
    }
    int n = strlen(str+1);
    for(int i = 1;i <= n;i++){
    
    
        a[i] = a[i-1]*base+(str[i]-97);
    }
    for(int i = n;i >= 1;i--){
    
    
        b[i] = b[i+1]*base+(str[i]-97);
    }

    int ans = 0;
    for(int i = 1;i <= n;i++){
    
    
        if(i+i <= n){
    
    
            ll x = a[i];
            ll p = b[1]-b[i+1]*bit[i];
            ll y = a[i+i]-a[i]*bit[i];
            ll t = b[i+1]-b[i+i+1]*bit[i];
            if(x==y&&x==t&&x==p) ans++;
        }
        if(i+i-1 <= n){
    
    
            ll x = a[i];
            ll y = a[i+i-1]-a[i-1]*bit[i];
            ll t = b[i]-b[i+i]*bit[i];
            if(x == y && y == t) ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

おすすめ

転載: blog.csdn.net/weixin_42868863/article/details/115274631