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;
}
相关推荐
炽烈小老头7 分钟前
【每日天学习一点算法 2026/04/27】缺失的第一个正数
学习·算法
handler018 分钟前
Linux 进程探索:从 PCB 管理到 fork() 的写时拷贝
linux·c语言·c++·笔记·学习
南宫萧幕28 分钟前
HEV 智能能量管理实战:从 MPC/PPO 理论解析到 Python-Simulink 联合仿真闭环全流程
开发语言·python·算法·matlab·控制
啦啦啦_999928 分钟前
1. 一元/多元线性回归之 正规方程求解法
算法·回归·线性回归
众少成多积小致巨1 小时前
GNU Make 核心指南
android·c++
ECT-OS-JiuHuaShan1 小时前
整体论体系定理,全球开放,无法绕过
人工智能·科技·学习·算法·生活
贾斯汀玛尔斯1 小时前
每天学一个算法--BM25(Okapi BM25)
算法
谭欣辰1 小时前
详细讲解 C++ 状压 DP
开发语言·c++·动态规划
William_wL_1 小时前
【C++】stack和queue的使用和实现(附加deque的简单介绍)
开发语言·c++
山甫aa1 小时前
二叉树遍历----从零开始的数据结构
数据结构·c++·二叉树