问题描述
p1585原题链接:UVaOJ 1585 - Score
相关说明:本题为《算法竞赛入门经典(第2版)》习题 3-1
解法一:模拟
读入的时候维护表示当前 O 连续数的 cnt 变量,累加到结果 res 即可。
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; } |
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) |