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;
}
相关推荐
顺顺 尼几秒前
模板进阶和array
c++
MicroTech20259 分钟前
MLGO微算法科技发布多用户协同推理批处理优化系统,重构AI推理服务效率与能耗新标准
人工智能·科技·算法
一匹电信狗18 分钟前
【牛客CM11】链表分割
c语言·开发语言·数据结构·c++·算法·leetcode·stl
困鲲鲲20 分钟前
ROS2系列 (10) : C++话题通信节点——发布者示例
c++·ros2
..过云雨21 分钟前
11.【Linux系统编程】文件系统详解——从磁盘硬件到文件系统
linux·c++·后端·缓存
不染尘.25 分钟前
图的邻接矩阵实现以及遍历
开发语言·数据结构·vscode·算法·深度优先
码住懒羊羊33 分钟前
【C++】模板进阶 | 继承
android·java·c++
AndrewHZ39 分钟前
【图像处理基石】多波段图像融合算法入门:从概念到实践
图像处理·人工智能·算法·图像融合·遥感图像·多波段·变换域
yong99901 小时前
C++语法—类的声明和定义
开发语言·c++·算法
狂奔的sherry1 小时前
构造/析构/赋值运算理解
开发语言·c++