Skip to main content
  1. Posts/

codeforces #336 div 2 B. Hamming Distance Sum

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/608/problem/B 题意:给定两个字符串a,b,问b中的每个连续的长度为a的子串与a的哈密顿距离的和是多少。哈密顿距离是对应位置的字符的差的绝对值的和。由于是01串,也就是字符不同的位置数。 思路:类似前缀和。0和1分别搞。注意开long long

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月24日 星期四 00时32分33秒
 4File Name :code/cf/#336/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=3E5+7;
34int la,lb;
35char a[N],b[N];
36int sum0[N],sum1[N];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42	ms(sum0,0);
43	ms(sum1,0);
44	scanf("%s",a);
45	scanf("%s",b);
46	la = strlen(a);
47	lb = strlen(b);
48	int sum = 0 ;
49	if (la==1)
50	{
51	    for ( int i = 0 ;  i < lb ; i++)
52	    {
53		if (b[i]!=a[0]) sum++;
54	    }
55	    cout<<sum<<endl;
56	    return 0;
57
58	}
59
60	for ( int i = 0 ; i < lb ; i++)
61	{
62	    if (b[i]=='0')
63	    {
64		sum0[i+1] = sum0[i]+1;
65		sum1[i+1] = sum1[i];
66	    }
67	    else
68	    {
69		sum1[i+1] = sum1[i] +1;
70		sum0[i+1] = sum0[i];
71	    }
72	}
73//	for ( int i = 1 ; i <= lb ; i++)
74//	    cout<<sum0[i]<<" "<<sum1[i]<<endl;
75	LL ans =  0;
76	for (int i= 0 ;  i < la ; i++)
77	{
78	    if (a[i]=='0')
79	    {
80		ans+=LL(sum1[lb-la+i+1]-sum1[i]);
81	    }
82	    else
83	    {
84		ans+=LL(sum0[lb-la+i+1]-sum0[i]);
85	    }
86//	    cout<<ans<<endl;
87	}
88
89	cout<<ans<<endl;
90
91  #ifndef ONLINE_JUDGE
92  fclose(stdin);
93  #endif
94    return 0;
95}

Related

codeforces #336 div 2 A. Saitama Destroys Hotel

·1 min
http://codeforces.com/contest/608/problem/A 题意:一个电梯,从s到0层,单项,给出n个人,每个人在某时间出现在某层,问要花多长时间把所有人运到0。初始电梯在s,每下一层话费时间1,上来人不花时间。 思路:由于电梯只能是单项,那么到达某一层的时候,一定要等到把这层中最晚来的那个接走。所以排序的时候按照楼层高到低为第一关键字,等待时间长到短为第二关键字。对于同一楼层出现的,只考虑第一个即可,也就是最后出现的。需要注意的是最后接完最后一个人以后把电梯运行道0层。

codeforces #332 div 2 D. Spongebob and Squares

·2 mins
http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的时候gg了。。其实稍微在纸上推一下。就会得到对于n,m的矩形,一共会有-nnn+3nnm+n+3n*m的方格。数量级是n3。 我们可以实际跑一遍。发现对于x1E18的数量级,n不会超过1442550,1E6,可以搞。

codeforces #333 div 2 D. Lipshitz Sequence

·3 mins
http://codeforces.com/contest/602/problem/D 题意:见题目描述。 思路:我们很容易发现,l[h]函数其实就是在求区间斜率的最大值。哦不对,是斜率的绝对值的最大值。而且一个显而易见的结论是,斜率最大值一定是由相邻的点得到。画图可以很容易看出。

codeforces #333 div 2 C. The Two Routes

·2 mins
http://codeforces.com/problemset/problem/602/C 题意:给出n个城镇,m条双向铁路,对于任意不同的x,y,如果x,y之间没有铁路,那么一定有双向公路。train只能走铁路,bus只能走公路。现在一辆火车和一辆bus同时从1出发,要到达n,处于安全考虑,bus和火车不能同时处在除了n以外的点。bus和train不要求同时到达。任意一段道路的时间花费都是1小时。问最少需要多久使得bus和train都到达n。如果存在某个不能到达,那么输出-1. 思路:n才400.一开始打算先按照rail和road建两个图。这两个图互为补。然后在floyd的时候加以判断。但是马上就发现。。不能同时到达同伙一个点这个条件其实不会影响。。因为按照题意,一定存在一条1到n的路,不是公路就是铁路。那么就让有路的花费1的代价到n,然后剩下的求一个一到n的最短路即可。由于n才400.。最短路怎么搞都行。。我偷懒就用floyd了。