博客
关于我
The Bits CodeForces - 1017B(组合数学,水)
阅读量:240 次
发布时间:2019-03-01

本文共 1385 字,大约阅读时间需要 4 分钟。

状态转换问题的代码解释

这段代码是用来解决一个典型的状态转换问题。程序通过统计输入字符串中每对字符的转移次数,计算了不同状态之间的转换次数。

代码分析

#include 
#include
#include
#include
using namespace std;typedef long long ll;const int maxn = 1e6 + 5;int main() { int n; cin >> n; string a, b; cin >> a >> b; ll num00 = 0, num01 = 0, num10 = 0, num11 = 0; for (int i = 0; i < n; ++i) { char c1 = a[i], c2 = b[i]; if (c1 == c2) { if (c1 == '0') num00++; else num11++; } else { if (c1 == '0' && c2 == '1') num01++; else if (c1 == '1' && c2 == '0') num10++; } } // 输出结果 // ...}

代码解释

1. 包含头文件

  • iostream:用于标准输入输出操作
  • string:字符串操作
  • algorithm:算法库
  • cctype:字符分类和常用字符函数

2. 定义

  • using namespace std;:导入标准库
  • typedef long long ll;:定义长长整数类型
  • const int maxn = 1e6 + 5;:定义常量,用于数组大小

3. 主函数

int main() {    int n;    cin >> n;    string a, b;    cin >> a >> b;    ll num00 = 0, num01 = 0, num10 = 0, num11 = 0;    for (int i = 0; i < n; ++i) {        char c1 = a[i], c2 = b[i];        if (c1 == c2) {            if (c1 == '0') num00++;            else num11++;        } else {            if (c1 == '0' && c2 == '1') num01++;            else if (c1 == '1' && c2 == '0') num10++;        }    }    // ...}

4. 主循环

  • 遍历每个字符对
  • 判断两个字符是否相同
    • 如果相同,根据字符值更新对应的计数器
  • 如果不同,根据具体转换关系更新计数器

5. 输出结果

  • 根据计数器值输出最终结果

这段代码通过遍历两个字符串,统计每对字符的转移次数,适用于解决状态转换类的问题。

转载地址:http://ifnt.baihongyu.com/

你可能感兴趣的文章
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和package.json那些不为常人所知的小秘密
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm如何清空缓存并重新打包?
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>