"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
In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".
先从后往前遍历一遍,记录Q的个数,再从前往后遍历,
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lowbit(x) (x&(-x)) 4 #define max(x,y) (x>=y?x:y) 5 #define min(x,y) (x<=y?x:y) 6 #define MAX 100000000000000000 7 #define MOD 1000000007 8 #define pi acos(-1.0) 9 #define ei exp(1) 10 #define PI 3.1415926535897932384626433832 11 typedef long long ll; 12 #define INF 0x3f3f3f3f 13 #define maxn 200 14 char a[maxn]; 15 int dp[maxn]; 16 17 int main(){ 18 ios::sync_with_stdio(0); 19 scanf("%s",&a); 20 int len=strlen(a); 21 dp[len]=0; 22 for(int i=len-1;i>=0;i--){ 23 dp[i]=dp[i+1]; 24 if(a[i]=='Q') dp[i]++; 25 } 26 int ans=0,sum=0; 27 for(int i=0;i<len;i++){ 28 if(a[i]=='Q') ans++; 29 else if(a[i]=='A') sum+=ans*dp[i]; 30 } 31 printf("%d ",sum); 32 return 0; 33 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lowbit(x) (x&(-x)) 4 #define max(x,y) (x>=y?x:y) 5 #define min(x,y) (x<=y?x:y) 6 #define MAX 100000000000000000 7 #define MOD 1000000007 8 #define pi acos(-1.0) 9 #define ei exp(1) 10 #define PI 3.1415926535897932384626433832 11 typedef long long ll; 12 #define INF 0x3f3f3f3f 13 const int mod=1e9+7; 14 15 ll q_pow(ll n,ll m){ 16 ll ans=1; 17 while(m){ 18 if(m&1) ans=ans*n%mod; 19 m>>=1; 20 n=n*n%mod; 21 } 22 return ans; 23 } 24 int main(){ 25 ios::sync_with_stdio(0); 26 ll n, m,k; 27 while(cin>>n>>m>>k){ 28 if(((n%2)!=(m%2))&&k==-1) cout<<0<<endl; 29 else cout<<q_pow(q_pow(2,n-1),m-1)<<endl; 30 } 31 return 0; 32 }