codeforces #336 div 2 B. Hamming Distance Sum
http://codeforces.com/contest/608/problem/B 题意:给定两个字符串a,b,问b中的每个连续的长度为a的子串与a的哈密顿距离的和是多少。哈密顿距离是对应位置的字符的差的绝对值的和。由于是01串,也就是字符不同的位置数。 思路:类似前缀和。0和1分别搞。注意开long long
/* ***********************************************
Author :111qqz
Created Time :2015年12月24日 星期四 00时32分33秒
File Name :code/cf/#336/B.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=3E5+7;
int la,lb;
char a[N],b[N];
int sum0[N],sum1[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
ms(sum0,0);
ms(sum1,0);
scanf("%s",a);
scanf("%s",b);
la = strlen(a);
lb = strlen(b);
int sum = 0 ;
if (la==1)
{
for ( int i = 0 ; i < lb ; i++)
{
if (b[i]!=a[0]) sum++;
}
cout<<sum<<endl;
return 0;
}
for ( int i = 0 ; i < lb ; i++)
{
if (b[i]=='0')
{
sum0[i+1] = sum0[i]+1;
sum1[i+1] = sum1[i];
}
else
{
sum1[i+1] = sum1[i] +1;
sum0[i+1] = sum0[i];
}
}
// for ( int i = 1 ; i <= lb ; i++)
// cout<<sum0[i]<<" "<<sum1[i]<<endl;
LL ans = 0;
for (int i= 0 ; i < la ; i++)
{
if (a[i]=='0')
{
ans+=LL(sum1[lb-la+i+1]-sum1[i]);
}
else
{
ans+=LL(sum0[lb-la+i+1]-sum0[i]);
}
// cout<<ans<<endl;
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}