博客
关于我
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/

你可能感兴趣的文章
NSJSON的用法(oc系统自带的解析方法)
查看>>
nslookup 的基本知识与命令详解
查看>>
NSNumber与NSInteger的区别 -bei
查看>>
NSOperation基本操作
查看>>
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
NT symbols are incorrect, please fix symbols
查看>>
ntelliJ IDEA 报错:找不到包或者找不到符号
查看>>
NTFS文件权限管理实战
查看>>
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
ntp server 用法小结
查看>>
ntpdate 通过外网同步时间
查看>>
ntpdate同步配置文件调整详解
查看>>
NTPD使用/etc/ntp.conf配置时钟同步详解
查看>>
NTP及Chrony时间同步服务设置
查看>>