题目链接:http://codeforces.com/problemset/problem/608/B

题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减的绝对值然后相加(这个讲得有点绕),直接举个例子就很容易理解了。

  假设a = 01,b = 00111,

  符合条件的b的子串有:00, 01, 11, 11

  然后: |01-00| = |0-0| + |1-0| = 1,

      |01-01| = |0-0| + |1-1| = 0,

     |01-11| = |0-1| + |1-1| = 1,

      |01-11| = |0-1| + |1-1| = 1,

  再求和:1 + 0 + 1 + 1 = 3

  这个挺考观察能力的= =

  枚举 a 的每一位,然后找出 b 中哪些子串需要跟a的这一位进行运算的。

  假设 a = 010,b = 00111,la:a的字符串长度, lb:b的字符串长度

  第 i 位      需要 b 的 哪几位进行运算

  1          1, 2, 3 

  2          2, 3, 4

  3          3, 4, 5

  然后有个这样的关系:对于字符串a中第 i 位,需要b中 i ~ lb-(la-i)   的这些数进行运算。

  可以发现,如果 a 中第 i 位的数为1, 那么b中 i ~ lb-(la-i)   的这些数为 0 跟它相减才等于1,否则为0; a 为 0 的话亦然。。。。最后就是注意用 long long 啦,10^18很大呢~

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
char a[maxn], b[maxn];
int c0[maxn], c1[maxn]; long long res; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%s%s", a+, b+) != EOF) {
int la = strlen(a+);
int lb = strlen(b+); memset(c0, , sizeof(c0));
memset(c1, , sizeof(c1)); for (int i = ; i <= lb; i++) {
c1[i] = c1[i-];
c0[i] = c0[i-]; if (b[i] == '') {
c0[i]++;
}
else {
c1[i]++;
}
} res = ;
for (int i = ; i <= la; i++) {
if (a[i] == '') { // 统计跟它比对的1的个数
res += (c1[lb-(la-i)]-c1[i-]);
}
else {
res += (c0[lb-(la-i)]-c0[i-]);
}
}
printf("%lld\n", res);
}
return ;
}

  

codeforces 336 Div.2 B. Hamming Distance Sum的更多相关文章

  1. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  2. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...

  3. Codeforces 608B. Hamming Distance Sum 模拟

    B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  4. 关于前缀和,A - Hamming Distance Sum

    前缀和思想 Genos needs your help. He was asked to solve the following programming problem by Saitama: The ...

  5. Codefroces B. Hamming Distance Sum

    Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...

  6. Codeforces Round #336 Hamming Distance Sum

    题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...

  7. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  8. Codeforces Round #336 (Div. 2)

    水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...

  9. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

    B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 使用DOS比较两个txt文件的差异

    将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异

  2. ADB常用命令(Android Debug Bridge)

    首先要配置环境变量. adb devices 列出所有的设备 adb start-server 开启adb服务 adb kill-server    关闭adb服务 adb logcat    查看L ...

  3. jQuery源码-class操作

    写在前面 本文写作基于jQuery 1.9.1版本,源码分析系列目录:http://www.cnblogs.com/chyingp/archive/2013/06/03/jquery-souce-co ...

  4. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...

  5. CAN总线 SJA1000中断

    背景: 最近一直在使用C8051F340 + SJA1000来实现CAN通信,就SJA1000部分做个记录. 正文: 整个系统结构拓扑图如下: 两路CAN,C8051F340作为CPU,处理CAN与U ...

  6. 2012多校3.A(用O(log(n))判断b^k % a == 0)

    Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  7. 使用APPCAN开发移动应用APP心得

    要想使用APPCAN开发移动应用,首先要弄明白什么是APPCAN,APPCAN都具有哪些功能. 1.什么是APPCAN? APPCAN是正益无线公司开发的一套Hybrid混合应用开发平台(AppCan ...

  8. windows server 2003下安装iis6+php

    参照http://www.myhack58.com/Article/sort099/sort0100/2012/35579.htm 这篇文章,即可! 前 面我写了<windows安装PHP5.4 ...

  9. Android学习笔记(十三)——广播机制

     //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...

  10. [POJ1151]Atlantis

    [POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...