A. QAQ

A. QAQ

image

法1

由于数据范围比较小,可以三层循环求解

法2

每一个A的下标存到数组v中,开个cnt数组记录每个位置前面有多少个'Q',利用前缀和可以求得范围内'Q'的数量

\[ans = \sum_{i = 0}^{v.size()-1} cnt[i - 1] × (cnt[n] - cnt[i]) \]

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<cctype>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 110;
const int M = 2e5+10;
int n,m;
int cnt[N];
void solve(){
	string s;
	cin >> s;
	n = s.size();
	vector<int> v;
	for(int i = 0; i < n; i ++ ){
		if(s[i] == 'Q')cnt[i + 1] ++;
		else if(s[i] == 'A')v.push_back(i + 1);
		cnt[i + 1] += cnt[i];
	}
	LL ans = 0;
	for(auto x:v){
		ans += cnt[x - 1] * (cnt[n] - cnt[x]);
	}
	cout << ans;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}
posted @ 2023-03-11 11:38  Keith-  阅读(20)  评论(0编辑  收藏  举报