Educational Codeforces Round 78 (Rated for Div. 2)——A、B题简要题解

题目传送门:https://codeforces.com/contest/1278/problem/A
题目传送门:https://codeforces.com/contest/1278/problem/B

A. Shuffle Hashing(思维+暴力)

题目大意:

给你两个字符串,看第二个字符串中是否包含第一个字符串或者其相似串,相似串的定义是:两个字符串仅仅是字符的顺序不相同,其余均相同。

题目分析:

记录字符串1的长度为len1,字符串2的长度为len2。将字符串1先进行排序,然后将字符串2的1—len1位排序,与排序好的字符串1比较,如果两个字符串不同,就整体后移一位,将字符串2的2——len1+1位排序,然后再逐一比较,直至找出完全相同的为止。如果遍历到字符串2的最后一位时仍没有找到和排序好的字符串1完全相同的串,那就说明字符串2中不包含字符串1或其相似串。

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#include<string> 
#include<vector>
#include<stack>
#include<queue> 
#define PI 3.1415926
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) 
{
    if (b == 0) return a;
    else gcd(b, a % b);
}
char s1[105],s2[105];
 
int main()
{
    int t;
    cin>>t;
    getchar();
    while(t--){
        scanf("%s",&s1);
        scanf("%s",&s2);
        int len_1,len_2;
        len_1=strlen(s1);
        len_2=strlen(s2);
        sort(s1,s1+len_1);
        if(len_1>len_2){
            printf("NO\n");
            continue;
        }
        bool flag;
        for(int i=0;i<=len_2-len_1;i++)
        {
            flag=1;
            char t[105];
            strcpy(t,s2);
            sort(t+i,t+i+len_1);
            for(int j=0;j<len_1;j++){
                if(s1[j]!=t[i+j]){
                    flag=0;
                    break;
                }
            }
            if(flag)
                break;
        }
        if(flag)
			printf("YES\n");
        else
			printf("NO\n");
    }
    return 0;
}

B. A and B(思维)

题目大意:

给你两个数A、B,第i轮你能选择其中任何一个数加上i,问最小需要经过多少轮才能使这两个数相等?

题目分析:

我们知道,假如经过n轮后AB相等,那么所加上的数一定为1+2+3+……+n。设两个数的差为d,1+2+3+……+n是一个等差数列,它的和sum为(1+n)*n/2,当这个sum大于等于d且sum的奇偶性和d相同时,此时对应的n即为答案。下面具体说明一下奇偶性相同的原因,假如d是个奇数,sum也是个奇数,奇数-奇数=偶数,正好填补了AB的差后还能将剩余的平均分给AB,偶数也是同样的道理。

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#include<string> 
#include<vector>
#include<stack>
#include<queue> 
#define PI 3.1415926
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) 
{
    if (b == 0) return a;
    else gcd(b, a % b);
}
char s1[105],s2[105];
 
int main()
{
    int t;
    cin>>t;
    ll x=0,y=0,ans=0,sum=0; 
    while(t--){
        cin >> x >> y;
		if (x == y)
		{
			cout << 0 << endl;
		}
		else
		{
			ll d = abs(x - y);
			ll ans = 1;
			while (true)
			{
				ll sum = ans*(ans + 1) >> 1;
				if (sum >= d && (sum & 1) == (d & 1))
				{
					cout << ans << endl;
					break;
				}
				ans++;
			}
    	}
	}
    return 0;
}

小心得:

虽然是div2,但是感觉有的题还是有一些难度,这两道题自己写的话真的不一定写的出来,再参考了网上大佬们的思路后才AC的,路漫漫其修远兮,继续努力吧!争取寒假拜托灰名!干就完了,奥力给!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值