1093. Count PAT's (25)
来源:互联网 发布:nemo软件好用吗 编辑:程序博客网 时间:2024/05/31 18:30
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.
Now given any string, you are supposed to tell the number of PAT's contained in the string.
Input Specification:
Each input file contains one test case. For each case, there is only one line giving a string of no more than 105characters containing only P, A, or T.
Output Specification:
For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.
Sample Input:APPAPTSample Output:
2
如果直接暴力解决肯定超时,120ms
可以先做一个处理,把位置i前面的P的个数求出来
位置i后面T的个数求出来
然后在遍历一次求总数
我第一次提交3个超时点。把字符串长度赋值给一个变量,不要每次都进行strlen(s)这个操作。浪费时间
#include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;int main(){char s[100001];gets(s);long long p[100001];long long t[100001];p[0]=s[0]=='P'?1:0; int len=strlen(s);for(int i=1;i<len;i++){if(s[i]=='P'){p[i]=p[i-1]+1;}else p[i]=p[i-1];}t[len-1]=s[len-1]=='T'?1:0;for(int i=len-2;i>=0;i--){if(s[i]=='T'){t[i]=t[i+1]+1;}else t[i]=t[i+1];}long long sum=0;for(int i=0;i<len;i++){if(s[i]=='A'){//cout<<i<<" "<<p[i]<<" "<<t[i]<<endl;sum=(sum%1000000007+(p[i]*t[i])%1000000007)%1000000007;}}cout<<sum%1000000007;return 0;}
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- nodejs读取文件时相对路径的正确写法(使用fs模块)
- aop基本概念
- jdbc基本知识(二)
- 欢迎使用CSDN-markdown编辑器
- flowable 6.1.2 命令行完成请假审批流程的例子
- 1093. Count PAT's (25)
- UVA
- eclipse快捷键整理
- [BZOJ]4689: Find the Outlier 高斯消元
- Languages--CSU-ACM2017暑期训练1-Debug与STL
- UVA 548 Tree(中序+后序还原二叉树)
- HDOJ 6055-Regular polygon
- String,StringBuffer,StringBuilder 三者的讨论
- aop源码解析一:注册BPP