题目链接: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. MySQL创建一个用户,指定一个数据库 授权

    Mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -ppassworduse mysql;insert into user(h ...

  2. JavaScript 学习笔记 -- Function

    JS 中 函数.继承.闭包.作用域链... 一直都是硬伤,一碰到这样的问题头就大了.但是如果我继续着说:我不会,就真的无药可救了.要勇敢地说出:我的字典里就没有不会这个词,吼吼..正好昨天在书城里看了 ...

  3. getStyle(),修改样式属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Mastering Web Application Development with AngularJS 读书笔记(三)

    第一章笔记 (三) 一.Factories factory 方法是创建对象的另一种方式,与service相比更灵活,因为可以注册可任何任意对象创造功能.例如: myMod.factory('notif ...

  5. [译]git checkout

    git checkout git checkout提供3种不同的功能: checking out文件, checking out commits, checking out branch. check ...

  6. 必须知道的.net(字段、属性和方法)

    1.字段 通常定义为private(封装原则) 2.属性(property) 通常定义为public,表示类的对外成员.具有可读可写属性,通过get和set访问器实现 3.索引器(indexer) C ...

  7. JQuery-EasyUI DataGrid CRUD

    ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页! 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号. ...

  8. SqlServer中字符串和日期类型的转换

    SQL Server Date 函数 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(da ...

  9. JSP中根据不同的条件显示不一样的格式

    在做项目中遇到这样的场景: 当查询到记录时,需要将记录的字段作为下拉列表,让用户选择使用,即显示的是下拉列表. 当没有查询到记录时,则让用户手工填写该值,即显示的是文本框. 前段jsp使用if标签如下 ...

  10. Ubuntu 14 常用“快捷键”,Ctrl + Alt + F1 进入终端,按 Ctrl + Alt + F7 回到界面

    Ubuntu中所谓 Super键,就是 Windows建,一般在键盘的 ctrl 和 alt 2个键之间,一个微软窗口的图标. 1.持续按住 Super键,会弹出“键盘快捷键”大全: 2.修改快捷键路 ...