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 ...
随机推荐
- 关于R中的mode()和class()的区别
本文原创,转载请注明出处,本人Q1273314690(交流学习) 说明:本文曾经在15年11月在CSDN发过,但是由于CSDN不支持为知笔记的发布为博客的API功能,所以,自今天起,转移到博客园(幸好 ...
- bzoj1179 [Apio2009]Atm
Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口 ...
- 2012Chengdu B (快速组合数)
B - Candy Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- OC第九节——协议与代理
一.理解协议与代理 协议: 协议就是需要相互遵守的约定.规范:需要去实现协议中规定的方法. 代理: 代理是一个概念,很难用一个名词去定义(如我们可以说协议其实就是一个方法列表).它更像是一种关系,我要 ...
- LoadRunner监控Linux
rstat协议允许网络上的用户获得同一网络上各机器的性能参数. 需要下载3个包: (1)rsh-0.17-14.i386.rpm (2)rsh-server-0.17-14.i386.r ...
- Javascript高级程序设计——函数声明与函数表达式的区别
在Javascript中,函数是Functioin类型的实例,所以函数也具备属性和方法,因为函数是对象,所以函数名自然就是指向对象的指针啦. 函数可以通过声明语法和表达式来定义: 声明:functio ...
- java网络osi基础
- job_queue_processes参数讲解
http://blog.sina.com.cn/s/blog_62defbef0101opv0.html http://blog.163.com/donfang_jianping/blog/stati ...
- SpringMVC基础入门
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- linux下VMware安装出现的问题解决
linux下VMware安装出现的问题解决 linux下VMware安装出现的问题解决 报错信息 问题1liboverlay-scrollbar.so和libunity-gtk-module.so加载 ...