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;}
- codeforces 894-A
- codeforces 894A. QAQ
- codeforces 894A
- CodeForces-a
- Codeforces 903A A
- codeforces 894 A题 QAQ -mystical_curve
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- A. Divisibility CodeForces 597A
- codeforces 417A A. Elimination
- CodeForces 534A A. Exam
- codeforces#98 a
- codeforces#99 a
- codeforces 105 div2 A
- codeforces #121 A
- pokemon go 和 虚拟gps
- 13周:2.请编程设计一个登录界面,要求输入账号密码(不考虑事件)
- ubuntu扩展屏幕
- python 列表排序方法sort、sorted技巧篇
- 日常生活 -- 开博二周年
- codeforces 894-A
- 上小班研讨课的感想
- 状压dp Necklace
- Multiple Object Tracking Benchmark
- 如何在centos 6/7下面实现命令补全
- FPGA入门实验
- Redis学习笔记——(一)Redis安装配置
- JavaWeb学习之路(一)
- linux与window文件目录共享——samba配置及在windows映射