zoukankan      html  css  js  c++  java
  • Codeforces Round #336 B

    Hamming Distance Sum

    题意:给一个a串一个b串,a b都是0 1串,且strlen(a)<=strlen(b),在b串中取出连续的长度为strlen(a) 的子串,每次与a异或,每个元素异或的值相加,求和

    思路:a串中每个元素都异或了 lb-la+1次,且a中第i个元素是和b串中的di i到i+lb-la+1区间内的元素异或,所以只要求出区间内有多少个1多少个0就可以了,前缀和处理,以后用前缀记得一定从1开始 不要从0开始,坑死我了

    AC代码:

    #include "iostream"
    #include "string.h"
    #include "stack"
    #include "queue"
    #include "string"
    #include "vector"
    #include "set"
    #include "map"
    #include "algorithm"
    #include "stdio.h"
    #include "math.h"
    #define ll long long
    #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
    #define mem(a) memset(a,0,sizeof(a))
    using namespace std;
    const int N=1e5+100;
    char a[2*N],b[2*N];
    int pre[2*N]={0};
    int main(){
        cin>>a+1>>b+1;
        int la=strlen(a+1),lb=strlen(b+1);
        for(int i=1; i<=lb; ++i){
            if(b[i]=='1') pre[i]=1;
        }
        for(int i=1; i<=lb; ++i){
            pre[i]+=pre[i-1];
        }
        ll ans=0;
    
        for(int i=1; i<=la; ++i){
            if(a[i]=='0')
                ans+=(pre[lb-la+i]-pre[i-1]);
            else
                ans+=(lb-la+1-pre[lb-la+i]+pre[i-1]);
        }
        cout<<ans<<endl;
        return 0;
    }
    /*
    01
    00111
    0011
    0110
    */
  • 相关阅读:
    利用Oracle创建数据库
    安装Oracle 10g
    安装ArcGIS Engine 9.3
    安装ArcGIS Desktop 9.3
    绘制扇形效果线条小Bug解决
    MFC画线功能总结
    MFC消息映射机制以及画线功能实现
    truncate
    postgresql死锁处理
    python编程中的if __name__ == 'main': 的作用
  • 原文地址:https://www.cnblogs.com/max88888888/p/7106234.html
Copyright © 2011-2022 走看看