从元旦开始的 n 天里,每天在打卡表上记下是否遵守了新年目标。 第一行给出 n、b、c,用一个空格隔开。 第二行以长度为 n 的字符串给出打卡表。 字符串的第 i 个字符是第 i 天的记录,大写英文字母 O 表示那天遵守了目标,大写英文字母 X 表示没有遵守。 遵守目标的那天会得到表扬贴纸。 设 s 为包括当天在内、从当天往前数 O 不间断连续的天数。 若 s ≤ c,当天得到 b^(s−1) 张贴纸。 若 s > c,当天得到 b^(c−1) 张贴纸。 也就是说,O 连续到第 c 天的那天得到 b^(c−1) 张,从连续第 c+1 天起一直是 b^(c−1) 张。 b^k 是 k 个 b 相乘的值,b^0 = 1。 X 的那天得不到贴纸,下一个 O 的那天 s 重新从 1 开始数。 输出 n 天里得到的贴纸总数。 如果一天也没有遵守目标,输出 0。 输出的数只写数字,不加逗号。
输入
1 ≤ n ≤ 366,2 ≤ b ≤ 10,1 ≤ c ≤ 40,b^(c−1) ≤ 1,000,000,000,000(10^12),打卡表只由 O 和 X 组成
n, b, c = map(int, input().split())days = input().strip()total = 0streak = 0 # 오늘까지 이어진 O의 날 수for ch in days: if ch == "O": streak += 1 # c일째부터는 늘지 않는다 total += b ** (min(streak, c) - 1) else: streak = 0print(total)
JavaScriptmain.js · 17 行
const [first, days] = require("fs").readFileSync(0, "utf8").split("\n");const [n, b, c] = first.trim().split(/\s+/).map(Number);let total = 0;let streak = 0; // 어제까지 이어진 O의 날 수let today = 0; // 오늘 받는 스티커for (const ch of days.trim()) { if (ch === "O") { // 첫날은 1장, 그다음은 c일째까지 b배씩 if (streak === 0) today = 1; else if (streak < c) today *= b; streak++; total += today; // 많아야 약 3.7 × 10^14라 2^53 안에서 정확하다 } else { streak = 0; }}console.log(total);
TypeScriptmain.ts · 18 行
import { readFileSync } from "node:fs";const [first, days] = readFileSync(0, "utf8").split("\n");const [n, b, c]: number[] = first.trim().split(/\s+/).map(Number);let total = 0;let streak = 0; // 어제까지 이어진 O의 날 수let today = 0; // 오늘 받는 스티커for (const ch of days.trim()) { if (ch === "O") { // 첫날은 1장, 그다음은 c일째까지 b배씩 if (streak === 0) today = 1; else if (streak < c) today *= b; streak++; total += today; // 많아야 약 3.7 × 10^14라 2^53 안에서 정확하다 } else { streak = 0; }}console.log(total);
JavaMain.java · 24 行
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); String days = sc.next(); long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다 long today = 0; int streak = 0; // 어제까지 이어진 O의 날 수 for (char ch : days.toCharArray()) { if (ch == 'O') { // 첫날은 1장, 그다음은 c일째까지 b배씩 if (streak == 0) today = 1; else if (streak < c) today *= b; streak++; total += today; } else { streak = 0; } } System.out.println(total); }}
C#Main.cs · 30 行
using System;class Program{ static void Main() { string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int b = int.Parse(t[1]), c = int.Parse(t[2]); string days = Console.ReadLine().Trim(); long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다 long today = 0; int streak = 0; // 어제까지 이어진 O의 날 수 foreach (char ch in days) { if (ch == 'O') { // 첫날은 1장, 그다음은 c일째까지 b배씩 if (streak == 0) today = 1; else if (streak < c) today *= b; streak++; total += today; } else { streak = 0; } } Console.WriteLine(total); }}
C++main.cpp · 23 行
#include <iostream>#include <string>int main() { int n, b, c; std::string days; std::cin >> n >> b >> c >> days; long long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다 long long today = 0; int streak = 0; // 어제까지 이어진 O의 날 수 for (char ch : days) { if (ch == 'O') { // 첫날은 1장, 그다음은 c일째까지 b배씩 if (streak == 0) today = 1; else if (streak < c) today *= b; streak++; total += today; } else { streak = 0; } } std::cout << total << "\n";}
Cmain.c · 23 行
#include <stdio.h>int main(void) { int n, b, c; char days[400]; scanf("%d %d %d %399s", &n, &b, &c, days); long long total = 0; /* 10^12장짜리 날이 하루만 있어도 int를 넘는다 */ long long today = 0; int streak = 0; /* 어제까지 이어진 O의 날 수 */ for (int i = 0; days[i] != '\0'; i++) { if (days[i] == 'O') { /* 첫날은 1장, 그다음은 c일째까지 b배씩 */ if (streak == 0) today = 1; else if (streak < c) today *= b; streak++; total += today; } else { streak = 0; } } printf("%lld\n", total); return 0;}