牛客周賽 Round 36 (小白練習記)

开始优化發表於2024-03-10

A.小紅的數位刪除

思路:

這題簡單輸出即可

Code:

#include <bits/stdc++.h>
    
using namespace std; 

int main() { 
    string s; cin >> s;
    for (int i = 0; i < s.size() - 3; i++) {
        cout << s[i];
    }
    return 0;
}

  

B.小紅的小紅矩陣構造

思路:

所有元素之和恰好等於x,且每行、每列的異或和全部相等使用set或者map等等都可以儲存異或值,按題意來就行

Code:

#include <bits/stdc++.h>
     
using namespace std;
 
const int N = 1e2 + 5;
int a[N][N], sum, tmp, n, m, x;
set<int> st;
 
int main() {
    cin >> n >> m >> x;
    for (int i = 1; i <= n; i++) {
        tmp = 0;
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j]; sum += a[i][j]; tmp ^= a[i][j];
        }
        st.insert(tmp);
    }
    for (int j = 1; j <= m; j++) {
        tmp = 0;
        for (int i = 1; i <= n; i++) {
            tmp ^= a[i][j];
        }
        st.insert(tmp);
    }
    if (sum == x && st.size() == 1)
        cout << "accepted\n";
    else  
        cout << "wrong answer\n";
    return 0;
}

  

  

C.小紅的白色字串

思路:

1-開頭第一個大寫,其餘小寫(len=1也算)

2-所有都是小寫

所以從前往後判定即可

Code:

#include <bits/stdc++.h>
    
using namespace std;
    
int main() {
    string s; cin >> s;
    int cnt = 0;
    for (int i = 1; i < s.size(); i++) {
        if (s[i] >= 'A' && s[i] <= 'Z' && s[i - 1] != ' ') {
            cnt++; s[i] = ' ';
        }
    }
    cout << cnt;
    return 0;
}

  

D.小紅走矩陣

思路:

一個模板題bfs,能不能到達終點,能就輸出最短的長度,不能輸出-1

Code:

#include <bits/stdc++.h>
    
using namespace std; 
const int N = 1e3 + 5;

char a[N][N];
bool vis[N][N];
int n, m; 

int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

bool isVaild(int x, int y) {
    return !vis[x][y] && x >= 1 && x <= n && y >= 1 && y <= m;
}

void bfs() {
    queue<array<int, 3>> q;
    q.push({1, 1, 0});
    vis[1][1] = 1;

    while (!q.empty()) {
        auto [x, y, cnt] = q.front();
        q.pop();
        if (x == n && y == m) {
            cout << cnt;
            return ;
        }
        for (int i = 0; i < 4; i++) {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if (isVaild(dx, dy) && a[x][y] != a[dx][dy]) {
                vis[dx][dy] = 1;
                q.push({dx, dy, cnt + 1});
            }
        }
    }
    cout << -1;
}

int main() { 
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    bfs();
    return 0;
}