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;
}
相关推荐
有一个好名字3 分钟前
力扣-电话号码组合
算法·leetcode·职场和发展
闻缺陷则喜何志丹8 分钟前
【数论 快速指数幂 龟速乘】P8652 [蓝桥杯 2017 国 C] 小数第 n 位|普及+
c++·蓝桥杯·数论·快速指数幂·龟速乘
鱼跃鹰飞8 分钟前
Leetcode会员尊享面试100题:1086:前五科的均分
算法·leetcode·职场和发展
༾冬瓜大侠༿8 分钟前
C++string
c语言·开发语言·c++·算法
Lethehong10 分钟前
探索高效工作流的秘密:GLM-4.7 与 Dify 平台深度集成实践
大数据·人工智能·算法
Yeats_Liao11 分钟前
微调决策树:何时使用Prompt Engineering,何时选择Fine-tuning?
前端·人工智能·深度学习·算法·决策树·机器学习·prompt
sin_hielo13 分钟前
leetcode 3010
数据结构·算法·leetcode
sheji341614 分钟前
【开题答辩全过程】以 基于协同过滤算法电影个性化推荐系统设计与实现为例,包含答辩的问题和答案
算法
uesowys18 分钟前
Apache Spark算法开发指导-Random forest classifier
算法·随机森林·spark
仙俊红2 小时前
LeetCode487周赛T2,删除子数组后的最终元素
数据结构·算法