码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 608B

时间:2017-07-06 10:10:32      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:blank   com   log   .com   targe   bsp   ref   相同   前缀   

CodeForces - 608B

 

题意是:有字符串a和b(由0和1组成),a的长度小于等于b,求b中每个和a长度相同的子串的差值和,差值计算相同为0,不同为1,比如0011和1101的差值是3。

赛时没有想到前缀和,这题有前缀和就可以解决了。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int MAX = 2e5+10;
 5 char s[MAX], ss[MAX];
 6 int a[MAX];
 7 int main(){
 8     scanf("%s",s);
 9     scanf("%s",ss);
10     int len = strlen(s), len2 = strlen(ss);
11     for(int i = 0; i < len2; i ++){
12         if(ss[i] == 1)
13             a[i+1] ++;
14         a[i+1] += a[i];
15     }
16     ll ans = 0;
17     int k = len2-len+1;
18     for(int i = 0; i < len; i ++){
19         if(s[i] == 1) ans += k-a[k+i]+a[i];
20         else ans += a[k+i]-a[i];
21     }
22     cout << ans << endl;
23     return 0;
24 }

 

CodeForces - 608B

标签:blank   com   log   .com   targe   bsp   ref   相同   前缀   

原文地址:http://www.cnblogs.com/xingkongyihao/p/7124424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!