链接: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. springboot mybatis下临时表的创建和删除,可用于查重去重

    /** * 创建临时表 */ @Update({"drop temporary table if exists ${tableName};", "create tempo ...

  2. [ASP.NET] 解决点击控件下载文件没有响应的问题

    下载文件的方法是使用http响应输出流来实现的,使用到了response.write() 导致下载文件时点击控件出错,没有响应,也获取不了文件 是因为在母版页使用了updatepanel,因此回传时发 ...

  3. java集群技术(转)

    序言 越来越多的关键应用运行在J2EE(Java 2, Enterprise Edition)中,这些诸如银行系统和账单处理系统需要高的可用性(High Availability, HA),同时像Go ...

  4. 如何根据对象的属性,对集合(list / set)中的对象进行排序

      一:针对list 通过java.util.Collections的sort方法,有2个参数,第一个参数是list对象,第二个参数是new Comparator<对象类>(){}方法,这 ...

  5. Element el-table-column组件列宽度设置百分比无效

    问题 使用Element table组件时,给列设置百分比宽度无效(width="30%") 解决 用属性min-width="3"代替属性width=&quo ...

  6. springBoot2.0配置profile

    1. 使用yaml来配置,直接配置application.yml文件 server: port: 8888 spring: profiles: active: dev # 激活生产环境 --- # 测 ...

  7. mysql常用的基本命令

    一.基本命令 1.启动服务(以管理员身份进入cmd): 格式:net start 服务名称 示例:net start mysql 2.停止服务(以管理员身份进入cmd): 格式:net stop 服务 ...

  8. Git+码云安装

    注册码云 1.1 下载git https://git-scm.com 1.2 安装 git安装一直next 下一步就行 1.3 测试 1.4 git原理

  9. pyrhon 开始 基础类型

    https://repl.it/languages/python  线上编辑器 字符串不支持  减法 除法

  10. echarts图标使用(一)

    var data = []; // Parametric curve // for (var t = 0; t < 25; t += 0.001) { // var x = (1 + 0.25 ...