一些水题

https://www.luogu.com.cn/problem/CF374B

点击查看代码
#include <bits/stdc++.h>
using namespace std;

/*
  CF374B - Inna and Nine

  思路:
  - 找到字符串中所有 maximal 的“相邻两位和为9”的连续段(段内任意相邻和都为9)。
  - 对每个段,设长度为 L:
      - 若 L 偶数:该段对不同最优终态的贡献是 1。
      - 若 L 奇数:该段对不同最优终态的贡献是 (L+1)/2。
  - 答案为对所有段贡献的乘积(long long)。

  复杂度:O(n)
*/

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string s;
    if (!(cin >> s)) return 0;

    long long ans = 1; // 结果
    int n = (int)s.size();
    int i = 0;
    while (i < n - 1) {
        // 如果当前位置与下一个位置和为9,则开始一个段
        if ((s[i]-'0') + (s[i+1]-'0') == 9) {
            int j = i;
            // 扩展段,段内每个相邻对和都应为9
            while (j < n - 1 && (s[j]-'0') + (s[j+1]-'0') == 9) ++j;
            // 段的数字个数 L = j - i + 1
            int L = j - i + 1;
            if (L % 2 == 1) {
                // 奇数长度段的贡献为 (L+1)/2
                ans *= (long long)((L + 1) / 2);
            }
            // 偶数段的贡献为 1,不用乘
            // 跳过这个段
            i = j + 1;
        } else {
            ++i;
        }
    }

    cout << ans << '\n';
    return 0;
}
posted @ 2025-11-12 17:29  katago  阅读(5)  评论(0)    收藏  举报