A. QAQ

A. QAQ

image

法1

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

法2

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

ans=i=0v.size()1cnt[i1]×(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 @   Keith-  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示