MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 1585 - Score
AOAPC II

UVaOJ 1585 - Score

Chai
2021-11-24 0:00:00在线评测阅读 243

问题描述

p1585

原题链接:UVaOJ 1585 - Score

相关说明:本题为《算法竞赛入门经典(第2版)》习题 3-1

解法一:模拟

读入的时候维护表示当前 O 连续数的 cnt 变量,累加到结果 res 即可。

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  int t;
  cin >> t;
  while (t--) {
    int res = 0, cnt = 0;
    string line;
    cin >> line;
    for (auto const &ch : line) {
      cnt = (ch == 'O') ? cnt + 1 : 0;
      res += cnt;
    }
    cout << res << endl;
  }
  return 0;
}
Python
1
2
3
4
5
6
7
8
t = int(input())
for _ in range(t):
    line = input()
    res = cnt = 0
    for ch in line:
        cnt = cnt + 1 if ch == "O" else 0
        res += cnt
    print(res)

AOAPC II UVaOJ 字符串
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz