题目链接: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. java项目命名规范

    一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...

  2. 关于Mysql错误:./bin/mysqld_safe --user=mysql& [1] 32710 121003 16:40:22 mysqld_safe Logging to '/var/log/mysqld.log'. 121003 16:40:22 mysqld_s

    [root@www]# ./bin/mysqld_safe --user=mysql&[1] 32710[root@www]# 121003 16:40:22 mysqld_safe Logg ...

  3. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  4. Linux下Redis常用命令

    >src/redis-server  启动 Redis 服务  或者>src/redis-server redis.conf src/redis-server redis.conf 1&g ...

  5. javascript自定义滚动条插件,几行代码的事儿

    在实际项目中,经常由于浏览器自带的滚动条样式太戳,而且在各个浏览器中显示不一样,所以我们不得不去实现自定义的滚动条,今天我就用最少的代码实现了一个自定义滚动条,代码量区区只有几十行,使用起来也非常方便 ...

  6. 2013长沙赛区现场赛 J - Josephina and RPG

    J - Josephina and RPG Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  7. taglib

    thinkphp中 taglib标签应用 原文出处:http://blog.csdn.net/a11085013/article/details/38172653 1.配置文件中加上: 'APP_AU ...

  8. block 块函数

    定义模块函数: <?php function smarty_block_text($args,$content,$smarty,$a) { $color=$args["color&qu ...

  9. Lua模块测试

    Lua模块 ---------------------------------------------------------- ----------------------- 模块测试module_ ...

  10. Nginx初学者指南

    Starting, Stopping, and Reloading Configuration To start nginx, run the executable file. Once nginx ...