Codeforces Round #769 (Div. 2)

这篇博客分享了三道来自Codeforces Round #769 (Div.2)的比赛题目解题思路和AC代码。题目包括字符串排序与回文子串判断、数字异或构建屋顶和复杂逻辑测试。博主通过分析每个问题的核心,给出了简洁的解决方案,并对自己的思考过程进行了反思和总结。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.A - ABC-Codeforces Round #769 (Div. 2)

题意:

给出一个01字符串,你可以将他重新排序,问排序后的字符串的长度大于1的子串是否没有回文串。

题解:

长度为1,肯定输出yes。长度为2单独判断。当长度大于等于三时,长度等于三包括000,001,010,011,100,101,110,111,可以看到都是回文,那长度大于三也肯定有子串是回文的。

总结:

子串的问题就拆分成小的子串观察。开始的时候没有单独判断长度为2的时候。

AC代码:

#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <math.h>
#include <iomanip>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
 
using namespace std;
#define ll long long
 
int n,m;
 
int main()
{
//    freopen("1.txt","r",stdin);
    int t;
    int i1,i2,i3,i4,i5;
    string x;
    cin>>t;
    while(t--)
    {
        cin>>n;
        cin>>x;
        if(n<=1) {cout<<"YES"<<endl;continue;}
        if(n==2) {
                if(x[0]!=x[1]) cout<<"YES"<<endl;else cout<<"NO"<<endl;continue;}
        else cout<<"NO"<<endl;
    }
    return 0;
}

2.B - Roof Construction-Codeforces Round #769 (Div. 2)

题解:

0-n-1,计算出n-1表达成2进制最高位是多大,然后先把最高位是1的都写出来,然后从0依次向后写就可以了。

解释:

无论怎么排序,异或后最大的最小肯定要大于等于2进制的最高位。

总结:

开始的时候只把n-1单独处理了,没有想到最大位不止有n-1。

AC代码:

#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <math.h>
#include <iomanip>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
 
using namespace std;
#define ll long long
int n,m;
 
int main()
{
//    freopen("1.txt","r",stdin);
    int t;
    int i1,i2,i3,i4,i5;
    cin>>t;
    while(t--)
    {
        cin>>n;
        i1=1;
        while(i1<=n-1)
            i1<<=1;
        i1>>=1;
        i2=n-1;
        for(i2;i2>=i1;i2--)
        {
            cout<<i2<<" ";
        }
        for(i3=0;i3<i1;i3++)
        {
            cout<<i3<<" ";
        }
        cout<<endl;
    }
    return 0;
}

3.C - Strange Test-Codeforces Round #769 (Div. 2)

题解:

(网上题解很多,都很详细大家还是去看别人的题解吧)假设只用第一种和第二种处理后形成的分别是a1,b1。ans=a1-a+b1-b+(a1|b1)-b1+1。a1-a和b1-b进行第一种和第二种操作的次数。(a1|b1)-b1是进行或操作后,还需要进行的第二种操作,加一就是那一次或运算。只有a1和b1是变量,之后就用贪心来求解。a1很容易知道范围(作为循环变量),对于每一个a1都有一个确定的b1使得当前a1进行操作后步骤最少,那怎么求解b1就很麻烦了,就不写了。

总结:

这题看了网上很多题解一共有两种方法,一种就是我写的这种比较难懂。另一种代码很简单,但是想了很久都没想明白,分别固定了b和a,?为什么要固定a?当时想了很久这第二种方法,难道是巧了,不不-_-||,是我太菜了。

AC代码:

/**tijie
 */
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <math.h>
#include <iomanip>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
 
using namespace std;
#define ll long long
int n,m;
 
int main()
{
    //freopen("1.txt","r",stdin);
    int t;
    int i1,i2,i3,i4,i5;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int ans=m-n;
        for(int a1=n;a1<=m;a1++)
        {
            int b1=0;
            for(int j=21;j>=0;j--)
            {
                if((m>>j)&1) {
                    b1+=(1<<j);
                }else
                {
                    if((a1>>j)&1)
                    {
                        b1+=(1<<j);
                        break;
                    }
                }
            }
            ans=min(ans,a1+(a1|b1)+1-n-m);
        }
        cout<<ans<<endl;
    }
    return 0;
}

今天是2月8号,哈😁哈🎂🎉👏😑😐🤨😏

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值