codeforces 894-A

来源:互联网 发布:淘宝无忧美淘店 编辑:程序博客网 时间:2024/06/14 11:26

A. QAQ
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
“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.

Input
The only line contains a string of length n (1 ≤ n ≤ 100). It’s guaranteed that the string only contains uppercase English letters.

Output
Print a single integer — the number of subsequences “QAQ” in the string.

Examples
input
QAQAQYSYIOIWIN
output
4
input
QAQQQZZYNOIWIN
output
3
Note
In the first example there are 4 subsequences “QAQ”: “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”.
AC(O(n^3))

#include<bits/stdc++.h>#define LL long long using namespace std;const int MAX = 1e6+10;const int INF = 0x3fffffff;char a[MAX];int main(){    cin>>a;    getchar();    int n = strlen(a);    int num=0;    for(int i=0;i<n-2;i++){        if(a[i]=='Q'){            for(int j=i+1;j<n-1;j++){                if(a[j]=='A'){                    for(int k=j+1;k<n;k++){                        if(a[k]=='Q'){                            num++;                        }                    }                }            }        }    }    cout<<num<<endl;    return 0;}

AC (O(n))

#include<bits/stdc++.h>#define LL long long using namespace std;const int MAX = 1e3+10;const int INF = 0x3fffffff;char s[MAX];int a[MAX];int b[MAX];struct node{    int id;    int left;    int right;}p[MAX];int main(){    cin>>s;    getchar();    int n = strlen(s);    int num=0;    int ans=0;    for(int i=0;i<n;i++){        if(s[i]=='Q'){            num++;        }        a[i]=num;        if(s[i]=='A'){            p[ans++].left=a[i];        }    }    int temp = ans;    num=0;    for(int i=n-1;i>=0;i--){        if(s[i]=='Q'){            num++;        }        b[n-i-1]=num;        if(s[i]=='A'){            p[--temp].right=b[n-i-1];            //cout<<n-i-1<<endl;        }    }    int final=0;    for(int i=0;i<ans;i++){        final+=p[i].left*p[i].right;    }//  printf("%d\n%d\n%d\n",ans,p[0].left,p[0].right);    cout<<final<<endl;    return 0;}
原创粉丝点击