AtCoder Beginner Contest 322

A - First ABC 2

AC代码:

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
//#define int long long
using namespace std;
int n;
string s;
void solve() {
    cin>>n>>s;
    s=' '+s;
    for(int i=1;i<=n-2;i++){
        if(s[i]=='A'&&s[i+1]=='B'&&s[i+2]=='C'){
            cout<<i<<endl;
            return;
        }
    }
    cout<<-1<<endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
//    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}

B - Prefix and Suffix

AC代码:

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
//#define int long long
using namespace std;
int n,m;
string s,t;
void solve() {
    cin>>n>>m;
    cin>>s>>t;
    bool ok1=true;
    for(int i=0,j=0;i<n;i++){
        if(s[i]!=t[j++]){
            ok1=false;
            break;
        }
    }
    bool ok2=true;
    for(int i=n-1,j=m-1;i>=0;i--){
        if(s[i]!=t[j--]){
            ok2=false;
            break;
        }
    }
    if(ok1&&ok2) cout<<0<<endl;
    else if(ok1&&!ok2) cout<<1<<endl;
    else if(!ok1&&ok2) cout<<2<<endl;
    else cout<<3<<endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
//    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}

C - Festival

AC代码:

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
//#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n,m;
void solve() {
    cin>>n>>m;
    for(int i=1;i<=m;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        int id=lower_bound(a+1,a+1+m,i)-a;
        cout<<a[id]-i<<endl;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
//    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}

D - Polyomino

暴力

图形的旋转平移

枚举三个图案旋转平移的所有情况,通过状态压缩,转化为一个二进制串,0~15位,如果该格为#,那么就为1

三个图案的二进制串按位或,如果等于2^(SIZE*SIZE)-1,那么说明三个图案把正方形填满了,如果任意两个图案二进制串按位与不等于0,那么说明图案有重叠

AC代码:

cpp 复制代码
#include<bits/stdc++.h>
#define SIZE 4
#define endl '\n'
//#define int long long
using namespace std;
//逆时针旋转90度
void rotate(string s[]){
    char temp[SIZE][SIZE];//中间变量
    for(int i=0;i<SIZE;i++){
        for(int j=0;j<SIZE;j++){
            temp[SIZE-j-1][i]=s[i][j];
        }
    }
    for(int i=0;i<SIZE;i++){
        for(int j=0;j<SIZE;j++){
            s[i][j]=temp[i][j];
        }
    }
}
//判断合法性
bool valid(int x){
    return x>=0&&x<SIZE;
}
//平移
int get(string s[],int dx,int dy){
    int ret=0;//ret存储二进制串
    for(int x=0;x<SIZE;x++){
        for(int y=0;y<SIZE;y++){
            if(s[x][y]=='#'){
                int tx=x+dx;
                int ty=y+dy;
                if(!valid(tx)||!valid(ty)) return -1;
                ret|=1<<(tx*4+ty);
            }
        }
    }
    return ret;//返回图案所对应的二进制串
}
vector<int>add(int id){
    vector<int>ret;
    string s[SIZE];
    for(int i=0;i<SIZE;i++) cin>>s[i];
    for(int i=0;i<4;i++){
        for(int dx=-3;dx<=3;dx++){
            for(int dy=-3;dy<=3;dy++){
                int v=get(s,dx,dy);
                if(v>=0) ret.push_back(v);
            }
        }
        rotate(s);
    }
    return ret;
}
void solve() {
    vector<int>mask[3];
    for(int id=0;id<3;id++) mask[id]=add(id);
    for(int x:mask[0]){
        for(int y:mask[1]){
            for(int z:mask[2]){
                if((x|y|z)!=(1<<SIZE*SIZE)-1) continue;
                if(x&y) continue;
                if(x&z) continue;
                if(y&z) continue;
                cout<<"Yes"<<endl;
                return;
            }
        }
    }
    cout<<"No"<<endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
//    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}
相关推荐
偷偷的卷22 分钟前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
北京地铁1号线28 分钟前
Zero-Shot(零样本学习),One-Shot(单样本学习),Few-Shot(少样本学习)概述
人工智能·算法·大模型
大白的编程日记.35 分钟前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
C语言小火车41 分钟前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针
凤年徐1 小时前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
kualcal1 小时前
代码随想录17|二叉树的层序遍历|翻转二叉树|对称二叉树
数据结构·算法
踏莎行hyx1 小时前
使用langchain连接llama.cpp部署的本地deepseek大模型开发简单的LLM应用
c++·ai·langchain·大模型·llama.cpp·deepseek
山河木马1 小时前
前端学C++可太简单了:双冒号 :: 操作符
前端·javascript·c++
满分观察网友z2 小时前
从混乱到有序:我用“逐层扫描”法优雅搞定公司组织架构图(515. 在每个树行中找最大值)
后端·算法
满分观察网友z2 小时前
一行代码的惊人魔力:从小白到大神,我用递归思想解决了TB级数据难题(3304. 找出第 K 个字符 I)
后端·算法