Educational Codeforces round 78 A、B
链接: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的更多相关文章
- Educational Codeforces Round 41 B、C、D
http://codeforces.com/contest/961 B题 可以将长度为k的连续区间转化成1 求最大和 解析 简单尺取 #include <stdio.h> #include ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)
比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...
- 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- ...
- 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 ...
- Educational Codeforces Round 78 (Rated for Div. 2)
A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...
随机推荐
- Metinfo5.1 /message/access.php SQL注入漏洞
- mysql——单表查询——其它整理示例00
), sname ), sage ), ssex ) ); ','zhaolei','1990-01-01','nan'); ','qiandian','1990-12-21','nan'); ',' ...
- css样式,媒体查询,垂直居中,js对象
下面是一些截图,有关查询效率,css样式,媒体查询,垂直居中,js基本类型.
- python 并发编程 阻塞IO模型
阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 当用户进程调用了recvfrom这个系统调用,kernel内核就 ...
- C语言作业09
问题 答案 这个作业属于那个课程 C语言程序设计 这个作业要求在哪里 https://i.cnblogs.com/EditPosts.aspx?opt=1 我在这个课程的目标是 在学好C语言编程的基础 ...
- Vue 子组件,向父组件传递。
- 在Ueditor的内容区添加一个下拉选框改变事件
<script>html='';html=html+` <select name="" onchange='this.appendChild(document.g ...
- 使用Jenkins结合Gogs和SonarQube对项目代码进行测试、部署、回滚,以及使用keepalived+haproxy调度至后端tomcat
0 环境说明 主tomcat:192.168.0.112 备tomcat:192.168.0.183 haproxy+keepalived-1:192.168.0.156 haproxy+keepal ...
- RabbitMQ入门教程(六):路由选择Routing
原文:RabbitMQ入门教程(六):路由选择Routing 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...
- 9.jQuery之简洁版滑动下拉菜单
知识点:hover的使用,已经slideToggle的切换效果 <style> * { margin: 0; padding: 0; } li { list-style-type: non ...