链接:https://codeforces.com/contest/1278

A:Shuffle Hashing

题意:对于一个字符串p可以执行一个”hash”操作,首先将p内的元素随机排列(可能保持原样),随后在p的前面和后面分别加两个字符串(可以为空),组成的新字符串称为h。输入字符串p和h,输出h是否可能是p经过hash操作而来。

例如:输入p = “abacaba”,h = “zyxaabcaabkjh”,输出YES

分析:hash操作分为两步:①随机排列p中的元素②在p的前面和后面分别加两个字符串组成一个新的字符串。那么首先,进行第一步操作之后的p如果记作p’,显然有p中各元素出现的次数与p’中的相等。因此检测另一个字符串是否是p随机排列而来可以使用一个计数数组检测其中各元素出现的次数。其次,②说明p’在h中是连续的一串子字符串,记p长度为plen,h为hlen,则其长度是plen,由此,我们可以遍历h从0位开始一直到hlen-plen位开始的长度为plen的子字符串是否是p随机排列而来。

代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std; int cnt[30];
int main()
{
int T;cin >> T;
while(T--)
{
string p,h;cin >> p >> h;
int plen = p.length(),hlen = h.length();
int ok = -1;
for(int i = 0;i <= hlen - plen;++i)
{
memset(cnt,0,sizeof(cnt));
string tmp = h.substr(i,plen);
for(int j = 0;j < plen;++j)
{
++cnt[tmp[j] - 'a'];
--cnt[p[j] - 'a'];
}
ok = 1;
for(int i = 0;i < 30;++i)
{
if(cnt[i] != 0)
{
ok = 0;
break;
}
}
if(ok == 1)
break;
}
if(ok == 1)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}

B:A and B

题意:给定两个变量a,b。每次选择两者之间的一个数,执行一个操作:将这个数加上i(i = 当前执行的第i步),求至少多少步可以使a和b相等。

分析:在进行了n次操作之后,对于a和b两个数来说,总体上增加的是dsum = n(n+1)/2。但是如果想要把总体增加的和运用到a和b两个数单个身上来说的话就会变得很复杂,因为你没法知道第几步是给a增加的还是b增加的。所以,我们可以从整体上来看,对于a和b而言,他们的和本来是cursum = a+b,进行了n次操作之后会变成cursum + dsum = a’ + b’(此时a‘ == b’),那么就可以从中得到一个重要信息:cursum+dsum由于这个时候a’ == b’,那么cursum + dsum一定是2的倍数,否则不成立,其次,(cursum+dsum)/2也一定>=max(a,b),因为极端的情况就是一边很小,一直在很小的一边加,最后与右边更大的值相等,倘若(cursum + dsum)/2不满足>=max(a,b),则说明虽然此时加的数已经是2的倍数,但是还不足以分成两半使得a和b此时相等。

代码:

#include <iostream>
#include <algorithm> using namespace std; int main()
{
int T;cin >> T;
while(T--)
{
int a,b;cin >> a >> b;
if(a == b)
{
cout << 0 << endl;
continue;
}
int cursum = a + b,n = 1,nsum = 0;
for(;n <= max(a,b)*2;++n)
{
nsum += n;
if(((cursum + nsum) & 1 ) == 0 && ((cursum + nsum) / 2) >= max(a,b))
{
cout << n << endl;
break;
}
}
}
}

Educational Codeforces round 78 A、B的更多相关文章

  1. Educational Codeforces Round 41 B、C、D

    http://codeforces.com/contest/961 B题 可以将长度为k的连续区间转化成1 求最大和 解析 简单尺取 #include <stdio.h> #include ...

  2. Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree

    链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...

  3. Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam

    链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...

  4. Educational Codeforces Round 78 (Rated for Div. 2) B. A and B

    链接: https://codeforces.com/contest/1278/problem/B 题意: You are given two integers a and b. You can pe ...

  5. Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing

    链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...

  6. 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)

    比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...

  7. Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)

    题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...

  8. Educational Codeforces Round 13 A、B、C、D

    A. Johny Likes Numbers time limit per test 0.5 seconds memory limit per test 256 megabytes input sta ...

  9. Educational Codeforces Round 78 (Rated for Div. 2)

    A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...

随机推荐

  1. DateTime相关

    1.string数据转成datetime DateTimeFormatter forPattern = DateTimeFormat.forPattern("yyyyMMddHH" ...

  2. <数据结构系列3>队列的实现与变形(循环队列)

    数据结构第三课了,今天我们再介绍一种很常见的线性表——队列 就像它的名字,队列这种数据结构就如同生活中的排队一样,队首出队,队尾进队.以下一段是百度百科中对队列的解释: 队列是一种特殊的线性表,特殊之 ...

  3. 运行RGB-DSLAM的一些报错及处理方法

    part 4 报错‘create’ is not a menber of 'CV::FeatureDetector::create(detector.c_str()); 查看opencv版本 修改Cm ...

  4. Series的idxmax和argmax

    转载至:https://www.cnblogs.com/liulangmao/p/9211537.html pandas Series 的 argmax 方法和 idxmax 方法用于获取 Serie ...

  5. hive_server2的权限控制

    CDH的core-sit开启: 第一个false表示用系统用户来和hive用户的权限绑定,但经测试并没有生效,所以可以改为true 第二项设置成ALL,表示创建者对其创建的表拥有所有的权限,这样也是比 ...

  6. etcd数据单机部署

    单机下载 版本信息请参考https://github.com/etcd-io/etcd/releases 本次以最新版本3.4.1为例https://github.com/etcd-io/etcd/r ...

  7. Android尺寸适配问题

    1, 布局与组件大小用dp,文字大小用sp 2,

  8. Centos7 升级php版本到php7

    一.首先查看是否有老版本 yum list installed | grep php 二.如果安装的有 yum remove php.x86_64 php-cli.x86_64 php-common. ...

  9. jQuery-点击按钮页面滚动到顶部,底部,指定位置

    $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); //页面滚动至顶部 $('. ...

  10. 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)

    出处:关于 Spring AOP (AspectJ) 你该知晓的一切