蓝桥杯2025年第十六届省赛真题-水质检测

C语言代码:

cs 复制代码
#include <stdio.h>
#include <string.h>

#define MAX_LEN 1000000

int main() {
    char a[MAX_LEN + 1], b[MAX_LEN + 1];
    // 使用 scanf 读取字符数组
    scanf("%s", a);
    scanf("%s", b);
    int ans = 0;
    int pre = -1;
    int state = -1;
    int len = strlen(a);
    for (int i = 0; i < len; i++) {
        if (a[i] == '.' && b[i] == '.') continue;
        if (pre != -1) ans += i - pre - 1;

        if (a[i] == '#' && b[i] == '#') state = 3;
        else if (a[i] == '#' && b[i] == '.') {
            if (state == 2) {
                ans++;
                state = 3;
            } else state = 1;
        } else if (a[i] == '.' && b[i] == '#') {
            if (state == 1) {
                ans++;
                state = 3;
            } else state = 2;
        }
        pre = i;
    }
    // 使用 printf 输出结果
    printf("%d", ans);
    return 0;
}
    

C++代码:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string a, b;
    // 使用 cin 读取 string 类型变量
    cin >> a >> b;
    int ans = 0;
    int pre = -1;
    int state = -1;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] == '.' && b[i] == '.') continue;
        if (pre != -1) ans += i - pre - 1;

        if (a[i] == '#' && b[i] == '#') state = 3;
        else if (a[i] == '#' && b[i] == '.') {
            if (state == 2) {
                ans++;
                state = 3;
            } else state = 1;
        } else if (a[i] == '.' && b[i] == '#') {
            if (state == 1) {
                ans++;
                state = 3;
            } else state = 2;
        }
        pre = i;
    }
    // 使用 cout 输出结果
    cout << ans;
    return 0;
}
相关推荐
8Qi83 小时前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
moeyui70510 小时前
LeetCode 380:Insert Delete GetRandom O(1) 题解和一些延伸
算法·leetcode·职场和发展
小欣加油11 小时前
leetcode3689最大子数组总值I
c++·算法·leetcode·职场和发展·贪心算法
海梨花12 小时前
字节面试高频算法题
java·算法·面试·职场和发展
小欣加油13 小时前
leetcode121买卖股票的最佳时机
数据结构·c++·算法·leetcode·职场和发展
酉鬼女又兒14 小时前
零基础入门计算机网络:集线器与交换机区别、以太网交换机自学习转发流程及生成树协议STP全解析
服务器·网络·网络协议·tcp/ip·计算机网络·考研·职场和发展
8Qi815 小时前
LeetCode 5:最长回文子串(Longest Palindromic Substring)—— 题解
算法·leetcode·职场和发展·动态规划
8Qi81 天前
LeetCode 1143 & 718:最长公共子序列 / 最长重复子数组
算法·leetcode·职场和发展·动态规划
嵌入式ZYXC1 天前
第3篇:《面试题:I2C为什么要加上拉电阻?阻值怎么选?》
stm32·单片机·嵌入式硬件·面试·职场和发展