计算每一个A 前面P的个数 * 后面T的个数。
#include <bits/stdc++.h>
using namespace std;
struct node {
int p, t;
};
unordered_map<int, node> mp;
int main() {
int cntp = 0, cntt = 0, ans = 0;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'P') cntp++;
if (s[i] == 'A') mp[i].p = cntp;
}
for (int i = s.size() - 1; i >= 0; i--) {
if (s[i] == 'T') cntt++;
if (s[i] == 'A') mp[i].t = cntt;
}
for (auto it : mp) {
ans += it.second.p * it.second.t;
ans = ans % 1000000007;
}
printf ("%d", ans);
}
本文介绍了一个C++程序,用于统计输入字符串中每个'A'字符前后的'P'和'T'字符数量,并计算它们的乘积。该程序通过遍历字符串并利用unordered_map存储每个'A'的位置及其前后P和T计数,最后返回结果并对答案取模1000000007。
240

被折叠的 条评论
为什么被折叠?



