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很大呢~

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 200000 + 5;
 8 char a[maxn], b[maxn];
 9 int c0[maxn], c1[maxn];
10
11 long long res;
12
13 int main()
14 {
15     #ifndef ONLINE_JUDGE
16         freopen("in.txt", "r", stdin);
17     #endif // ONLINE_JUDGE
18
19     while (scanf("%s%s", a+1, b+1) != EOF) {
20         int la = strlen(a+1);
21         int lb = strlen(b+1);
22
23         memset(c0, 0, sizeof(c0));
24         memset(c1, 0, sizeof(c1));
25
26         for (int i = 1; i <= lb; i++) {
27             c1[i] = c1[i-1];
28             c0[i] = c0[i-1];
29
30             if (b[i] == ‘0‘) {
31                 c0[i]++;
32             }
33             else {
34                 c1[i]++;
35             }
36         }
37
38         res = 0;
39         for (int i = 1; i <= la; i++) {
40             if (a[i] == ‘0‘) {   // 统计跟它比对的1的个数
41                 res += (c1[lb-(la-i)]-c1[i-1]);
42             }
43             else {
44                 res += (c0[lb-(la-i)]-c0[i-1]);
45             }
46         }
47         printf("%lld\n", res);
48     }
49     return 0;
50 }

  

时间: 2024-10-24 02:11:01

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

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 problem by Saitama: The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si i

关于前缀和,A - Hamming Distance Sum

前缀和思想 https://blog.csdn.net/chen_yuazzy/article/details/77074410 #include<cstdio>#include<cstring>#define m 300000using namespace std;char a[m];char b[m];int pre0[m];int pre1[m];int main(){    int len1,len2,i;    long long s=0;    gets(a);   

hdu 4712 Hamming Distance 随机

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hammi

Codeforces #250 (Div. 2) C.The Child and Toy

之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小,肯定拆相应权值较小的 在这个基础上考虑问题就能够了 代码例如以下: #include <cstdio> #include <iostream> #include <algorithm> #define MAXN 10010 #define ll long long usi

HDU 4217 Hamming Distance 随机化水过去

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1569    Accepted Submission(s): 616 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 100001 int a[N],b[N]; int sum[N],wh[N][2]; int num1[N],num2[N]; void read(int &x) { x=0; char c=getc

461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

HDU 4712 Hamming Distance (随机函数)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

UVa 729 - The Hamming Distance Problem

题目:构造n位01串,其中有m个1的所有组合. 分析:搜索.枚举.可以利用库函数,求解,也可以利用dfs求解:我这里采用位运算计算组合数. 说明:注意库啊! #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int S[20]; int main() { int T,N,M; while ( cin >> T ) for ( int t = 1 ; t