C语言代码:
- #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++代码:
- #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;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |