题目:

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)的复杂度解决。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define maxn 200005
  4. char a[maxn],b[maxn] ;
  5. int f[maxn][];
  6. int main(){
  7. scanf(" %s %s",a+,b+);
  8. int lena = strlen(a+);
  9. int lenb = strlen(b+);
  10. for(int i = ;i<=lenb;i++){
  11. f[i][] = f[i-][];
  12. f[i][] = f[i-][];
  13. f[i][b[i]-'']++;
  14. }
  15. long long ans = ;
  16. for(int i = ;i<=lena;i++){
  17. int l = i-,r = lenb-lena+i;
  18. if(a[i]=='')
  19. ans += f[r][]-f[l][];
  20. else
  21. ans += f[r][]-f[l][];
  22. }
  23. printf("%lld\n",ans);
  24. return ;
  25. }

Codeforces Round #336 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. 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 ...

  5. Codeforces Round #336 (Div. 2)

    水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...

  6. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  7. 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 ...

  8. Codeforces 608 B. Hamming Distance Sum-前缀和

      B. Hamming Distance Sum   time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

随机推荐

  1. js002-在HTML中使用JavaScript

    js002-在HTML中使用JavaScript 2.1            <script>元素   定义了以下6个属性   async: 可选.表示应该立即下载脚本,但不妨碍页面中的 ...

  2. 表x有 一列 ,程序每次生成id的时候都先从这里获取最大值再加1,初始值是A0001,然后到A9999的时候则是到B0001 共5位

    drop table x gocreate table x(id varchar(10))--insert into x values('A001')gowith a as (select ISNUL ...

  3. spring boot properties

    [转载] 代码从开发到测试要经过各种环境,开发环境,测试环境,demo环境,线上环境,各种环境的配置都不一样,同时要方便各种角色如运维,接口测试, 功能测试,全链路测试的配置,hardcode 肯定不 ...

  4. Ztree的简单使用和后台交互的写法(二)

    针对Ztree的简单使用和后台交互的写法(一)中的树进行改进 1.增加节点的权限 由页面的当前用户,决定树的根节点 然后动态获取树的详细节点: 初始化函数为: function init(){ //初 ...

  5. easyUI中tree的简单使用

    一.在JS中的代码 $('#tt').tree({ url: baseCtx + 'lib/easyui-1.4/demo/tree/tree_data1.json',//tree数据的来源,json ...

  6. Oralce配置正确,报监听错误或无法识别描述中的服务

    出差客户现场,修改过网络配置,回来后本地虚拟机的Oracle数据库就不能登陆了 报监听错误,在服务器中使用Net Configration Assistant删除以前的,重新配置新的,还是不行,重启系 ...

  7. Tomcat 用户访问控制

    要设置Tomcat下的Host都有哪些ip能访问,可以在conf/server.xml的相应Host元素内加Value标签,示例: <Valve className="org.apac ...

  8. 入门:HTML:hello world!

    <html> <head> </head> <body> <h1>hello world!</h1> </body> ...

  9. Photoshop 融合属性 Unity Shader

    http://forum.unity3d.com/threads/free-photoshop-blends.121661/

  10. phpstorm webstorm安装主题 sublime样 还有都可以用的注册码

    注册码 webstorm phpstorm 基本所有版本通吃  webstrom9.0.3 通过 phpstorm 8.0.1 User Name: EMBRACE License Key: ==== ...