Codeforces Round #447 (Div. 2) A
Posted kissheart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #447 (Div. 2) A相关的知识,希望对你有一定的参考价值。
"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.
Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!).
Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don‘t have to be consecutive, but the order of letters should be exact.
The only line contains a string of length n (1 ≤ n ≤ 100). It‘s guaranteed that the string only contains uppercase English letters.
Print a single integer — the number of subsequences "QAQ" in the string.
QAQAQYSYIOIWIN
4
QAQQQZZYNOIWIN
3
题意
给一个字符串,找一共有里面多少个“QAQ”子序列(可以不连续)。
分析
可以暴力求解,这里用练习一下dp用dp解。一维不行就二维来讨论情况。
dp[i][1]表示,前i个中“Q”这种情况个数。
dp[i][2]表示,前i个中“QA”这种情况个数。
dp[i][3]表示,前i个中“QAQ”这种情况的个数。
再讨论一下当前字符是Q还是A状态转移就行了。
/// author:Kissheart /// #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<vector> #include<stdlib.h> #include<math.h> #include<queue> #include<deque> #include<ctype.h> #include<map> #include<set> #include<stack> #include<string> #define INF 0x3f3f3f3f #define FAST_IO ios::sync_with_stdio(false) const double PI = acos(-1.0); const double eps = 1e-6; const int MAX=1e6+10; const int mod=1e9+7; typedef long long ll; using namespace std; #define gcd(a,b) __gcd(a,b) inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;} inline ll inv1(ll b){return qpow(b,mod-2);} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;} inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;return x*f;} //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); string s; int n,dp[105][3]; int main() { cin>>s; n=s.size(); if(s[0]==‘Q‘) dp[0][1]=1; for(int i=1;i<n;i++) { dp[i][1]=dp[i-1][1]; dp[i][2]=dp[i-1][2]; dp[i][3]=dp[i-1][3]; if(s[i]==‘A‘) dp[i][2]+=dp[i][1]; else if(s[i]==‘Q‘) { dp[i][1]++; dp[i][3]+=dp[i][2]; } } printf("%d ",dp[n-1][3]); return 0; }
以上是关于Codeforces Round #447 (Div. 2) A的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #447 (Div. 2)
Codeforces Round #447 (Div. 2) A
Codeforces Round #447 (Div. 2) 题解 ABCDE
Codeforces Round #447 (Div. 2)