codeforces 336 Div.2 B. Hamming Distance Sum
题目链接: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的更多相关文章
- Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和
B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...
- 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 ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- 关于前缀和,A - Hamming Distance Sum
前缀和思想 Genos needs your help. He was asked to solve the following programming problem by Saitama: The ...
- Codefroces B. Hamming Distance Sum
Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...
- Codeforces Round #336 Hamming Distance Sum
题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...
- 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 ...
- Codeforces Round #336 (Div. 2)
水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...
- 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 ...
随机推荐
- 时间处理工具类DateUtils
public class DateUtils { public static final String SHORT_DATE ...
- 【Solr】数据库数据导入索引库
目录 分析框图 配置数据库与solrconfig.xml 回到顶部 分析框图 框图画的粗糙!勿喷啊!勿喷啊! 回到顶部 配置数据库与solrconfig.xml Dataimport插件 可以批量把数 ...
- Codeforces Round #270 1002
Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...
- ThinkPHP魔术方法
我们在使用thinkphp开发系统的时候,有时候会用到getById('1')这个方法快速的获取一条信息的内容,比用where(" id =1 ")->find();好用多了 ...
- python入门基础代码
#查找index函数的帮助 help(str.index) #for循环和break语句from math import sqrtfor i in range(2,101): flag=1 k=int ...
- 一次失败的动态转换bean的尝试与思考
前因 公司规范确定不允许使用反射类的进行属性bean的拷贝了,只允许手动的get/set,可以猜到这样定义的原因是制定规范的同事认为反射性能低,虽然写get/set慢点好在性能高.平时开发的时候也是迫 ...
- Memcached和Redis对比和适用场景
关于memcached和redis的使用场景,根据大神们的讨论和我在网上查到的资料,总结一下: 两者对比: redis提供数据持久化功能,memcached无持久化: redis的数据结构比memca ...
- iOS开发网络篇—大文件的多线程断点下载
http://www.cnblogs.com/wendingding/p/3947550.html iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了同时 ...
- linux下使用rdp
简单的说就是在linux下如何远程终端连接一台windows的服务器. 在windwos下我们直接可以mstsc开启远程终端的连接.而linux下呢.就需要安装一款工具了. 命令:sudo apt-g ...
- 剑指Offer 栈的压入、弹出序列
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...