题目描述
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.
输入格式
Each input file contains one test case. For each case, there is only one line giving a string of no more than 10^5
​​characters containing only P, A, or T.
输入格式
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.
输入样例

APPAPT

输出样例

2

全部AC

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 101000;
const int MOD = 1000000007; int main() {
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
char str[maxn];
scanf("%s", str);
int len = strlen(str);
// for(int i = 0; i < len; i++) {
// printf("%c", str[i]);
// }
int times[3] = {0};
long num = 0; //可以形成PAT的个数
int leftNumP[maxn] = {0}, rightNumT[maxn] = {0};
int temp = 0;
//遍历记录每一位左边字母P的数量
for(int i = 0; i < len; i++) {
if(str[i] == 'P'&& temp == 0) {
++leftNumP[i];
temp += 1;
} else if(str[i] == 'P' && temp != 0) {
leftNumP[i] = temp;
++leftNumP[i];
temp += 1;
} else leftNumP[i] = temp;
}
temp = 0;
long long ans = 0;//答案
//遍历记录每一位右边字母为T的数量
for(int i = len; i >= 0; i--) {
if(str[i] == 'T' && temp == 0) {
++rightNumT[i];
temp += rightNumT[i];
} else if(str[i] == 'T' && temp != 0) {
rightNumT[i] = temp;
++rightNumT[i];
temp += 1;
} else rightNumT[i] = temp;
}
for(int i = 0; i < len; i++) {
if(str[i] == 'A') {
ans = (ans + leftNumP[i] * rightNumT[i]) % MOD;
}
}
// for(int i = 0; i < len; i++) {
// printf("leftNumP[%d]:%d rightNumT[%d]:%d\n", i, leftNumP[i], i, rightNumT[i]);
// }
//直接暴力会超时
// for(int i = 0; i < len; i++){
// if(i != 0 && str[i] == 'A') {
// int leftNumP = 0; //左边字母P的数量
// int rightNumT = 0;//右边字母T的数量
// for(int j = 0; j < i; j++) {
// if(str[j] == 'P') leftNumP++;
// }
// for(int j = i; j < len; j++) {
// if(str[j] == 'T') rightNumT++;
// }
// num += leftNumP * rightNumT;
// //printf("i=%d leftNumP:%d rightNumT:%d num:%ld\n", i, leftNumP, rightNumT, num);
// }
// }
// int ans = num % 1000000007;
printf("%lld", ans);
return 0;
}

最新文章

  1. ftp文件的部署
  2. C++ 消息中间件(MQ4CPP)
  3. nginx修改配置后不生效的问题
  4. 小谈 - web模仿手机打电话与正则表达式
  5. vim 设定一个新的snippets
  6. Animate自定义动画
  7. 如何修改SVN客户端中保存的密码
  8. Jackson注解学习参考(转)
  9. WPF MVVM 架构 Step By Step(3)(把后台代码移到一个类中)
  10. 201521123093 java 第十四周学习总结
  11. Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型?
  12. Spark算子--partitionBy
  13. 关于git post-receive 钩子
  14. 关于JQuery的绑定方法
  15. scrapy学习笔记(1)
  16. 如何使用 ccs7.2调试代码
  17. HDU 3342 Legal or Not(有向图判环 拓扑排序)
  18. IOS 启动画面和图标设置(适配IOS7 and Xcode5)
  19. Q143 重排链表
  20. git 学习(2)--恢复版本

热门文章

  1. vs2017+opencv配置参考链接
  2. Mybati example generatorConfig.xml 配置详解
  3. 基于Python使用scrapy-redis框架实现分布式爬虫
  4. iOS UIBarButtonItem 固定尺寸
  5. 操作 SQL语句之数据操作语言(DML)详解
  6. Qt编写自定义控件18-魔法小鱼
  7. powershell自动添加静态IP
  8. 关于虚拟机可以ping通本机,本机可以ping通虚拟机,但是虚拟机就是不能上网
  9. mysql 如何给大表添加字段
  10. SCI EI 检索的会议期刊