Codeforces Round #336 Hamming Distance Sum
题目:
http://codeforces.com/contest/608/problem/B
字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00、01、11、11从左到右挨个比较,希望能找到一些规律,结果并没有。。。
其实,如果我们能从整个比较过程来看这个问题,整个过程就没有那么难。题目要求的东西,其实就是a字符串和b字符串子串每次比较的不同的个数的总和,当我们像上面那个思路,拿a字符串每次移动一位,和b进行比较的时候,从整个过程来看,就相当于a的每一个元素从前往后和b比较不同的个数,再把每个元素的不同个数相加。
a中元素与b中比较并不是完全从前往后比,而是,对于a中第i个元素来说,他在b中也是从i个位置开始比较。比较区间的右端是len_b-len_a+i,这是因为,a与b最后一个子串的比较,一定是从len_b-len_a这个位置开始到最后,加上i就是比较到最后一个子串中i的位置。
另外,这道题用到了前缀和的技巧,就可以用O(a+b)的复杂度解决。
#include<stdio.h>
#include<string.h>
#define maxn 200005
char a[maxn],b[maxn] ;
int f[maxn][];
int main(){
scanf(" %s %s",a+,b+);
int lena = strlen(a+);
int lenb = strlen(b+);
for(int i = ;i<=lenb;i++){
f[i][] = f[i-][];
f[i][] = f[i-][];
f[i][b[i]-'']++;
}
long long ans = ;
for(int i = ;i<=lena;i++){
int l = i-,r = lenb-lena+i;
if(a[i]=='')
ans += f[r][]-f[l][];
else
ans += f[r][]-f[l][];
}
printf("%lld\n",ans);
return ;
}
Codeforces Round #336 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 ...
- 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) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- 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 ...
- Codeforces 608 B. Hamming Distance Sum-前缀和
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
随机推荐
- infobright 导入 导出
SET SQL_LOG_BIN = 0;select * into outfile 'mydata.cvs' fields terminated by ',' from table1;load dat ...
- GSON使用笔记(1) -- 序列化时排除字段的几种方式
http://blog.csdn.net/zxhoo/article/details/21471005 GSON是Google发布的JSON序列化/反序列化工具,非常容易使用.本文简要讨论在使用GSO ...
- POJ3254Corn Fields(状态压缩DP入门)
题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...
- shell 中 &&和||的方法
Shell && 和 || shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? == 1 时 ...
- liunx 的 grep命令(转载)
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- 非阻塞socket学习,select基本用法
server #include <stdio.h> #include <winsock2.h> #include <iostream> #pragma commen ...
- OBJ Loader Source Code
https://github.com/ChrisJansson/ObjLoader http://www.codeproject.com/Articles/798054/SimpleScene-d-s ...
- wex5 开机图片时间长
作用: 控制刚打开图片 时间长 修改config.xml 地址:F:\wex\model\Native\templates\advanced 延迟的时间是在本地app的 config.xml中修改, ...
- $().index() 两种用法
第一种:获得第一个 p 元素的名称和值: $(this).index() <script type="text/javascript"> $(document).rea ...
- 使用wp_editor函数实现可视化编辑器
在最近的wp项目中遇到了需要使用可视化编辑器来接收用户的输入,正好就研究了一下wp_editor这个函数的用法,利用这个函数能很方便的把textarea文本域变成可视化编辑器. Wp_editor函数 ...